-
Notifications
You must be signed in to change notification settings - Fork 6
/
commandline.go
96 lines (79 loc) · 1.94 KB
/
commandline.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
package main
import (
"fmt"
"time"
"github.com/nsf/termbox-go"
)
type CommandLine struct {
// allow user to edit it
input *Editbox
// program to call
cmd string
cmdargs []string
// use space for showing errors too
isActive bool
showingError bool
modelineError string
// cached
fullCmdline string
summarizedCmdline string
}
func NewCommandLine(cmd string) *CommandLine {
input := new(Editbox)
input.fg = termbox.ColorRed
input.bg = termbox.ColorDefault
return &CommandLine{
input: input,
cmd: cmd,
}
}
func (cmd *CommandLine) Update(results ResultArray) {
text := cmd.cmd
cmd.cmdargs = make([]string, 0, len(results))
for _, res := range results {
text = text + " " + res.displayContents
cmd.cmdargs = append(cmd.cmdargs, res.displayContents)
}
cmd.input.MoveCursorToBeginningOfTheLine()
cmd.fullCmdline = text
cmd.summarizedCmdline = fmt.Sprintf("%s <%d files...>", cmd.cmd, len(results))
}
func (cmd *CommandLine) SummarizeCommand(maxlen int) string {
if len(cmd.fullCmdline) > maxlen {
return cmd.summarizedCmdline
} else {
return cmd.fullCmdline
}
}
func (cmd *CommandLine) ShowError(redraw chan bool, err error) {
cmd.showingError = true
cmd.modelineError = "Error: " + err.Error()
clearErrorTimer := time.NewTimer(1 * time.Second)
go func() {
<-clearErrorTimer.C
cmd.showingError = false
redraw <- true
}()
}
func (cmd *CommandLine) SetActive(active bool) {
cmd.isActive = active
if active {
cmd.input.text = []byte(" $FILES")
} else {
cmd.input.text = []byte(cmd.cmd + " $FILES")
}
}
func (cmd *CommandLine) Draw(x, y, w int) {
if cmd.showingError {
tclearcolor(x, y, w, 1, cmd.input.bg)
tbprint(x, y, termbox.ColorRed, cmd.input.bg, cmd.modelineError)
return
}
if cmd.isActive {
cmd.input.Draw(x, y, w)
termbox.SetCursor(x+cmd.input.CursorX(), y)
} else {
tclearcolor(x, y, w, 1, cmd.input.bg)
tbprint(x, y, cmd.input.fg, cmd.input.bg, cmd.SummarizeCommand(w))
}
}