mirror of https://github.com/Hilbis/Hilbish
Compare commits
5 Commits
237bc19f68
...
5175367b35
Author | SHA1 | Date |
---|---|---|
TorchedSammy | 5175367b35 | |
TorchedSammy | 83ca8066e6 | |
TorchedSammy | 92f279e5cf | |
TorchedSammy | f26ef935dd | |
TorchedSammy | 6b9bbb615d |
26
api.go
26
api.go
|
@ -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{
|
||||
Fn: fn,
|
||||
NRet: 0,
|
||||
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{
|
||||
Fn: cb,
|
||||
NRet: 0,
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
5
main.go
5
main.go
|
@ -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
5
rl.go
|
@ -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 := ""
|
||||
|
|
Loading…
Reference in New Issue