2022-03-05 01:55:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
2022-07-13 20:02:09 +00:00
|
|
|
"path/filepath"
|
2022-03-05 01:55:37 +00:00
|
|
|
"strings"
|
2022-07-13 20:02:09 +00:00
|
|
|
|
|
|
|
rt "github.com/arnodel/golua/runtime"
|
2022-03-05 01:55:37 +00:00
|
|
|
)
|
|
|
|
|
2022-07-13 20:02:09 +00:00
|
|
|
type luaHistory struct {}
|
|
|
|
|
|
|
|
func (h *luaHistory) Write(line string) (int, error) {
|
|
|
|
histWrite := hshMod.Get(rt.StringValue("history")).AsTable().Get(rt.StringValue("add"))
|
|
|
|
ln, err := rt.Call1(l.MainThread(), histWrite, rt.StringValue(line))
|
|
|
|
|
|
|
|
var num int64
|
|
|
|
if ln.Type() == rt.IntType {
|
|
|
|
num = ln.AsInt()
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(num), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *luaHistory) GetLine(idx int) (string, error) {
|
|
|
|
histGet := hshMod.Get(rt.StringValue("history")).AsTable().Get(rt.StringValue("get"))
|
|
|
|
lcmd, err := rt.Call1(l.MainThread(), histGet, rt.IntValue(int64(idx)))
|
|
|
|
|
|
|
|
var cmd string
|
|
|
|
if lcmd.Type() == rt.StringType {
|
|
|
|
cmd = lcmd.AsString()
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *luaHistory) Len() int {
|
|
|
|
histSize := hshMod.Get(rt.StringValue("history")).AsTable().Get(rt.StringValue("size"))
|
|
|
|
ln, _ := rt.Call1(l.MainThread(), histSize)
|
|
|
|
|
|
|
|
var num int64
|
|
|
|
if ln.Type() == rt.IntType {
|
|
|
|
num = ln.AsInt()
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(num)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *luaHistory) Dump() interface{} {
|
|
|
|
// hilbish.history interface already has all function, this isnt used in readline
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-05 01:55:37 +00:00
|
|
|
type fileHistory struct {
|
|
|
|
items []string
|
|
|
|
f *os.File
|
|
|
|
}
|
|
|
|
|
2022-07-13 20:02:09 +00:00
|
|
|
func newFileHistory(path string) *fileHistory {
|
|
|
|
dir := filepath.Dir(path)
|
|
|
|
|
|
|
|
err := os.MkdirAll(dir, 0755)
|
2022-03-13 01:50:57 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-07-13 20:02:09 +00:00
|
|
|
data, err := os.ReadFile(path)
|
2022-03-05 01:55:37 +00:00
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, fs.ErrNotExist) {
|
2022-03-13 01:43:02 +00:00
|
|
|
panic(err)
|
2022-03-05 01:55:37 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-05 19:23:17 +00:00
|
|
|
|
2022-03-05 12:28:34 +00:00
|
|
|
lines := strings.Split(string(data), "\n")
|
2022-11-30 17:20:00 +00:00
|
|
|
itms := make([]string, len(lines) - 1)
|
2022-03-05 12:28:34 +00:00
|
|
|
for i, l := range lines {
|
|
|
|
if i == len(lines) - 1 {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-30 17:20:00 +00:00
|
|
|
itms[i] = l
|
2022-03-05 01:55:37 +00:00
|
|
|
}
|
2022-07-13 20:02:09 +00:00
|
|
|
f, err := os.OpenFile(path, os.O_APPEND | os.O_WRONLY | os.O_CREATE, 0755)
|
2022-03-05 01:55:37 +00:00
|
|
|
if err != nil {
|
2022-03-13 01:43:02 +00:00
|
|
|
panic(err)
|
2022-03-05 01:55:37 +00:00
|
|
|
}
|
2022-03-05 19:23:17 +00:00
|
|
|
|
2022-03-05 01:55:37 +00:00
|
|
|
fh := &fileHistory{
|
|
|
|
items: itms,
|
|
|
|
f: f,
|
|
|
|
}
|
2022-03-05 19:23:17 +00:00
|
|
|
|
2022-03-13 01:43:02 +00:00
|
|
|
return fh
|
2022-03-05 01:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *fileHistory) Write(line string) (int, error) {
|
2022-03-05 02:04:44 +00:00
|
|
|
if line == "" {
|
|
|
|
return len(h.items), nil
|
|
|
|
}
|
|
|
|
|
2022-03-05 01:55:37 +00:00
|
|
|
_, err := h.f.WriteString(line + "\n")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
h.f.Sync()
|
2022-03-05 19:23:17 +00:00
|
|
|
|
2022-03-05 01:55:37 +00:00
|
|
|
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{} {
|
2022-03-05 19:46:38 +00:00
|
|
|
return h.items
|
2022-03-05 01:55:37 +00:00
|
|
|
}
|
2022-03-22 23:01:29 +00:00
|
|
|
|
|
|
|
func (h *fileHistory) clear() {
|
|
|
|
h.items = []string{}
|
|
|
|
h.f.Truncate(0)
|
|
|
|
h.f.Sync()
|
|
|
|
}
|