Skip to content

Commit

Permalink
Pretty core names (#449)
Browse files Browse the repository at this point in the history
* Display core name in file explorer

* Add info about system

* Clean

* Rename filter

* Rename stuff

* Tests
  • Loading branch information
kivutar authored Jul 17, 2021
1 parent 1112deb commit 3d847ae
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 16 deletions.
20 changes: 17 additions & 3 deletions menu/menu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,14 @@ func Test_buildExplorer(t *testing.T) {
os.Create(tmp + "File 7.txt")
os.Mkdir(tmp+"Folder 1", 0777)

scene := buildExplorer(os.TempDir()+"/Test_buildExplorer/", []string{".img"}, cbMock, dirActionMock)
prettify := func(in string) string {
if in == "File 4" {
return "IMAGE 4"
}
return in
}

scene := buildExplorer(os.TempDir()+"/Test_buildExplorer/", []string{".img"}, cbMock, dirActionMock, prettify)
menu.Push(scene)

children := scene.Entry().children
Expand All @@ -146,7 +153,7 @@ func Test_buildExplorer(t *testing.T) {

t.Run("Files have file icon", func(t *testing.T) {
if children[2].icon != "file" {
t.Errorf("buildExplorer = %v, want %v", children[1].icon, "file")
t.Errorf("buildExplorer = %v, want %v", children[2].icon, "file")
}
})

Expand All @@ -159,7 +166,7 @@ func Test_buildExplorer(t *testing.T) {

t.Run("Folders have folder icon", func(t *testing.T) {
if children[4].icon != "folder" {
t.Errorf("buildExplorer = %v, want %v", children[1].icon, "folder")
t.Errorf("buildExplorer = %v, want %v", children[4].icon, "folder")
}
})

Expand All @@ -172,6 +179,13 @@ func Test_buildExplorer(t *testing.T) {
t.Errorf("buildExplorer = %v, want %v", len(menu.stack), 2)
}
})

t.Run("Prettifier should work", func(t *testing.T) {
want := "IMAGE 4"
if children[3].label != want {
t.Errorf("buildExplorer = %v, want %v", children[3].label, want)
}
})
}

func TestExtractTags(t *testing.T) {
Expand Down
29 changes: 19 additions & 10 deletions menu/scene_explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type sceneExplorer struct {
entry
}

// Prettifier processes a file name
type Prettifier func(string) string

func matchesExtensions(f os.FileInfo, exts []string) bool {
if len(exts) > 0 {
var fileExtension = filepath.Ext(f.Name())
Expand Down Expand Up @@ -46,7 +49,7 @@ func getWindowsDrives() (drives []string) {
return drives
}

func appendFolder(list *sceneExplorer, label, newPath string, exts []string, cb func(string), dirAction *entry) {
func appendFolder(list *sceneExplorer, label, newPath string, exts []string, cb func(string), dirAction *entry, prettifier Prettifier) {
list.children = append(list.children, entry{
label: label,
icon: "folder",
Expand All @@ -56,7 +59,7 @@ func appendFolder(list *sceneExplorer, label, newPath string, exts []string, cb
if dirAction != nil {
dirAction.callbackOK = func() { cb(newPath) }
}
menu.Push(buildExplorer(newPath, exts, cb, dirAction))
menu.Push(buildExplorer(newPath, exts, cb, dirAction, prettifier))
},
})
}
Expand All @@ -69,7 +72,7 @@ func explorerIcon(f os.FileInfo) string {
return icon
}

func appendNode(list *sceneExplorer, fullPath string, name string, f os.FileInfo, exts []string, cb func(string), dirAction *entry) {
func appendNode(list *sceneExplorer, fullPath string, name string, f os.FileInfo, exts []string, cb func(string), dirAction *entry, prettifier Prettifier) {
// Check whether or not we are to display hidden files.
if name[:1] == "." && !settings.Current.ShowHiddenFiles {
return
Expand All @@ -80,8 +83,14 @@ func appendNode(list *sceneExplorer, fullPath string, name string, f os.FileInfo
return
}

// Process file name if needed, used for user friendly file names
displayName := name
if prettifier != nil {
displayName = prettifier(utils.FileName(name))
}

list.children = append(list.children, entry{
label: name,
label: displayName,
icon: explorerIcon(f),
callbackOK: func() {
if f.IsDir() {
Expand All @@ -90,15 +99,15 @@ func appendNode(list *sceneExplorer, fullPath string, name string, f os.FileInfo
if dirAction != nil {
dirAction.callbackOK = func() { cb(newPath) }
}
menu.Push(buildExplorer(newPath, exts, cb, dirAction))
menu.Push(buildExplorer(newPath, exts, cb, dirAction, prettifier))
} else if cb != nil && (exts == nil || utils.StringInSlice(filepath.Ext(name), exts)) {
cb(filepath.Clean(fullPath))
}
},
})
}

func buildExplorer(path string, exts []string, cb func(string), dirAction *entry) Scene {
func buildExplorer(path string, exts []string, cb func(string), dirAction *entry, prettifier Prettifier) Scene {
var list sceneExplorer
list.label = "Explorer"

Expand All @@ -113,17 +122,17 @@ func buildExplorer(path string, exts []string, cb func(string), dirAction *entry
if runtime.GOOS == "windows" {
drives := getWindowsDrives()
for _, drive := range drives {
appendFolder(&list, drive+":\\", drive+":\\", exts, cb, dirAction)
appendFolder(&list, drive+":\\", drive+":\\", exts, cb, dirAction, prettifier)
}
list.segueMount()
return &list
}
} else if isWindowsDrive(path) {
// Special .. entry pointing to the list of drives on Windows
appendFolder(&list, "..", "/", exts, cb, dirAction)
appendFolder(&list, "..", "/", exts, cb, dirAction, prettifier)
} else {
// Add a first entry for the parent directory.
appendFolder(&list, "..", filepath.Clean(path+"/.."), exts, cb, dirAction)
appendFolder(&list, "..", filepath.Clean(path+"/.."), exts, cb, dirAction, prettifier)
}

files, err := ioutil.ReadDir(path)
Expand All @@ -144,7 +153,7 @@ func buildExplorer(path string, exts []string, cb func(string), dirAction *entry
log.Println(err)
continue
}
appendNode(&list, fullPath, f.Name(), fi, exts, cb, dirAction)
appendNode(&list, fullPath, f.Name(), fi, exts, cb, dirAction, prettifier)
}
buildIndexes(&list.entry)

Expand Down
44 changes: 44 additions & 0 deletions menu/scene_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,48 @@ type sceneMain struct {
entry
}

var prettyCoreNames = map[string]string{
"atari800_libretro": "Atari 800 (Atari 5200)",
"bluemsx_libretro": "BlueMSX (MSX)",
"fbneo_libretro": "FBNeo (Arcade)",
"fceumm_libretro": "Fceumm (NES)",
"gambatte_libretro": "Gambatte (Game Boy)",
"genesis_plus_gx_libretro": "Genesis Plus GX (Genesis, Master System, Sega CD)",
"handy_libretro": "Handy (Atari Lynx)",
"lutro_libretro": "Lutro (LÖVE)",
"mednafen_ngp_libretro": "Beetle NeoGeo Pocket",
"mednafen_pce_fast_libretro": "Beetle PC-Engine Fast",
"mednafen_pce_libretro": "Beetle PC-Engine",
"mednafen_pcfx_libretro": "Beetle PC-FX",
"mednafen_psx_libretro": "Beetle PlayStation",
"mednafen_saturn_libretro": "Beetle Saturn",
"mednafen_supergrafx_libretro": "Beetle SuperGrafx",
"mednafen_vb_libretro": "Beetle VirtualBoy",
"mednafen_wswan_libretro": "Beetle WonderSwan",
"melonds_libretro": "MelonDS (Nintendo DS)",
"mgba_libretro": "mGBA (Game Boy Advance)",
"np2kai_libretro": "NP2Kai (PC-98)",
"o2em_libretro": "O2EM (Odyssey²)",
"pcsx_rearmed_libretro": "PCSX Rearmed (PLayStation)",
"picodrive_libretro": "PicoDrive (Genesis, 32X)",
"pokemini_libretro": "PokeMini (Pokemon Mini)",
"prosystem_libretro": "Prosystem (Atari 7800)",
"sameboy_libretro": "SameBoy (Game Boy)",
"snes9x_libretro": "Snes9x (SNES)",
"stella2014_libretro": "Stella 2014 (Atari 2600)",
"swanstation_libretro": "SwanStation (PlayStation)",
"vecx_libretro": "VecX (Vectrex)",
"virtualjaguar_libretro": "Virtual Jaguar (Atari Jaguar)",
}

func prettifyCoreName(in string) string {
name, ok := prettyCoreNames[in]
if ok {
return name
}
return in
}

func buildMainMenu() Scene {
var list sceneMain
list.label = "Main Menu"
Expand Down Expand Up @@ -44,6 +86,7 @@ func buildMainMenu() Scene {
[]string{".dll", ".dylib", ".so"},
coreExplorerCb,
nil,
prettifyCoreName,
))
},
})
Expand All @@ -59,6 +102,7 @@ func buildMainMenu() Scene {
nil,
gameExplorerCb,
nil,
nil,
))
} else {
ntf.DisplayAndLog(ntf.Warning, "Menu", "Please load a core first.")
Expand Down
5 changes: 3 additions & 2 deletions menu/scene_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ func buildSettings() Scene {
&entry{
label: "<Select this directory>",
icon: "scan",
}),
)
},
nil,
))
},
})
} else {
Expand Down
4 changes: 3 additions & 1 deletion menu/scene_tabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ func buildTabs() Scene {
&entry{
label: "<Scan this directory>",
icon: "scan",
}))
},
nil,
))
},
})

Expand Down

0 comments on commit 3d847ae

Please sign in to comment.