From 46a9c7dab3e11411267c164fe11681c8531a9715 Mon Sep 17 00:00:00 2001 From: "5684185+vsariola@users.noreply.github.com" <5684185+vsariola@users.noreply.github.com> Date: Sat, 25 Jan 2025 22:52:11 +0200 Subject: [PATCH] feat(tracker): preset names include their directories --- CHANGELOG.md | 2 ++ tracker/presets.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 823e81c..9094a58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,6 +77,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ([#156][i156]) ### Changed +- Presets get their name by concatenating their subdirectory path (with path + separators replaced with spaces) to their filename - The keyboard shortcuts are now again closer to what they were old trackers ([#151][i151]) - The stand-alone apps now output floating point sound, as made possible by diff --git a/tracker/presets.go b/tracker/presets.go index 96ec4ba..eabf4ba 100644 --- a/tracker/presets.go +++ b/tracker/presets.go @@ -5,7 +5,9 @@ import ( "io/fs" "os" "path/filepath" + "slices" "sort" + "strings" "github.com/vsariola/sointu" "github.com/vsariola/sointu/vm" @@ -179,6 +181,10 @@ func init() { } var instr sointu.Instrument if yaml.Unmarshal(data, &instr) == nil { + noExt := path[:len(path)-len(filepath.Ext(path))] + splitted := splitPath(noExt) + splitted = splitted[1:] // remove "presets" from the path + instr.Name = strings.Join(splitted, " ") instrumentPresets = append(instrumentPresets, instr) } return nil @@ -198,6 +204,13 @@ func init() { } var instr sointu.Instrument if yaml.Unmarshal(data, &instr) == nil { + if len(userPresets)+1 > len(path) { + return nil + } + subPath := path[len(userPresets)+1:] + noExt := subPath[:len(subPath)-len(filepath.Ext(subPath))] + splitted := splitPath(noExt) + instr.Name = strings.Join(splitted, " ") instrumentPresets = append(instrumentPresets, instr) } return nil @@ -206,6 +219,31 @@ func init() { sort.Sort(instrumentPresets) } +func splitPath(path string) []string { + subPath := path + var result []string + for { + subPath = filepath.Clean(subPath) // Amongst others, removes trailing slashes (except for the root directory). + + dir, last := filepath.Split(subPath) + if last == "" { + if dir != "" { // Root directory. + result = append(result, dir) + } + break + } + result = append(result, last) + + if dir == "" { // Nothing to split anymore. + break + } + subPath = dir + } + + slices.Reverse(result) + return result +} + func (p instrumentPresetsSlice) Len() int { return len(p) } func (p instrumentPresetsSlice) Less(i, j int) bool { return p[i].Name < p[j].Name } func (p instrumentPresetsSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }