diff --git a/data/commands.json b/data/commands.json new file mode 100644 index 0000000..d961305 --- /dev/null +++ b/data/commands.json @@ -0,0 +1,72 @@ +{ + "linux": [ + { + "command": "du -sh --block-size=1M /path/to/directory", + "desc": "Show size of directory in MB (Change M to G for GB)" + }, + { + "command": "df -h /path/to/mount", + "desc": "Show statistics about a drive (remove path for all)" + }, + { + "command": "find /path/to/directory -type f | wc -l", + "desc": "Show amount of files in directory and its sub-directories" + }, + { + "command": "find /path/to/directory -type f -exec grep -n 'search' {} +", + "desc": "Print all rows together with the filenames and rows a string is found in" + }, + { + "command": "ss -tuln", + "desc": "Show open ports on current host (linux)" + }, + { + "command": "echo Hostname: $(hostname); echo Local_IP: $(hostname -I | awk '{print $1}'); echo Default_Gateway: $(ip route | awk '/default/ {print $3}')", + "desc": "Print IP Address, Hostname, Default Route" + } + ], + "python": [ + { + "command": "python3 -m venv .venv; source .venv/bin/activate", + "desc": "Initialise a (simple) .venv and activate it" + }, + { + "command": "command", + "desc": "Initialise a poetry project" + }, + { + "command": "command", + "desc": "..." + } + ], + "go": [ + { + "command": "go mod init github.com/username/projectname", + "desc": "Initialise a go project" + }, + { + "command": "go get github.com/some/package@latest", + "desc": "Add dependencies (cringe)" + }, + { + "command": "go mod tidy", + "desc": "Tidy up unused dependencies" + }, + { + "command": "go mod verify", + "desc": "Verify Integrity" + }, + { + "command": "go run src/main.go", + "desc": "Run a main.go file" + }, + { + "command": "go build -o bin/myprogram main.go", + "desc": "Build an executable" + }, + { + "command": "GOOS=linux GOARCH=amd64 go build -o bin/myprogram.exe main.go", + "desc": "Cross-compile for Linux (alt. windows, darwin (arm64 / amd64))" + } + ] +} diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..5a74bbb --- /dev/null +++ b/src/main.go @@ -0,0 +1,96 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "os" + "sort" + + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" +) + +// Individual Command Items Later Extracted +type CmdItem struct { + CommandName string `json:"command"` + CommandDescription string `json:"desc"` +} + +// All commands +type CmdMap map[string][]CmdItem + +// Read Json-File and return it +func loadCommands(path string) (CmdMap, error) { + data, err := os.ReadFile(path) + if err != nil { + return nil, err + } + + var m CmdMap + if err := json.Unmarshal(data, &m); err != nil { + return nil, err + } + return m, nil +} + +// Function Called when a subset of functions has been selected +func showCommandList(app *tview.Application, lang string, cmds []CmdItem, langList *tview.List) { + cmdList := tview.NewList() + cmdList.SetBorder(true).SetTitle(fmt.Sprintf("%s commands", lang)) + + for _, c := range cmds { + c := c + cmdList.AddItem(c.CommandName, c.CommandDescription, 0, func() { + //fmt.Println(c.CommandName) + app.Suspend(func() { + fmt.Println(c.CommandName) // this now prints visibly + }) + app.Stop() + os.Exit(0) + }) + } + + cmdList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Rune() == 'b' || event.Key() == tcell.KeyEscape { + // Restore the language list + app.SetRoot(langList, true) + return nil + } + return event + }) + + app.SetRoot(cmdList, true) +} + +// Main Function +// Initialises the first page and starts app +func main() { + commands, err := loadCommands("data/commands.json") + if err != nil { + log.Fatalf("Cannot load commands file: %v", err) + } + + app := tview.NewApplication() + + // Sort alphabetically + langs := make([]string, 0, len(commands)) + for lang := range commands { + langs = append(langs, lang) + } + sort.Strings(langs) + + langList := tview.NewList() + langList.SetBorder(true).SetTitle("Select a topic") + + for _, lang := range langs { + lang := lang // capture loop variable + langList.AddItem(lang, "", 0, func() { + showCommandList(app, lang, commands[lang], langList) + }) + } + + if err := app.SetRoot(langList, true).Run(); err != nil { + log.Fatalf("Error initialising TUI: %v", err) + } +}