286 lines
6.8 KiB
Go
286 lines
6.8 KiB
Go
/*
|
|
Copyright © Brian Buller <brian@bullercodeworks.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package widgets
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
h "git.bullercodeworks.com/brian/tcell-widgets/helpers"
|
|
"github.com/gdamore/tcell"
|
|
)
|
|
|
|
type Cli struct {
|
|
id string
|
|
style tcell.Style
|
|
|
|
x, y int
|
|
w, h int
|
|
active bool
|
|
visible bool
|
|
|
|
title string
|
|
rawLog []string
|
|
log []string
|
|
logPosition int
|
|
|
|
value string
|
|
cursor int
|
|
|
|
history []string
|
|
historyPosition int
|
|
|
|
commands []string
|
|
commandMap map[string]cliCommand
|
|
commandGuessMap map[string]func(args ...string) string
|
|
}
|
|
|
|
func NewCli(id string, s tcell.Style) *Cli {
|
|
ret := &Cli{}
|
|
ret.Init(id, s)
|
|
return ret
|
|
}
|
|
|
|
func (w *Cli) Init(id string, s tcell.Style) {
|
|
w.id, w.style = id, s
|
|
w.visible = true
|
|
}
|
|
|
|
func (w *Cli) Id() string { return w.id }
|
|
func (w *Cli) HandleResize(ev *tcell.EventResize) {}
|
|
func (w *Cli) HandleKey(ev *tcell.EventKey) bool {
|
|
if !w.active {
|
|
return false
|
|
}
|
|
if h.IsKey(*ev, tcell.KeyEsc) {
|
|
w.SetActive(false)
|
|
return true
|
|
}
|
|
if h.IsBS(*ev) {
|
|
if w.cursor > 0 {
|
|
w.cursor--
|
|
}
|
|
if w.cursor < len(w.value) {
|
|
w.value = w.value[:w.cursor] + w.value[w.cursor+1:]
|
|
}
|
|
}
|
|
if done := h.HandleKeys(*ev, map[tcell.Key]func() bool{
|
|
tcell.KeyEsc: func() bool {
|
|
w.SetActive(false)
|
|
return true
|
|
},
|
|
tcell.KeyUp: func() bool {
|
|
if w.historyPosition < len(w.history)-1 {
|
|
w.historyPosition++
|
|
w.value = w.history[w.historyPosition]
|
|
w.cursor = len(w.value)
|
|
return true
|
|
}
|
|
return false
|
|
},
|
|
tcell.KeyDown: func() bool {
|
|
if w.historyPosition > -1 {
|
|
w.historyPosition--
|
|
if w.historyPosition == -1 {
|
|
w.value = ""
|
|
} else {
|
|
w.value = w.history[w.historyPosition]
|
|
}
|
|
w.cursor = len(w.value)
|
|
return true
|
|
}
|
|
return false
|
|
},
|
|
tcell.KeyLeft: func() bool {
|
|
if w.cursor > 0 {
|
|
w.cursor--
|
|
return true
|
|
}
|
|
return false
|
|
},
|
|
tcell.KeyRight: func() bool {
|
|
if w.cursor < len(w.value) {
|
|
w.cursor++
|
|
return true
|
|
}
|
|
return false
|
|
},
|
|
tcell.KeyCtrlU: func() bool {
|
|
w.value = w.value[w.cursor:]
|
|
w.cursor = 0
|
|
return true
|
|
},
|
|
tcell.KeyTab: func() bool {
|
|
// Auto-complete
|
|
guess := w.findBestGuess()
|
|
if guess != "" {
|
|
w.value = guess
|
|
w.cursor = len(w.value)
|
|
return true
|
|
}
|
|
return false
|
|
},
|
|
tcell.KeyEnter: func() bool {
|
|
cmds := strings.Split(w.value, " ")
|
|
if v, ok := w.commandMap[cmds[0]]; ok {
|
|
w.history = append(w.history, w.value)
|
|
w.historyPosition = -1
|
|
w.value = ""
|
|
w.cursor = 0
|
|
if len(cmds) > 1 {
|
|
return v(cmds[1:]...)
|
|
} else {
|
|
return v()
|
|
}
|
|
}
|
|
return true
|
|
},
|
|
}); done {
|
|
return done
|
|
}
|
|
var ch string
|
|
if h.KeyIsDisplayable(*ev) {
|
|
ch = string(ev.Rune())
|
|
if w.cursor < len(w.value) {
|
|
strPt1 := w.value[:w.cursor]
|
|
strPt2 := w.value[w.cursor:]
|
|
w.value = fmt.Sprintf("%s%s%s", strPt1, ch, strPt2)
|
|
} else {
|
|
w.value = fmt.Sprintf("%s%s", w.value, ch)
|
|
}
|
|
w.cursor++
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
func (w *Cli) HandleTime(ev *tcell.EventTime) {}
|
|
|
|
func (w *Cli) Draw(screen tcell.Screen) {
|
|
if !w.visible {
|
|
return
|
|
}
|
|
dStyle := w.style
|
|
if !w.active {
|
|
dStyle = dStyle.Dim(true)
|
|
}
|
|
if w.title != "" {
|
|
h.TitledBorderFilled(w.x, w.y, w.x+w.w-1, w.y+w.h, w.title, h.BRD_SIMPLE, dStyle, screen)
|
|
} else {
|
|
h.BorderFilled(w.x, w.y, w.x+w.w-1, w.y+w.h, h.BRD_SIMPLE, dStyle, screen)
|
|
}
|
|
|
|
x, y := w.x+1, w.y+1+w.h-3
|
|
for i := 0; i < w.h-2; i++ {
|
|
if len(w.log) > i {
|
|
line := w.log[len(w.log)-i-1]
|
|
if len(line) > w.w-2 {
|
|
line = line[:w.w-2]
|
|
}
|
|
h.DrawText(x, y, h.PadR(line, w.w), dStyle, screen)
|
|
y--
|
|
}
|
|
}
|
|
y = w.y + w.h - 1
|
|
cursor := " "
|
|
pre := "> "
|
|
var post string
|
|
if len(w.value) > 0 {
|
|
pre = fmt.Sprintf("%s%s", pre, w.value[:w.cursor])
|
|
if w.cursor < len(w.value) {
|
|
cursor = string(w.value[w.cursor])
|
|
post = w.value[w.cursor+1:]
|
|
}
|
|
}
|
|
h.DrawText(x, y, pre, dStyle, screen)
|
|
x += len(pre)
|
|
h.DrawText(x, y, cursor, dStyle.Reverse(w.active), screen)
|
|
x += 1
|
|
h.DrawText(x, y, h.PadR(post, w.w-x), dStyle, screen)
|
|
// x += len(post) - 1
|
|
}
|
|
|
|
func (w *Cli) Active() bool { return w.active }
|
|
func (w *Cli) SetActive(a bool) { w.active = a }
|
|
func (w *Cli) Visible() bool { return w.visible }
|
|
func (w *Cli) SetVisible(a bool) { w.visible = a }
|
|
func (w *Cli) Focusable() bool { return true }
|
|
func (w *Cli) SetX(x int) { w.x = x }
|
|
func (w *Cli) SetY(y int) { w.y = y }
|
|
func (w *Cli) GetX() int { return w.x }
|
|
func (w *Cli) GetY() int { return w.y }
|
|
func (w *Cli) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
|
func (w *Cli) GetW() int { return w.w }
|
|
func (w *Cli) GetH() int { return w.h }
|
|
func (w *Cli) SetW(wd int) { w.w = wd }
|
|
func (w *Cli) SetH(h int) { w.h = h }
|
|
func (w *Cli) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
|
|
func (w *Cli) WantW() int { return w.w }
|
|
func (w *Cli) WantH() int { return w.h }
|
|
|
|
func (w *Cli) SetTitle(ttl string) { w.title = ttl }
|
|
func (w *Cli) Title() string { return w.title }
|
|
func (w *Cli) SetValue(val string) {
|
|
w.value = val
|
|
w.cursor = len(val)
|
|
}
|
|
|
|
func (w *Cli) AddComamnd(cmd string, do cliCommand) {
|
|
w.commands = append(w.commands, cmd)
|
|
sort.Strings(w.commands)
|
|
w.commandMap[cmd] = do
|
|
}
|
|
|
|
func (w *Cli) RemoveCommand(cmd string) {
|
|
var idx int
|
|
for idx = range w.commands {
|
|
if w.commands[idx] == cmd {
|
|
break
|
|
}
|
|
}
|
|
w.commands = append(w.commands[:idx], w.commands[idx+1:]...)
|
|
delete(w.commandMap, cmd)
|
|
}
|
|
|
|
func (w *Cli) findBestGuess() string {
|
|
if w.value == "" {
|
|
return ""
|
|
}
|
|
for _, v := range w.commands {
|
|
if strings.HasPrefix(v, w.value) {
|
|
return v
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (w *Cli) DoLog(txt string) {
|
|
w.rawLog = append(w.rawLog, txt)
|
|
w.log = append(w.log, h.WrapStringToSlice(txt, w.w-2)...)
|
|
}
|
|
|
|
type cliCommand func(args ...string) bool
|
|
|
|
func (c *cliCommand) findBestGuess(args ...string) string {
|
|
return ""
|
|
}
|