2021-10-16 15:31:01 +00:00
|
|
|
package main
|
|
|
|
|
2021-11-22 16:24:31 +00:00
|
|
|
import (
|
2022-02-27 23:21:21 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-11-22 16:24:31 +00:00
|
|
|
"strings"
|
|
|
|
|
2024-07-21 15:37:08 +00:00
|
|
|
"hilbish/moonlight"
|
2022-04-04 10:40:02 +00:00
|
|
|
"hilbish/util"
|
|
|
|
|
|
|
|
rt "github.com/arnodel/golua/runtime"
|
2023-07-10 04:06:29 +00:00
|
|
|
"github.com/maxlandon/readline"
|
|
|
|
"github.com/sahilm/fuzzy"
|
2021-11-22 16:24:31 +00:00
|
|
|
)
|
2021-05-17 23:03:56 +00:00
|
|
|
|
2021-12-06 21:21:31 +00:00
|
|
|
type lineReader struct {
|
2022-02-27 23:21:21 +00:00
|
|
|
rl *readline.Instance
|
2022-07-13 20:02:09 +00:00
|
|
|
fileHist *fileHistory
|
2021-05-17 23:03:56 +00:00
|
|
|
}
|
2022-04-04 10:40:02 +00:00
|
|
|
var hinter *rt.Closure
|
|
|
|
var highlighter *rt.Closure
|
2021-05-17 23:03:56 +00:00
|
|
|
|
2022-03-06 18:38:27 +00:00
|
|
|
func newLineReader(prompt string, noHist bool) *lineReader {
|
2022-02-27 23:21:21 +00:00
|
|
|
rl := readline.NewInstance()
|
2022-07-13 20:02:09 +00:00
|
|
|
lr := &lineReader{
|
|
|
|
rl: rl,
|
|
|
|
}
|
|
|
|
|
2023-07-10 04:06:29 +00:00
|
|
|
regexSearcher := rl.Searcher
|
|
|
|
rl.Searcher = func(needle string, haystack []string) []string {
|
2024-07-19 20:54:15 +00:00
|
|
|
fz, _ := l.DoString("return hilbish.opts.fuzzy")
|
2023-07-10 04:06:29 +00:00
|
|
|
fuzz, ok := fz.TryBool()
|
|
|
|
if !fuzz || !ok {
|
|
|
|
return regexSearcher(needle, haystack)
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := fuzzy.Find(needle, haystack)
|
|
|
|
suggs := make([]string, 0)
|
|
|
|
|
|
|
|
for _, match := range matches {
|
|
|
|
suggs = append(suggs, match.Str)
|
|
|
|
}
|
|
|
|
|
|
|
|
return suggs
|
|
|
|
}
|
|
|
|
|
2022-03-06 18:38:27 +00:00
|
|
|
// we don't mind hilbish.read rl instances having completion,
|
|
|
|
// but it cant have shared history
|
|
|
|
if !noHist {
|
2022-07-13 20:02:09 +00:00
|
|
|
lr.fileHist = newFileHistory(defaultHistPath)
|
|
|
|
rl.SetHistoryCtrlR("History", &luaHistory{})
|
2022-03-06 18:38:27 +00:00
|
|
|
rl.HistoryAutoWrite = false
|
2022-03-05 01:55:37 +00:00
|
|
|
}
|
2022-03-02 02:00:46 +00:00
|
|
|
rl.ShowVimMode = false
|
|
|
|
rl.ViModeCallback = func(mode readline.ViMode) {
|
|
|
|
modeStr := ""
|
|
|
|
switch mode {
|
|
|
|
case readline.VimKeys: modeStr = "normal"
|
|
|
|
case readline.VimInsert: modeStr = "insert"
|
|
|
|
case readline.VimDelete: modeStr = "delete"
|
2022-04-21 18:01:59 +00:00
|
|
|
case readline.VimReplaceOnce, readline.VimReplaceMany: modeStr = "replace"
|
2022-03-02 02:00:46 +00:00
|
|
|
}
|
|
|
|
setVimMode(modeStr)
|
|
|
|
}
|
2022-03-13 20:44:11 +00:00
|
|
|
rl.ViActionCallback = func(action readline.ViAction, args []string) {
|
|
|
|
actionStr := ""
|
|
|
|
switch action {
|
|
|
|
case readline.VimActionPaste: actionStr = "paste"
|
|
|
|
case readline.VimActionYank: actionStr = "yank"
|
|
|
|
}
|
2022-08-17 22:01:32 +00:00
|
|
|
hooks.Emit("hilbish.vimAction", actionStr, args)
|
2022-03-13 20:44:11 +00:00
|
|
|
}
|
2022-03-26 21:34:09 +00:00
|
|
|
rl.HintText = func(line []rune, pos int) []rune {
|
2024-07-21 15:37:08 +00:00
|
|
|
hinter := hshMod.Get(moonlight.StringValue("hinter"))
|
|
|
|
retVal, err := l.Call1(hinter, moonlight.StringValue(string(line)), moonlight.IntValue(int64(pos)))
|
2022-03-26 21:34:09 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return []rune{}
|
|
|
|
}
|
|
|
|
|
|
|
|
hintText := ""
|
2022-04-04 10:40:02 +00:00
|
|
|
if luaStr, ok := retVal.TryString(); ok {
|
|
|
|
hintText = luaStr
|
2022-03-26 21:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return []rune(hintText)
|
|
|
|
}
|
2022-03-26 22:25:19 +00:00
|
|
|
rl.SyntaxHighlighter = func(line []rune) string {
|
2024-07-21 15:37:08 +00:00
|
|
|
highlighter := hshMod.Get(moonlight.StringValue("highlighter"))
|
|
|
|
retVal, err := l.Call1(highlighter, moonlight.StringValue(string(line)))
|
2022-03-26 22:25:19 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return string(line)
|
|
|
|
}
|
|
|
|
|
|
|
|
highlighted := ""
|
2022-04-04 10:40:02 +00:00
|
|
|
if luaStr, ok := retVal.TryString(); ok {
|
|
|
|
highlighted = luaStr
|
2022-03-26 22:25:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return highlighted
|
|
|
|
}
|
2024-07-21 15:37:08 +00:00
|
|
|
setupTabCompleter(rl)
|
2021-05-17 23:03:56 +00:00
|
|
|
|
2022-07-13 20:02:09 +00:00
|
|
|
return lr
|
2021-05-17 23:03:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:21:31 +00:00
|
|
|
func (lr *lineReader) Read() (string, error) {
|
2022-08-17 22:01:32 +00:00
|
|
|
hooks.Emit("command.precmd", nil)
|
2022-02-27 23:21:21 +00:00
|
|
|
s, err := lr.rl.Readline()
|
|
|
|
// this is so dumb
|
|
|
|
if err == readline.EOF {
|
2022-03-02 02:00:46 +00:00
|
|
|
fmt.Println("")
|
2022-02-27 23:21:21 +00:00
|
|
|
return "", io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, err // might get another error
|
2021-05-17 23:03:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 21:20:41 +00:00
|
|
|
func (lr *lineReader) SetPrompt(p string) {
|
|
|
|
halfPrompt := strings.Split(p, "\n")
|
2022-02-27 23:21:21 +00:00
|
|
|
if len(halfPrompt) > 1 {
|
2022-03-05 01:16:45 +00:00
|
|
|
lr.rl.Multiline = true
|
2022-02-27 23:21:21 +00:00
|
|
|
lr.rl.SetPrompt(strings.Join(halfPrompt[:len(halfPrompt) - 1], "\n"))
|
|
|
|
lr.rl.MultilinePrompt = halfPrompt[len(halfPrompt) - 1:][0]
|
|
|
|
} else {
|
2022-03-05 01:16:45 +00:00
|
|
|
lr.rl.Multiline = false
|
2022-03-13 20:42:14 +00:00
|
|
|
lr.rl.MultilinePrompt = ""
|
2022-03-06 21:20:41 +00:00
|
|
|
lr.rl.SetPrompt(p)
|
2022-02-27 23:21:21 +00:00
|
|
|
}
|
2022-03-06 21:20:41 +00:00
|
|
|
if initialized && !running {
|
2022-03-02 02:00:46 +00:00
|
|
|
lr.rl.RefreshPromptInPlace("")
|
|
|
|
}
|
2021-05-17 23:03:56 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 14:13:46 +00:00
|
|
|
func (lr *lineReader) SetRightPrompt(p string) {
|
|
|
|
lr.rl.SetRightPrompt(p)
|
|
|
|
if initialized && !running {
|
|
|
|
lr.rl.RefreshPromptInPlace("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:21:31 +00:00
|
|
|
func (lr *lineReader) AddHistory(cmd string) {
|
2022-07-13 20:02:09 +00:00
|
|
|
lr.fileHist.Write(cmd)
|
2021-05-17 23:03:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:21:31 +00:00
|
|
|
func (lr *lineReader) ClearInput() {
|
2022-02-27 23:21:21 +00:00
|
|
|
return
|
2021-06-09 01:00:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:21:31 +00:00
|
|
|
func (lr *lineReader) Resize() {
|
2022-02-27 23:21:21 +00:00
|
|
|
return
|
2021-11-22 15:41:27 +00:00
|
|
|
}
|
2022-01-27 21:02:21 +00:00
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface history
|
|
|
|
// command history
|
|
|
|
// The history interface deals with command history.
|
|
|
|
// This includes the ability to override functions to change the main
|
|
|
|
// method of saving history.
|
2022-04-04 10:40:02 +00:00
|
|
|
func (lr *lineReader) Loader(rtm *rt.Runtime) *rt.Table {
|
|
|
|
lrLua := map[string]util.LuaExport{
|
2024-07-21 15:37:08 +00:00
|
|
|
/*
|
2022-04-04 10:40:02 +00:00
|
|
|
"add": {lr.luaAddHistory, 1, false},
|
|
|
|
"all": {lr.luaAllHistory, 0, false},
|
|
|
|
"clear": {lr.luaClearHistory, 0, false},
|
|
|
|
"get": {lr.luaGetHistory, 1, false},
|
|
|
|
"size": {lr.luaSize, 0, false},
|
2024-07-21 15:37:08 +00:00
|
|
|
*/
|
2022-01-27 21:02:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
mod := rt.NewTable()
|
|
|
|
util.SetExports(rtm, mod, lrLua)
|
2022-01-27 21:02:21 +00:00
|
|
|
|
|
|
|
return mod
|
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface history
|
|
|
|
// add(cmd)
|
|
|
|
// Adds a command to the history.
|
2023-12-26 03:08:29 +00:00
|
|
|
// #param cmd string
|
2022-04-04 10:40:02 +00:00
|
|
|
func (lr *lineReader) luaAddHistory(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cmd, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-27 21:02:21 +00:00
|
|
|
lr.AddHistory(cmd)
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), nil
|
2022-01-27 21:02:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface history
|
2023-02-07 22:18:03 +00:00
|
|
|
// size() -> number
|
2022-12-15 04:00:54 +00:00
|
|
|
// Returns the amount of commands in the history.
|
2023-12-26 03:08:29 +00:00
|
|
|
// #eturns number
|
2022-04-04 10:40:02 +00:00
|
|
|
func (lr *lineReader) luaSize(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
2022-07-13 20:02:09 +00:00
|
|
|
return c.PushingNext1(t.Runtime, rt.IntValue(int64(lr.fileHist.Len()))), nil
|
2022-01-27 21:02:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface history
|
2023-12-26 03:08:29 +00:00
|
|
|
// get(index)
|
|
|
|
// Retrieves a command from the history based on the `index`.
|
|
|
|
// #param index number
|
2022-04-04 10:40:02 +00:00
|
|
|
func (lr *lineReader) luaGetHistory(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
idx, err := c.IntArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-05 19:57:46 +00:00
|
|
|
|
2022-07-13 20:02:09 +00:00
|
|
|
cmd, _ := lr.fileHist.GetLine(int(idx))
|
2022-04-04 10:40:02 +00:00
|
|
|
|
|
|
|
return c.PushingNext1(t.Runtime, rt.StringValue(cmd)), nil
|
2022-01-27 21:02:21 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 22:18:03 +00:00
|
|
|
// #interface history
|
|
|
|
// all() -> table
|
2023-12-26 03:08:29 +00:00
|
|
|
// Retrieves all history as a table.
|
|
|
|
// #returns table
|
2022-04-04 10:40:02 +00:00
|
|
|
func (lr *lineReader) luaAllHistory(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
tbl := rt.NewTable()
|
2022-07-13 20:02:09 +00:00
|
|
|
size := lr.fileHist.Len()
|
2022-03-05 19:57:46 +00:00
|
|
|
|
|
|
|
for i := 1; i < size; i++ {
|
2022-07-13 20:02:09 +00:00
|
|
|
cmd, _ := lr.fileHist.GetLine(i)
|
2022-04-04 10:40:02 +00:00
|
|
|
tbl.Set(rt.IntValue(int64(i)), rt.StringValue(cmd))
|
2022-03-05 19:57:46 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.PushingNext1(t.Runtime, rt.TableValue(tbl)), nil
|
2022-01-27 21:02:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface history
|
|
|
|
// clear()
|
|
|
|
// Deletes all commands from the history.
|
2022-04-04 10:40:02 +00:00
|
|
|
func (lr *lineReader) luaClearHistory(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
2022-07-13 20:02:09 +00:00
|
|
|
lr.fileHist.clear()
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), nil
|
2022-01-27 21:02:21 +00:00
|
|
|
}
|