Compare commits

...

5 Commits

Author SHA1 Message Date
TorchedSammy 5175367b35
fix: handle errors in goro and timeout callbacks 2022-03-04 22:21:34 -04:00
TorchedSammy 83ca8066e6
chore: remove unused prompt var 2022-03-04 22:21:14 -04:00
TorchedSammy 92f279e5cf
fix: prevent writing empty line to history 2022-03-04 22:04:44 -04:00
TorchedSammy f26ef935dd
feat: add file history (closes #97) 2022-03-04 21:55:37 -04:00
TorchedSammy 6b9bbb615d
fix: handle prompt properly and remove global 2022-03-04 21:54:47 -04:00
5 changed files with 92 additions and 16 deletions

18
api.go
View File

@ -174,10 +174,8 @@ These will be formatted and replaced with the appropriate values.
--- @param str string
*/
func hlprompt(L *lua.LState) int {
prompt = L.CheckString(1)
if lr != nil {
lr.SetPrompt(fmtPrompt())
}
prompt := L.CheckString(1)
lr.SetPrompt(fmtPrompt(prompt))
return 0
}
@ -276,11 +274,13 @@ func hlgoro(L *lua.LState) int {
// call fn
go func() {
L.CallByParam(lua.P{
if err := L.CallByParam(lua.P{
Fn: fn,
NRet: 0,
Protect: true,
}, args...)
}, args...); err != nil {
fmt.Fprintln(os.Stderr, "Error in goro function:\n\n", err)
}
}()
return 0
@ -297,11 +297,13 @@ func hltimeout(L *lua.LState) int {
timeout := time.Duration(ms) * time.Millisecond
time.Sleep(timeout)
L.CallByParam(lua.P{
if err := L.CallByParam(lua.P{
Fn: cb,
NRet: 0,
Protect: true,
})
}); err != nil {
fmt.Fprintln(os.Stderr, "Error in goro function:\n\n", err)
}
return 0
}

71
history.go 100644
View File

@ -0,0 +1,71 @@
package main
import (
"errors"
"io/fs"
"os"
"strings"
)
type fileHistory struct {
items []string
f *os.File
}
func newFileHistory() (*fileHistory, error) {
data, err := os.ReadFile(defaultHistPath)
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
return nil, err
}
}
var itms []string
for _, l := range strings.Split(string(data), "\n") {
itms = append(itms, l)
}
f, err := os.OpenFile(defaultHistPath, os.O_RDWR | os.O_CREATE, 0755)
if err != nil {
return nil, err
}
fh := &fileHistory{
items: itms,
f: f,
}
return fh, nil
}
func (h *fileHistory) Write(line string) (int, error) {
if line == "" {
return len(h.items), nil
}
_, err := h.f.WriteString(line + "\n")
if err != nil {
return 0, err
}
h.f.Sync()
h.items = append(h.items, line)
return len(h.items), nil
}
func (h *fileHistory) GetLine(idx int) (string, error) {
if len(h.items) == 0 {
return "", nil
}
if idx == -1 { // this should be fixed readline side
return "", nil
}
return h.items[idx], nil
}
func (h *fileHistory) Len() int {
return len(h.items)
}
func (h *fileHistory) Dump() interface{} {
return nil
}

View File

@ -114,6 +114,7 @@ func main() {
go handleSignals()
luaInit()
lr = newLineReader("")
// If user's config doesn't exixt,
if _, err := os.Stat(defaultConfPath); os.IsNotExist(err) && *configflag == defaultConfPath {
// Read default from current directory
@ -162,12 +163,10 @@ func main() {
os.Exit(0)
}
lr = newLineReader("")
input:
for interactive {
running = false
lr.SetPrompt(fmtPrompt())
input, err := lr.Read()
if err == io.EOF {
@ -225,7 +224,7 @@ func continuePrompt(prev string) (string, error) {
}
// This semi cursed function formats our prompt (obviously)
func fmtPrompt() string {
func fmtPrompt(prompt string) string {
host, _ := os.Hostname()
cwd, _ := os.Getwd()

5
rl.go
View File

@ -18,6 +18,11 @@ type lineReader struct {
// other gophers might hate this naming but this is local, shut up
func newLineReader(prompt string) *lineReader {
rl := readline.NewInstance()
fileHist, err := newFileHistory()
if err != nil {
panic(err)
}
rl.SetHistoryCtrlR("file", fileHist)
rl.ShowVimMode = false
rl.ViModeCallback = func(mode readline.ViMode) {
modeStr := ""

View File

@ -7,7 +7,6 @@ var (
defaultHistDir = ""
commonRequirePaths = "';./libs/?/init.lua;./?/init.lua;./?/?.lua'"
prompt string // Prompt will always get changed anyway
multilinePrompt = "> "
)