-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpscreen.go
175 lines (144 loc) · 6.7 KB
/
helpscreen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"os"
"os/user"
"runtime"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type HelpScreen struct {
*tview.Box
fen *Fen
visible bool
scrollIndex int
}
func NewHelpScreen(fen *Fen) *HelpScreen {
return &HelpScreen{Box: tview.NewBox().SetBackgroundColor(tcell.ColorDefault), fen: fen}
}
type control struct {
KeyBindings []string
Description string
}
var helpScreenControlsList = []control{
{KeyBindings: []string{"?", "F1"}, Description: "Toggle help menu (you are here!)"},
{KeyBindings: []string{"F2"}, Description: "Show libraries used in fen"},
{KeyBindings: []string{"q"}, Description: "Quit fen"},
{KeyBindings: []string{"o"}, Description: "Options"},
{KeyBindings: []string{"z", "Backspace"}, Description: "Toggle hidden files"},
{KeyBindings: []string{"^Space", "^N"}, Description: "Open file(s) with specific program"},
{KeyBindings: []string{"!"}, Description: "Run system shell command"},
{KeyBindings: []string{"n"}, Description: "Create a new file"},
{KeyBindings: []string{"N"}, Description: "Create a new folder"},
{KeyBindings: []string{"y"}, Description: "Copy file"},
{KeyBindings: []string{"d"}, Description: "Cut file"},
{KeyBindings: []string{"p"}, Description: "Paste file"},
{KeyBindings: []string{"a"}, Description: "Rename a file"},
{KeyBindings: []string{"b"}, Description: "Bulk-rename files in editor"},
{KeyBindings: []string{"Del", "x"}, Description: "Delete file"},
{KeyBindings: []string{"/", "^F"}, Description: "Search"},
{KeyBindings: []string{"c"}, Description: "Goto path"},
{KeyBindings: []string{"Home", "g"}, Description: "Go to the top"},
{KeyBindings: []string{"End", "G"}, Description: "Go to the bottom"},
{KeyBindings: []string{"Ctrl+Left"}, Description: "Go to the root folder"},
{KeyBindings: []string{"Ctrl+Right"}, Description: "Go to the path furthest down in history"},
{KeyBindings: []string{"M"}, Description: "Go to the middle"},
{KeyBindings: []string{"PgUp", "PgDn"}, Description: "Scroll up/down an entire page"},
{KeyBindings: []string{"H"}, Description: "Go to the top of the screen"},
{KeyBindings: []string{"L"}, Description: "Go to the bottom of the screen"},
{KeyBindings: []string{"Space"}, Description: "Select files"},
{KeyBindings: []string{"A"}, Description: "Flip selection in folder (select all files)"},
{KeyBindings: []string{"V"}, Description: "Start selecting by moving"},
{KeyBindings: []string{"D"}, Description: "Deselect all, press again to un-yank"},
{KeyBindings: []string{"F5"}, Description: "Refresh files, sync screen"},
{KeyBindings: []string{"0-9"}, Description: "Go to a configured bookmark"},
}
func (helpScreen *HelpScreen) Draw(screen tcell.Screen) {
if !helpScreen.visible {
return
}
x, y, w, h := helpScreen.GetInnerRect()
helpScreen.Box.SetRect(x, y+1, w, h-2)
helpScreen.Box.DrawForSubclass(screen, helpScreen)
tview.Print(screen, "[::r] fen "+version+" help menu [::-]", x, y+1, w, tview.AlignCenter, tcell.ColorDefault)
longestDescriptionLength := 0
for _, e := range helpScreenControlsList {
if len(e.Description) > longestDescriptionLength {
longestDescriptionLength = len(e.Description)
}
}
controlsYOffset := h/2 - len(helpScreenControlsList)/2
if controlsYOffset < 1 {
controlsYOffset = 1
}
controlsYOffset++
stat, err := os.Lstat(helpScreen.fen.sel)
if err != nil {
return
}
username, groupname, err := FileUserAndGroupName(stat)
topUser, _ := user.Current()
topUsernameColor := "[lime::b]"
if topUser.Uid == "0" {
topUsernameColor = "[red::b]"
}
hostname := ""
if helpScreen.fen.config.ShowHostname && runtime.GOOS != "windows" {
hostname, _ = os.Hostname()
hostname += " " // So the length includes the preceding '@' symbol from the topbar
}
tview.Print(screen, topUsernameColor+"|[-:-:-:-]", x, y+1, w, tview.AlignLeft, tcell.ColorDefault)
tview.Print(screen, "[blue::b]|", x+len(topUser.Username)+1+len(hostname), y+1, w, tview.AlignLeft, tcell.ColorDefault)
tview.Print(screen, topUsernameColor+"|[-:-:-:-]", x, y+2, w, tview.AlignLeft, tcell.ColorDefault)
tview.Print(screen, "[blue::b]Path", x+len(topUser.Username)+1+len(hostname), y+2, w, tview.AlignLeft, tcell.ColorDefault)
tview.Print(screen, topUsernameColor+"User[-:-:-:-]", x, y+3, w, tview.AlignLeft, tcell.ColorDefault)
// There is no User:Group shown on Windows, so only describe the file permissions
if err != nil {
tview.Print(screen, "[teal:]File permissions", x, h-3, w, tview.AlignLeft, tcell.ColorDefault)
tview.Print(screen, "[teal:]|", x, h-2, w, tview.AlignLeft, tcell.ColorDefault)
} else {
tview.Print(screen, "[teal:]File permissions", x, h-4, w, tview.AlignLeft, tcell.ColorDefault)
tview.Print(screen, "[teal:]|[default:]", x, h-3, w, tview.AlignLeft, tcell.ColorDefault)
tview.Print(screen, UsernameColor(username)+"User:[-:-:-:-]"+GroupnameColor(groupname)+"Group", x+10, h-3, w, tview.AlignLeft, tcell.ColorDefault)
tview.Print(screen, UsernameColor(username)+"|[-:-:-:-]", x+10, h-2, w, tview.AlignLeft, tcell.ColorDefault)
}
tview.Print(screen, "[teal:]|[default:]", x, h-2, w, tview.AlignLeft, tcell.ColorDefault)
for dY, e := range helpScreenControlsList {
xPos := x + w/2 - (longestDescriptionLength+15)/2
if xPos < len("| User:Group")+1 {
xPos = len("| User:Group") + 1
}
yPos := y + dY + controlsYOffset + helpScreen.scrollIndex
if yPos < 2 {
continue
}
if yPos >= h-2 {
tview.Print(screen, "▼ Press arrow keys/hjkl to scroll", 0, yPos, w, tview.AlignCenter, tcell.ColorDefault)
break
}
keybindingsStrLengthWithoutStyleTags := 0
var keyBindingsStrBuilder strings.Builder
for i, keyBinding := range e.KeyBindings {
keyBindingsStrBuilder.WriteString("[blue:]" + keyBinding + "[default:]")
keybindingsStrLengthWithoutStyleTags += len(keyBinding)
if i < len(e.KeyBindings)-1 {
keyBindingsStrBuilder.WriteString(" or ")
keybindingsStrLengthWithoutStyleTags += len(" or ")
}
}
tview.Print(screen, " "+keyBindingsStrBuilder.String()+strings.Repeat(" ", 15-keybindingsStrLengthWithoutStyleTags), xPos-1, yPos, w, tview.AlignLeft, tcell.ColorDefault)
tview.Print(screen, e.Description, xPos+15, yPos, w, tview.AlignLeft, tcell.ColorDefault)
}
// After the controls list so the leading space of " Available disk space" appears above
tview.Print(screen, " Available disk space", x, h-3, w, tview.AlignRight, tcell.ColorDefault)
tview.Print(screen, "|", x, h-2, w, tview.AlignRight, tcell.ColorDefault)
}
func (helpScreen *HelpScreen) ScrollDown() {
helpScreen.scrollIndex--
if helpScreen.scrollIndex <= -len(helpScreenControlsList)+5 {
helpScreen.scrollIndex = -len(helpScreenControlsList) + 5 + 1
}
}
func (helpScreen *HelpScreen) ScrollUp() {
helpScreen.scrollIndex = min(0, helpScreen.scrollIndex+1)
}