mirror of
https://github.com/agresdominik/cheat_sheet.git
synced 2026-04-21 18:05:51 +00:00
Refactored the map to a struct because maps are for some reason not sortable, added sorting
This commit is contained in:
@@ -57,3 +57,6 @@ dkms.conf
|
|||||||
|
|
||||||
# Go
|
# Go
|
||||||
bin/*
|
bin/*
|
||||||
|
|
||||||
|
# Log Files
|
||||||
|
log/
|
||||||
|
|||||||
+75
-5
@@ -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() {
|
||||||
@@ -38,5 +81,32 @@ func printHelp() {
|
|||||||
Options:
|
Options:
|
||||||
--config <file> Specify a config file
|
--config <file> Specify a config file
|
||||||
--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
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-53
@@ -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,34 +131,16 @@ func StartTui(configFilePath ...string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadCommands(path string) (CmdMap, error) {
|
func cmdListKeysToList(cmds CmdList) []list.Item {
|
||||||
|
items := []list.Item{}
|
||||||
data, err := os.ReadFile(path)
|
for _, group := range cmds {
|
||||||
if err != nil {
|
listItem := item{
|
||||||
return nil, err
|
title: group.Category,
|
||||||
}
|
description: "",
|
||||||
|
}
|
||||||
var m CmdMap
|
items = append(items, listItem)
|
||||||
err = json.Unmarshal(data, &m)
|
}
|
||||||
if err != nil {
|
return items
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return m, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func cmdMapKeysToList(m CmdMap) []list.Item {
|
|
||||||
|
|
||||||
items := []list.Item{}
|
|
||||||
|
|
||||||
for name, _ := range m {
|
|
||||||
listItem := item{
|
|
||||||
title: name,
|
|
||||||
description: "",
|
|
||||||
}
|
|
||||||
items = append(items, listItem)
|
|
||||||
}
|
|
||||||
return items
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmdItemsToList(cmds []CmdItem) []list.Item {
|
func cmdItemsToList(cmds []CmdItem) []list.Item {
|
||||||
|
|||||||
Reference in New Issue
Block a user