From fe244c7e682a15c13bf2b7b2941c14261b0ec607 Mon Sep 17 00:00:00 2001 From: Hazel Noack Date: Thu, 9 Oct 2025 12:53:25 +0200 Subject: [PATCH] merging unrelated objects into one --- internal/cli/shell.go | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/internal/cli/shell.go b/internal/cli/shell.go index ed37b4a..5e54d24 100644 --- a/internal/cli/shell.go +++ b/internal/cli/shell.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "os" + "regexp" "strconv" "strings" @@ -77,6 +78,8 @@ func (s musicObjectStore) currentMusicObjects() ([]data.MusicObject, error) { return (s)[len(s)-1], nil } +var indexSelectionPattern = regexp.MustCompile(`^[\d ,]+$`) + func interpretCommand(command string, store musicObjectStore) (musicObjectStore, error) { // going back in history if command == ".." { @@ -88,21 +91,33 @@ func interpretCommand(command string, store musicObjectStore) (musicObjectStore, } // fetch special music object - if index, err := strconv.Atoi(command); err == nil { + if indexSelectionPattern.MatchString(command) { currentMusicObjects, err := store.currentMusicObjects() if err != nil { return store, err } - if index >= len(currentMusicObjects) || index < 0 { - return store, errors.New(command + " is out of bounds [0 <= " + strconv.Itoa(index) + " <= " + strconv.Itoa(len(currentMusicObjects)-1) + "]") - } + var fetched data.MusicObject - current := currentMusicObjects[index] + for _, stringIndex := range strings.Split(command, ",") { + index, _ := strconv.Atoi(strings.TrimSpace(stringIndex)) - fetched, err := plugin.Fetch(current) - if err != nil { - return store, err + if index >= len(currentMusicObjects) || index < 0 { + return store, errors.New(command + " is out of bounds [0 <= " + strconv.Itoa(index) + " <= " + strconv.Itoa(len(currentMusicObjects)-1) + "]") + } + + current := currentMusicObjects[index] + + newFetched, err := plugin.Fetch(current) + if err != nil { + return store, err + } + + if fetched == nil { + fetched = newFetched + } else { + fetched = fetched.Merge(newFetched) + } } return append(store, fetched.Related()), nil