mirror of
https://github.com/agresdominik/cheat_sheet.git
synced 2026-04-21 10:01:58 +00:00
Moved some files, implemented Dockerfile for tests, implemented makefile with install functionality
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
FROM golang:1.24-alpine AS builder
|
||||||
|
WORKDIR /app
|
||||||
|
COPY . .
|
||||||
|
RUN apk add --no-cache make
|
||||||
|
RUN make install
|
||||||
|
CMD ["cheatsh"]
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
EXECUTABLE = cheatsh
|
||||||
|
PACKAGES = ./src/.
|
||||||
|
BINDIR = bin
|
||||||
|
SYSCONFDIR = /etc/cheatsh
|
||||||
|
PREFIX ?= /usr/local
|
||||||
|
|
||||||
|
all: build
|
||||||
|
|
||||||
|
build:
|
||||||
|
@mkdir -p bin
|
||||||
|
go build -o bin/$(EXECUTABLE) $(PACKAGES)
|
||||||
|
|
||||||
|
install: build
|
||||||
|
|
||||||
|
@echo "Installing binary in $(PREFIX)"
|
||||||
|
install -Dm755 bin/$(EXECUTABLE) $(PREFIX)/bin/$(EXECUTABLE)
|
||||||
|
|
||||||
|
@echo "Copying config files to $(SYSCONFDIR)"
|
||||||
|
install -Dm644 data/commands.json $(SYSCONFDIR)/commands.json
|
||||||
|
install -Dm644 data/commands_template.json $(SYSCONFDIR)/commands_template.json
|
||||||
|
|
||||||
|
uninstall:
|
||||||
|
rm -f $(PREFIX)/bin/$(EXECUTABLE)
|
||||||
|
rm -rf $(SYSCONFDIR)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf bin/
|
||||||
|
|
||||||
|
docker:
|
||||||
|
docker build -t cheatsh-test .
|
||||||
|
docker run -it --rm cheatsh-test /bin/sh
|
||||||
@@ -68,5 +68,11 @@
|
|||||||
"command": "GOOS=linux GOARCH=amd64 go build -o bin/myprogram.exe main.go",
|
"command": "GOOS=linux GOARCH=amd64 go build -o bin/myprogram.exe main.go",
|
||||||
"desc": "Cross-compile for Linux (alt. windows, darwin (arm64 / amd64))"
|
"desc": "Cross-compile for Linux (alt. windows, darwin (arm64 / amd64))"
|
||||||
}
|
}
|
||||||
|
],
|
||||||
|
"git": [
|
||||||
|
{
|
||||||
|
"command": "git checkout -b branch_name",
|
||||||
|
"desc": "Create and checkout a new branch"
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"language/system": [
|
||||||
|
{
|
||||||
|
"command": "the actual command",
|
||||||
|
"desc": "Describe the command here"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "and add any amount of commands below",
|
||||||
|
"desc": "Describe them"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"different language/system": [
|
||||||
|
{
|
||||||
|
"command": "the command for a different language",
|
||||||
|
"desc": "Describe it"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
module github.com/dominikagres/cheat_sheet
|
||||||
|
|
||||||
|
go 1.24.1
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/gdamore/tcell/v2 v2.8.1
|
||||||
|
github.com/rivo/tview v0.42.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/gdamore/encoding v1.0.1 // indirect
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||||
|
github.com/mattn/go-runewidth v0.0.16 // indirect
|
||||||
|
github.com/rivo/uniseg v0.4.7 // indirect
|
||||||
|
golang.org/x/sys v0.29.0 // indirect
|
||||||
|
golang.org/x/term v0.28.0 // indirect
|
||||||
|
golang.org/x/text v0.21.0 // indirect
|
||||||
|
)
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
github.com/gdamore/encoding v1.0.1 h1:YzKZckdBL6jVt2Gc+5p82qhrGiqMdG/eNs6Wy0u3Uhw=
|
||||||
|
github.com/gdamore/encoding v1.0.1/go.mod h1:0Z0cMFinngz9kS1QfMjCP8TY7em3bZYeeklsSDPivEo=
|
||||||
|
github.com/gdamore/tcell/v2 v2.8.1 h1:KPNxyqclpWpWQlPLx6Xui1pMk8S+7+R37h3g07997NU=
|
||||||
|
github.com/gdamore/tcell/v2 v2.8.1/go.mod h1:bj8ori1BG3OYMjmb3IklZVWfZUJ1UBQt9JXrOCOhGWw=
|
||||||
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||||
|
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
|
||||||
|
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||||
|
github.com/rivo/tview v0.42.0 h1:b/ftp+RxtDsHSaynXTbJb+/n/BxDEi+W3UfF5jILK6c=
|
||||||
|
github.com/rivo/tview v0.42.0/go.mod h1:cSfIYfhpSGCjp3r/ECJb+GKS7cGJnqV8vfjQPwoXyfY=
|
||||||
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
|
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||||
|
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||||
|
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||||
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
|
||||||
|
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
|
||||||
|
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||||
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
|
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
|
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
|
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||||
|
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||||
|
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||||
|
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||||
|
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
|
||||||
|
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||||
|
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||||
|
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
|
||||||
|
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||||
|
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
|
||||||
|
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
|
||||||
|
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
|
||||||
|
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
|
||||||
|
golang.org/x/term v0.28.0 h1:/Ts8HFuMR2E6IP/jlo7QVLZHggjKQbhu/7H0LJFr3Gg=
|
||||||
|
golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
|
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||||
|
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
|
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||||
|
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
|
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||||
|
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
|
||||||
|
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
|
||||||
|
func testFunction() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
+25
-83
@@ -1,96 +1,38 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"flag"
|
||||||
|
|
||||||
"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() {
|
func main() {
|
||||||
commands, err := loadCommands("data/commands.json")
|
|
||||||
if err != nil {
|
configFlag := flag.String("config", "", "Specify a config file")
|
||||||
log.Fatalf("Cannot load commands file: %v", err)
|
helpFlag := flag.Bool("help", false, "Show help")
|
||||||
|
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if len(os.Args) == 1 {
|
||||||
|
StartTui()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
app := tview.NewApplication()
|
switch {
|
||||||
|
case *helpFlag:
|
||||||
// Sort alphabetically
|
printHelp()
|
||||||
langs := make([]string, 0, len(commands))
|
case *configFlag != "":
|
||||||
for lang := range commands {
|
StartTui(*configFlag)
|
||||||
langs = append(langs, lang)
|
default:
|
||||||
}
|
printHelp()
|
||||||
sort.Strings(langs)
|
os.Exit(1)
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func printHelp() {
|
||||||
|
fmt.Println(`Usage: cheatsh [options]
|
||||||
|
Options:
|
||||||
|
--config <file> Specify a config file
|
||||||
|
--help Show this help message`)
|
||||||
|
}
|
||||||
|
|||||||
+111
@@ -0,0 +1,111 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/gdamore/tcell/v2"
|
||||||
|
"github.com/rivo/tview"
|
||||||
|
)
|
||||||
|
|
||||||
|
var commandsFile []byte
|
||||||
|
|
||||||
|
type CmdItem struct {
|
||||||
|
CommandName string `json:"command"`
|
||||||
|
CommandDescription string `json:"desc"`
|
||||||
|
}
|
||||||
|
type CmdMap map[string][]CmdItem
|
||||||
|
|
||||||
|
|
||||||
|
func StartTui(configFilePath ...string) {
|
||||||
|
|
||||||
|
var configFilePathString string
|
||||||
|
if len(configFilePath) > 0 {
|
||||||
|
configFilePathString = configFilePath[0]
|
||||||
|
} else {
|
||||||
|
configFilePathString = "/etc/cheatsh/commands.json"
|
||||||
|
}
|
||||||
|
commands, err := loadCommands(configFilePathString)
|
||||||
|
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
|
||||||
|
langList.AddItem(lang, "", 0, func() {
|
||||||
|
showCommandList(app, lang, commands[lang], langList)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
langList.SetWrapAround(false).SetHighlightFullLine(true)
|
||||||
|
langList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
||||||
|
if event.Rune() == 'b' || event.Key() == tcell.KeyEscape {
|
||||||
|
app.Stop()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
return event
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := app.SetRoot(langList, true).Run(); err != nil {
|
||||||
|
log.Fatalf("Error initialising TUI: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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() {
|
||||||
|
app.Suspend(func() {
|
||||||
|
fmt.Println(c.CommandName)
|
||||||
|
})
|
||||||
|
app.Stop()
|
||||||
|
os.Exit(0)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdList.SetWrapAround(false).SetHighlightFullLine(true)
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadCommands(path string) (CmdMap, error) {
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user