Skip to content

Commit c8aad4d

Browse files
author
Massiles Ghernaout
committed
init project structure + created a small demo using tview (about to remove it since it does almost everyhting and it is not suitable for learning purposes)
1 parent 7bb6e3e commit c8aad4d

File tree

6 files changed

+241
-0
lines changed

6 files changed

+241
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bin/*
2+
.env
3+
.env.local

BINARY_NAME

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
eddy

Makefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
VERSION:=$(shell cat ./VERSION)
3+
BINARY_NAME:=$(shell cat ./BINARY_NAME)
4+
AUTHOR:="Massiles Ghernaout"
5+
6+
7+
info:
8+
@echo "Project: ${BINARY_NAME}@${VERSION}"
9+
@echo "Author: ${AUTHOR}"
10+
11+
clean:
12+
rm -rf bin/*
13+
14+
bin:
15+
go build -ldflags "-X main.version=${VERSION} -X main.binary_name=${BINARY_NAME}" cmd/main.go -o bin/${BINARY_NAME}
16+
17+
18+
binstatic:
19+
rm -rf bin/*
20+
@echo "Building a static executable..."
21+
CGO_ENABLED=0 go build -a -tags netgo,osusergo -ldflags "-X main.version=${VERSION} -X main.binary_name=${BINARY_NAME} -extldflags '-static -s -w'" -o bin/${BINARY_NAME} cmd/main.go
22+
23+
run:
24+
./bin/${BINARY_NAME}
25+
26+
runsrc:
27+
CURR_DEV_ENV=dev go run ./cmd/main.go
28+
29+
30+
install:
31+
@echo "(Makefile): Not implemented yet."
32+
33+
34+
uninstall:
35+
@echo "(Makefile): Not implemented yet."

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v0.1.0

cmd/main.go

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/gdamore/tcell/v2"
9+
"github.com/rivo/tview"
10+
)
11+
12+
// will be populated by the build command
13+
var version string
14+
var binary_name string
15+
16+
func main() {
17+
if os.Getenv("CURR_DEV_ENV") == "dev" {
18+
version = "v0.1.0"
19+
binary_name = "eddy"
20+
}
21+
22+
app := tview.NewApplication()
23+
24+
rootDir := "."
25+
root := tview.NewTreeNode(rootDir).
26+
SetColor(tcell.ColorRed)
27+
tree := tview.NewTreeView().
28+
SetRoot(root).
29+
SetCurrentNode(root)
30+
31+
tree.SetBorder(true)
32+
33+
// A helper function which adds the files and directories of the given path
34+
// to the given target node.
35+
add := func(target *tview.TreeNode, path string) {
36+
// TODO: add a check for files
37+
files, err := os.ReadDir(path)
38+
if err != nil {
39+
fmt.Printf("err: %s\n", err.Error())
40+
}
41+
for _, file := range files {
42+
node := tview.NewTreeNode(file.Name()).
43+
SetReference(filepath.Join(path, file.Name())).
44+
SetSelectable(true)
45+
// SetSelectable(file.IsDir())
46+
if file.IsDir() {
47+
node.SetColor(tcell.ColorGreen)
48+
} else {
49+
node.SetSelectedFunc(func() {
50+
node.SetText(node.GetText() + " selected")
51+
})
52+
}
53+
target.AddChild(node)
54+
}
55+
}
56+
57+
// Add the current directory to the root node.
58+
add(root, rootDir)
59+
60+
// If a directory was selected, open it.
61+
tree.SetSelectedFunc(func(node *tview.TreeNode) {
62+
reference := node.GetReference()
63+
if reference == nil {
64+
return // Selecting the root node does nothing.
65+
}
66+
children := node.GetChildren()
67+
if len(children) == 0 {
68+
// Load and show files in this directory.
69+
path := reference.(string)
70+
add(node, path)
71+
} else {
72+
// Collapse if visible, expand if collapsed.
73+
node.SetExpanded(!node.IsExpanded())
74+
}
75+
})
76+
77+
textArea := tview.NewTextArea()
78+
textArea.SetTitle(fmt.Sprintf("%s@%s", binary_name, version)).SetBorder(true)
79+
80+
helpInfo := tview.NewTextView().SetText("Press F1 for help, press Ctrl-C to exit")
81+
82+
position := tview.NewTextView().SetDynamicColors(true).SetTextAlign(tview.AlignRight)
83+
pages := tview.NewPages()
84+
85+
textArea.SetMovedFunc(func() { updateCursorInfo(position, textArea) })
86+
updateCursorInfo(position, textArea)
87+
88+
mainView := tview.NewGrid().
89+
SetRows(0, 1).
90+
AddItem(tree, 0, 0, 1, 1, 0, 0, false).
91+
AddItem(textArea, 0, 1, 1, 2, 0, 0, true).
92+
AddItem(helpInfo, 1, 0, 1, 1, 0, 0, false).
93+
AddItem(position, 1, 2, 1, 1, 0, 0, false)
94+
95+
help1 := tview.NewTextView().
96+
SetDynamicColors(true).
97+
SetText(`[green]Navigation
98+
99+
[yellow]Left arrow[white]: Move left.
100+
[yellow]Right arrow[white]: Move right.
101+
[yellow]Down arrow[white]: Move down.
102+
[yellow]Up arrow[white]: Move up.
103+
[yellow]Ctrl-A, Home[white]: Move to the beginning of the current line.
104+
[yellow]Ctrl-E, End[white]: Move to the end of the current line.
105+
[yellow]Ctrl-F, page down[white]: Move down by one page.
106+
[yellow]Ctrl-B, page up[white]: Move up by one page.
107+
[yellow]Alt-Up arrow[white]: Scroll the page up.
108+
[yellow]Alt-Down arrow[white]: Scroll the page down.
109+
[yellow]Alt-Left arrow[white]: Scroll the page to the left.
110+
[yellow]Alt-Right arrow[white]: Scroll the page to the right.
111+
[yellow]Alt-B, Ctrl-Left arrow[white]: Move back by one word.
112+
[yellow]Alt-F, Ctrl-Right arrow[white]: Move forward by one word.
113+
114+
[blue]Press Enter for more help, press Escape to return.`,
115+
)
116+
117+
help2 := tview.NewTextView().
118+
SetDynamicColors(true).
119+
SetText(`[green]Editing[white]
120+
121+
Type to enter text.
122+
[yellow]Ctrl-H, Backspace[white]: Delete the left character.
123+
[yellow]Ctrl-D, Delete[white]: Delete the right character.
124+
[yellow]Ctrl-K[white]: Delete until the end of the line.
125+
[yellow]Ctrl-W[white]: Delete the rest of the word.
126+
[yellow]Ctrl-U[white]: Delete the current line.
127+
128+
[blue]Press Enter for more help, press Escape to return.`,
129+
)
130+
131+
help3 := tview.NewTextView().
132+
SetDynamicColors(true).
133+
SetText(`[green]Selecting Text[white]
134+
135+
Move while holding Shift or drag the mouse.
136+
Double-click to select a word.
137+
138+
[green]Clipboard
139+
140+
[yellow]Ctrl-Q[white]: Copy.
141+
[yellow]Ctrl-X[white]: Cut.
142+
[yellow]Ctrl-V[white]: Paste.
143+
144+
[green]Undo
145+
146+
[yellow]Ctrl-Z[white]: Undo.
147+
[yellow]Ctrl-Y[white]: Redo.
148+
149+
[blue]Press Enter for more help, press Escape to return.`,
150+
)
151+
152+
help := tview.NewFrame(help1).SetBorders(1, 1, 0, 0, 2, 2)
153+
154+
help.SetBorder(true).
155+
SetTitle("Help").
156+
SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
157+
if event.Key() == tcell.KeyEscape {
158+
pages.SwitchToPage("main")
159+
return nil
160+
} else if event.Key() == tcell.KeyEnter {
161+
switch { // can use an array and the modulo operator
162+
case help.GetPrimitive() == help1:
163+
help.SetPrimitive(help2)
164+
case help.GetPrimitive() == help2:
165+
help.SetPrimitive(help3)
166+
case help.GetPrimitive() == help3:
167+
help.SetPrimitive(help1)
168+
}
169+
return nil
170+
}
171+
return event
172+
})
173+
174+
pages.AddAndSwitchToPage("main", mainView, true).
175+
AddPage("help", tview.NewGrid().
176+
SetColumns(0, 64, 0).
177+
SetRows(0, 22, 0).
178+
AddItem(help, 1, 1, 1, 1, 0, 0, true), true, false)
179+
180+
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
181+
if event.Key() == tcell.KeyF1 {
182+
pages.ShowPage("help") //TODO: Check when clicking outside help window with the mouse. Then clicking help again.
183+
return nil
184+
}
185+
return event
186+
})
187+
188+
if err := app.SetRoot(pages,
189+
true).EnableMouse(true).Run(); err != nil {
190+
panic(err)
191+
}
192+
}
193+
func updateCursorInfo(position *tview.TextView, textArea *tview.TextArea) {
194+
fromRow, fromColumn, toRow, toColumn := textArea.GetCursor()
195+
if fromRow == toRow && fromColumn == toColumn {
196+
position.SetText(fmt.Sprintf("Row: [yellow]%d[white], Column: [yellow]%d ", fromRow, fromColumn))
197+
} else {
198+
position.SetText(fmt.Sprintf("[red]From[white] Row: [yellow]%d[white], Column: [yellow]%d[white] - [red]To[white] Row: [yellow]%d[white], To Column: [yellow]%d ", fromRow, fromColumn, toRow, toColumn))
199+
}
200+
}

todo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Extract the messages to an external resource file to be able to add i18n

0 commit comments

Comments
 (0)