-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_line.go
More file actions
140 lines (123 loc) · 3.44 KB
/
Copy pathcommand_line.go
File metadata and controls
140 lines (123 loc) · 3.44 KB
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
package main
import (
"github.com/mattn/go-runewidth"
"github.com/unxed/vtinput"
"github.com/unxed/vtui"
)
// CommandLine is a simplified Edit control used for shell input.
type CommandLine struct {
vtui.ScreenObject
Edit *vtui.Edit
Prompt string
RichPrompt []vtui.CharInfo
}
func NewCommandLine(prompt string) *CommandLine {
cl := &CommandLine{
Prompt: prompt,
Edit: vtui.NewEdit(0, 0, 10, ""),
}
cl.Edit.DeduplicateHistory = false
cl.Edit.HistoryLimit = 100
cl.Edit.ColorTextIdx = ColCommandLineText
cl.Edit.ColorUnchangedIdx = ColCommandLineText
cl.Edit.ColorSelectedIdx = ColCommandLineSelectedText
cl.Edit.SetCanFocus(true)
cl.SetFocus(true) // Ensure cursor is active from the start
return cl
}
func (cl *CommandLine) SetPosition(x1, y1, x2, y2 int) {
cl.ScreenObject.SetPosition(x1, y1, x2, y2)
promptLen := 0
if len(cl.RichPrompt) > 0 {
for _, c := range cl.RichPrompt {
if c.Char != vtui.WideCharFiller {
promptLen++
}
}
} else {
promptLen = runewidth.StringWidth(cl.Prompt)
}
cl.Edit.SetPosition(x1+promptLen, y1, x2, y2)
}
func (cl *CommandLine) SetFocus(f bool) {
cl.ScreenObject.SetFocus(f)
cl.Edit.SetFocus(f)
}
func (cl *CommandLine) SetPrompt(prompt string) {
cl.RichPrompt = nil
if cl.Prompt == prompt {
return
}
cl.Prompt = prompt
// Trigger reposition of Edit control
cl.SetPosition(cl.X1, cl.Y1, cl.X2, cl.Y2)
}
func (cl *CommandLine) SetRichPrompt(prompt []vtui.CharInfo) {
cl.RichPrompt = prompt
cl.Prompt = ""
cl.SetPosition(cl.X1, cl.Y1, cl.X2, cl.Y2)
}
func (cl *CommandLine) Show(scr *vtui.ScreenBuf) {
cl.ScreenObject.Show(scr)
cl.DisplayObject(scr)
}
func (cl *CommandLine) DisplayObject(scr *vtui.ScreenBuf) {
if !cl.IsVisible() {
return
}
// 1. Draw Prompt
if len(cl.RichPrompt) > 0 {
scr.Write(cl.X1, cl.Y1, cl.RichPrompt)
} else if cl.Prompt != "" {
scr.Write(cl.X1, cl.Y1, vtui.StringToCharInfo(cl.Prompt, vtui.Palette[ColCommandLinePrompt]))
}
// 2. Draw Edit (input field)
cl.Edit.Show(scr)
}
func (cl *CommandLine) ProcessKey(e *vtinput.InputEvent) bool {
handled := cl.Edit.ProcessKey(e)
if handled && cl.Edit.HistoryPos != -1 {
// If a key was handled by the edit control, it means the text was modified.
// We should exit history browsing mode.
// We exclude simple cursor movements from this logic.
isNav := false
switch e.VirtualKeyCode {
case vtinput.VK_LEFT, vtinput.VK_RIGHT, vtinput.VK_HOME, vtinput.VK_END, vtinput.VK_E, vtinput.VK_X:
isNav = true
}
if !isNav {
cl.Edit.HistoryPos = -1
}
}
// AutoComplete logic:
if AppConfig.CommandLineAutoComplete && handled && cl.Edit.HistoryPos == -1 && !cl.IsEmpty() {
isChar := e.Char != 0
isDel := e.VirtualKeyCode == vtinput.VK_BACK || e.VirtualKeyCode == vtinput.VK_DELETE
if isChar || isDel {
top := vtui.FrameManager.GetTopFrame()
_, isAc := top.(*vtui.AutoCompleteMenu)
if !isAc {
ac := vtui.NewAutoCompleteMenu(cl.Edit)
if ac.HasMatches() {
vtui.FrameManager.Push(ac)
}
}
}
}
return handled
}
func (cl *CommandLine) ProcessMouse(e *vtinput.InputEvent) bool {
return cl.Edit.ProcessMouse(e)
}
// Clear empties the command line text.
func (cl *CommandLine) Clear() {
cl.Edit.SetText("")
}
// IsEmpty returns true if there is no text in the command line.
func (cl *CommandLine) IsEmpty() bool {
return cl.Edit.GetText() == ""
}
// InsertString adds text to the command line.
func (cl *CommandLine) InsertString(text string) {
cl.Edit.InsertString(text)
}