Refactored the map to a struct because maps are for some reason not sortable, added sorting

This commit is contained in:
2025-10-18 00:34:39 +02:00
parent b4f7007a9f
commit 43b1fbb64c
3 changed files with 93 additions and 58 deletions
+3
View File
@@ -57,3 +57,6 @@ dkms.conf
# Go # Go
bin/* bin/*
# Log Files
log/
+74 -4
View File
@@ -4,8 +4,32 @@ import (
"fmt" "fmt"
"os" "os"
"flag" "flag"
"encoding/json"
"log"
"sort"
) )
type CmdGroup struct {
Category string
Commands []CmdItem
}
type CmdList []CmdGroup
type CmdItem struct {
CommandName string `json:"command"`
CommandDescription string `json:"desc"`
}
func (list CmdList) Get(category string) []CmdItem {
for _, group := range list {
if group.Category == category {
return group.Commands
}
}
return nil
}
func main() { func main() {
@@ -15,22 +39,41 @@ func main() {
flag.Parse() flag.Parse()
if len(os.Args) == 1 { configFile := "/etc/cheatsh/commands.json"
StartTui()
var commands CmdList
var err error
if *configFlag != "" {
commands, err = loadCommands(*configFlag)
if err != nil {
log.Fatalf("Cannot load commands file: %v", err)
}
StartTui(commands)
return
} else if len(os.Args) == 1 {
commands, err = loadCommands(configFile)
if err != nil {
log.Fatalf("Cannot load commands file: %v", err)
}
StartTui(commands)
return return
} }
switch { switch {
case *helpFlag: case *helpFlag:
printHelp() printHelp()
case *configFlag != "":
StartTui(*configFlag)
case *newFlag: case *newFlag:
HandleInput() HandleInput()
default: default:
printHelp() printHelp()
os.Exit(1) os.Exit(1)
} }
} }
func printHelp() { func printHelp() {
@@ -40,3 +83,30 @@ func printHelp() {
--help Show this help message --help Show this help message
--new Add a new command to the config file`) --new Add a new command to the config file`)
} }
func loadCommands(path string) (CmdList, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var raw map[string][]CmdItem
err = json.Unmarshal(data, &raw)
if err != nil {
return nil, err
}
cmdList := make(CmdList, 0, len(raw))
for category, commands := range raw {
cmdList = append(cmdList, CmdGroup{
Category: category,
Commands: commands,
})
}
sort.Slice(cmdList, func(i, j int) bool {
return cmdList[i].Category < cmdList[j].Category
})
return cmdList, nil
}
+8 -46
View File
@@ -1,9 +1,7 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"log"
"os" "os"
//"sort" //"sort"
//"strings" //"strings"
@@ -15,11 +13,6 @@ import (
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
) )
type CmdMap map[string][]CmdItem
type CmdItem struct {
CommandName string `json:"command"`
CommandDescription string `json:"desc"`
}
type item struct { type item struct {
title string title string
@@ -33,7 +26,7 @@ type view int
type model struct { type model struct {
list list.Model list list.Model
commands CmdMap commands CmdList
currentView view currentView view
currentKey string currentKey string
selectedCmd string selectedCmd string
@@ -58,7 +51,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.currentKey = selected.title m.currentKey = selected.title
m.currentView = viewCommands m.currentView = viewCommands
m.list.Title = m.currentKey m.list.Title = m.currentKey
m.list.SetItems(cmdItemsToList(m.commands[m.currentKey])) m.list.SetItems(cmdItemsToList(m.commands.Get(m.currentKey)))
} else if m.currentView == viewCommands { } else if m.currentView == viewCommands {
m.selectedCmd = selected.title m.selectedCmd = selected.title
return m, tea.Quit return m, tea.Quit
@@ -70,7 +63,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.currentView == viewCommands { if m.currentView == viewCommands {
m.currentView = viewCategories m.currentView = viewCategories
m.list.Title = "Choose a list of commands" m.list.Title = "Choose a list of commands"
m.list.SetItems(cmdMapKeysToList(m.commands)) m.list.SetItems(cmdListKeysToList(m.commands))
} }
} }
@@ -98,20 +91,7 @@ const (
viewCommands viewCommands
) )
func StartTui(configFilePath ...string) { func StartTui(commands CmdList) {
configFile := "/etc/cheatsh/commands.json"
if len(configFilePath) > 0 {
configFile = configFilePath[0]
}
commands, err := loadCommands(configFile)
if err != nil {
log.Fatalf("Cannot load commands file: %v", err)
}
// ToDo: Sort Items
// ToDo: Handle readout of file before calling this function
delegate := list.NewDefaultDelegate() delegate := list.NewDefaultDelegate()
backKey := key.NewBinding( backKey := key.NewBinding(
@@ -129,7 +109,7 @@ func StartTui(configFilePath ...string) {
} }
} }
items := cmdMapKeysToList(commands) items := cmdListKeysToList(commands)
l := list.New(items, delegate, 0, 0) l := list.New(items, delegate, 0, 0)
l.Title = "Choose a list of commands" l.Title = "Choose a list of commands"
@@ -151,29 +131,11 @@ func StartTui(configFilePath ...string) {
} }
} }
func loadCommands(path string) (CmdMap, error) { func cmdListKeysToList(cmds CmdList) []list.Item {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var m CmdMap
err = json.Unmarshal(data, &m)
if err != nil {
return nil, err
}
return m, nil
}
func cmdMapKeysToList(m CmdMap) []list.Item {
items := []list.Item{} items := []list.Item{}
for _, group := range cmds {
for name, _ := range m {
listItem := item{ listItem := item{
title: name, title: group.Category,
description: "", description: "",
} }
items = append(items, listItem) items = append(items, listItem)