mirror of https://github.com/Hilbis/Hilbish
Compare commits
No commits in common. "14e6ae5a3c3f0fa5c32256e54302ca49b66a7425" and "12828d197abd967d3b8b1074dab3a533ae0666d1" have entirely different histories.
14e6ae5a3c
...
12828d197a
11
Makefile
11
Makefile
|
@ -4,13 +4,10 @@ BINDIR ?= $(PREFIX)/bin
|
|||
LIBDIR ?= $(PREFIX)/share/hilbish
|
||||
|
||||
build:
|
||||
@go build -ldflags "-s -w"
|
||||
@go build
|
||||
|
||||
dev:
|
||||
@go build -ldflags "-s -w -X main.version=$(shell git describe --tags)"
|
||||
|
||||
hilbiline:
|
||||
@go build -ldflags "-s -w -X main.version=$(shell git describe --tags)+hilbiline" -tags hilbiline
|
||||
build-dev:
|
||||
@go build -ldflags "-X main.version=$(git describe --tags)"
|
||||
|
||||
install:
|
||||
@install -v -d "$(DESTDIR)$(BINDIR)/" && install -m 0755 -v hilbish "$(DESTDIR)$(BINDIR)/hilbish"
|
||||
|
@ -31,4 +28,4 @@ clean:
|
|||
|
||||
all: build install
|
||||
|
||||
.PHONY: install uninstall build dev hilbiline clean
|
||||
.PHONY: install uninstall build build-dev clean
|
||||
|
|
14
README.md
14
README.md
|
@ -51,27 +51,15 @@ sudo zypper install readline-devel
|
|||
```
|
||||
|
||||
### Install
|
||||
First, clone Hilbish:
|
||||
```sh
|
||||
git clone --recursive https://github.com/Hilbis/Hilbish
|
||||
cd Hilbish
|
||||
```
|
||||
|
||||
Then build and install:
|
||||
```sh
|
||||
make dev
|
||||
make build
|
||||
sudo make install
|
||||
# Or
|
||||
sudo make all
|
||||
```
|
||||
|
||||
Or, if you want the latest stable release:
|
||||
```
|
||||
git checkout $(git describe --tags `git rev-list --tags --max-count=1`)
|
||||
make build
|
||||
sudo make install
|
||||
```
|
||||
|
||||
Alternatively, if you use Arch Linux, you can compile Hilbish with an **(unofficial)** AUR package:
|
||||
```sh
|
||||
yay -S hilbish
|
||||
|
|
10
go.mod
10
go.mod
|
@ -3,17 +3,13 @@ module hilbish
|
|||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/Hilbis/Hilbiline v0.0.0-20210416123506-f1b20c1c66e4 // indirect
|
||||
github.com/bobappleyard/readline v0.0.0-20150707195538-7e300e02d38e
|
||||
github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9
|
||||
github.com/pborman/getopt v1.1.0
|
||||
github.com/rivo/uniseg v0.2.0 // indirect
|
||||
github.com/sergi/go-diff v1.0.0 // indirect
|
||||
github.com/stretchr/testify v1.4.0 // indirect
|
||||
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
|
||||
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect
|
||||
golang.org/x/term v0.0.0-20210503060354-a79de5458b56
|
||||
golang.org/x/sys v0.0.0-20210415045647-66c3f260301c // indirect
|
||||
golang.org/x/term v0.0.0-20210406210042-72f3dc4e9b72
|
||||
layeh.com/gopher-luar v1.0.8
|
||||
mvdan.cc/sh/v3 v3.3.0
|
||||
mvdan.cc/sh/v3 v3.2.4
|
||||
)
|
||||
|
|
22
go.sum
22
go.sum
|
@ -1,5 +1,3 @@
|
|||
github.com/Hilbis/Hilbiline v0.0.0-20210416123506-f1b20c1c66e4 h1:S9ulYwm5MUp02mEKnbDhVGjVBkdYV7/tmgEO1g3Q+Z8=
|
||||
github.com/Hilbis/Hilbiline v0.0.0-20210416123506-f1b20c1c66e4/go.mod h1:mp9I7lvFoZ3ldAeydCzISakl6VdQYyrR8vVjmnKGqDo=
|
||||
github.com/bobappleyard/readline v0.0.0-20150707195538-7e300e02d38e h1:4G8AYOOwZdDWOiJR6D6JXaFmj5BDS7c5D5PyqsG/+Hg=
|
||||
github.com/bobappleyard/readline v0.0.0-20150707195538-7e300e02d38e/go.mod h1:fmqtV+Wqx0uFYLN1F4VhjZdtT56Dr8c3yA7nALFsw/Q=
|
||||
github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9 h1:xz6Nv3zcwO2Lila35hcb0QloCQsc38Al13RNEzWRpX4=
|
||||
|
@ -7,32 +5,21 @@ github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9/go.mod h1:2w
|
|||
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
||||
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
||||
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw=
|
||||
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
|
||||
github.com/google/renameio v1.0.1-0.20210406141108-81588dbe0453/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mattn/go-runewidth v0.0.12 h1:Y41i/hVW3Pgwr8gV+J23B9YEY0zxjptBuCWEaxmAOow=
|
||||
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||
github.com/pborman/getopt v1.1.0 h1:eJ3aFZroQqq0bWmraivjQNt6Dmm5M0h2JcDW38/Azb0=
|
||||
github.com/pborman/getopt v1.1.0/go.mod h1:FxXoW1Re00sQG/+KIkuSqRL/LwQgSkv7uyac+STFsbk=
|
||||
github.com/pkg/diff v0.0.0-20200914180035-5b29258ca4f7/go.mod h1:zO8QMzTeZd5cpnIkz/Gn6iK0jDfGicM1nynOkkPIl28=
|
||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY=
|
||||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rogpeppe/go-internal v1.6.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
|
||||
github.com/rogpeppe/go-internal v1.7.0/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
|
||||
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
|
@ -46,17 +33,11 @@ golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5h
|
|||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201029080932-201ba4db2418/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210414055047-fe65e336abe0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210415045647-66c3f260301c h1:6L+uOeS3OQt/f4eFHXZcTxeZrGCuz+CLElgEBjbcTA4=
|
||||
golang.org/x/sys v0.0.0-20210415045647-66c3f260301c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210503080704-8803ae5d1324/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E=
|
||||
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20191110171634-ad39bd3f0407/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
||||
golang.org/x/term v0.0.0-20210406210042-72f3dc4e9b72 h1:VqE9gduFZ4dbR7XoL77lHFp0/DyDUBKSXK7CMFkVcV0=
|
||||
golang.org/x/term v0.0.0-20210406210042-72f3dc4e9b72/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210503060354-a79de5458b56 h1:b8jxX3zqjpqb2LklXPzKSGJhzyxCOZSz8ncv8Nv+y7w=
|
||||
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
|
@ -64,8 +45,5 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|||
layeh.com/gopher-luar v1.0.8 h1:Uqws1Z6T0vK6pZ7ehNNurLLSFcz7+E0EOHVM4FNiMQs=
|
||||
layeh.com/gopher-luar v1.0.8/go.mod h1:TPnIVCZ2RJBndm7ohXyaqfhzjlZ+OA2SZR/YwL8tECk=
|
||||
mvdan.cc/editorconfig v0.1.1-0.20200121172147-e40951bde157/go.mod h1:Ge4atmRUYqueGppvJ7JNrtqpqokoJEFxYbP0Z+WeKS8=
|
||||
mvdan.cc/editorconfig v0.2.0/go.mod h1:lvnnD3BNdBYkhq+B4uBuFFKatfp02eB6HixDvEz91C0=
|
||||
mvdan.cc/sh/v3 v3.2.4 h1:+fZaWcXWRjYAvqzEKoDhDM3DkxdDUykU2iw0VMKFe9s=
|
||||
mvdan.cc/sh/v3 v3.2.4/go.mod h1:fPQmabBpREM/XQ9YXSU5ZFZ/Sm+PmKP9/vkFHgYKJEI=
|
||||
mvdan.cc/sh/v3 v3.3.0 h1:ujzElMnry63f4I5sjPFxzo6xia+gwsHZM0yyauuyZ6k=
|
||||
mvdan.cc/sh/v3 v3.3.0/go.mod h1:dh3avhLDhJJ/MJKzbak6FYn+DJKUWk7Fb6Dh5mGdv6Y=
|
||||
|
|
22
main.go
22
main.go
|
@ -12,6 +12,7 @@ import (
|
|||
"hilbish/golibs/bait"
|
||||
|
||||
"github.com/pborman/getopt"
|
||||
"github.com/bobappleyard/readline"
|
||||
"github.com/yuin/gopher-lua"
|
||||
"layeh.com/gopher-luar"
|
||||
"golang.org/x/term"
|
||||
|
@ -21,7 +22,6 @@ import (
|
|||
var (
|
||||
version = "v0.4.0"
|
||||
l *lua.LState
|
||||
lr *LineReader
|
||||
|
||||
prompt string // User's prompt, this will get set when lua side is initialized
|
||||
multilinePrompt = "> "
|
||||
|
@ -113,7 +113,8 @@ func main() {
|
|||
RunLogin()
|
||||
RunConfig(*configflag)
|
||||
|
||||
lr = NewLineReader("")
|
||||
readline.Completer = readline.FilenameCompleter
|
||||
readline.LoadHistory(homedir + "/.hilbish-history")
|
||||
|
||||
if *cmdflag != "" {
|
||||
RunInput(*cmdflag)
|
||||
|
@ -132,8 +133,7 @@ func main() {
|
|||
for interactive {
|
||||
running = false
|
||||
|
||||
lr.SetPrompt(fmtPrompt())
|
||||
input, err := lr.Read()
|
||||
input, err := readline.String(fmtPrompt())
|
||||
|
||||
if err == io.EOF {
|
||||
// Exit if user presses ^D (ctrl + d)
|
||||
|
@ -158,7 +158,6 @@ func main() {
|
|||
}
|
||||
}
|
||||
running = true
|
||||
HandleHistory(input)
|
||||
RunInput(input)
|
||||
|
||||
termwidth, _, err := term.GetSize(0)
|
||||
|
@ -171,8 +170,7 @@ func main() {
|
|||
|
||||
func ContinuePrompt(prev string) (string, error) {
|
||||
hooks.Em.Emit("multiline", nil)
|
||||
lr.SetPrompt(multilinePrompt)
|
||||
cont, err := lr.Read()
|
||||
cont, err := readline.String(multilinePrompt)
|
||||
if err != nil {
|
||||
fmt.Println("")
|
||||
return "", err
|
||||
|
@ -215,14 +213,8 @@ func HandleSignals() {
|
|||
|
||||
for range c {
|
||||
if !running {
|
||||
//readline.ReplaceLine("", 0)
|
||||
//readline.RefreshLine()
|
||||
readline.ReplaceLine("", 0)
|
||||
readline.RefreshLine()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func HandleHistory(cmd string) {
|
||||
lr.AddHistory(cmd)
|
||||
// TODO: load history again (history shared between sessions like this ye)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
-- The preload file initializes everything else for our shell
|
||||
-- Currently it just adds our builtins
|
||||
|
||||
local fs = require 'fs'
|
||||
local commander = require 'commander'
|
||||
local bait = require 'bait'
|
||||
|
||||
-- Builtins
|
||||
commander.register('cd', function (args)
|
||||
bait.throw('cd', args)
|
||||
if #args > 0 then
|
||||
|
@ -86,8 +86,3 @@ function string.split(str, delimiter)
|
|||
return result
|
||||
end
|
||||
|
||||
-- Hook handles
|
||||
bait.catch('command.not-found', function(cmd)
|
||||
print(string.format('hilbish: %s not found', cmd))
|
||||
end)
|
||||
|
||||
|
|
36
rl.go
36
rl.go
|
@ -1,36 +0,0 @@
|
|||
// +build !hilbiline
|
||||
|
||||
// Here we define a generic interface for readline and hilbiline,
|
||||
// making them interchangable during build time
|
||||
// this is normal readline
|
||||
package main
|
||||
|
||||
import "github.com/bobappleyard/readline"
|
||||
|
||||
type LineReader struct {
|
||||
Prompt string
|
||||
}
|
||||
|
||||
// other gophers might hate this naming but this is local, shut up
|
||||
func NewLineReader(prompt string) *LineReader {
|
||||
readline.Completer = readline.FilenameCompleter
|
||||
readline.LoadHistory(homedir + "/.hilbish-history")
|
||||
|
||||
return &LineReader{
|
||||
Prompt: prompt,
|
||||
}
|
||||
}
|
||||
|
||||
func (lr *LineReader) Read() (string, error) {
|
||||
return readline.String(lr.Prompt)
|
||||
}
|
||||
|
||||
func (lr *LineReader) SetPrompt(prompt string) {
|
||||
lr.Prompt = prompt
|
||||
}
|
||||
|
||||
func (lr *LineReader) AddHistory(cmd string) {
|
||||
readline.AddHistory(cmd)
|
||||
readline.SaveHistory(homedir + "/.hilbish-history")
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
// +build hilbiline
|
||||
|
||||
// Here we define a generic interface for readline and hilbiline,
|
||||
// making them interchangable during build time
|
||||
// this is hilbiline's, as is obvious by the filename
|
||||
package main
|
||||
|
||||
import "github.com/Hilbis/Hilbiline"
|
||||
|
||||
type LineReader struct {
|
||||
hl *hilbiline.HilbilineState
|
||||
}
|
||||
|
||||
// other gophers might hate this naming but this is local, shut up
|
||||
func NewLineReader(prompt string) *LineReader {
|
||||
hl := hilbiline.New(prompt)
|
||||
|
||||
return &LineReader{
|
||||
&hl,
|
||||
}
|
||||
}
|
||||
|
||||
func (lr *LineReader) Read() (string, error) {
|
||||
return lr.hl.Read()
|
||||
}
|
||||
|
||||
func (lr *LineReader) SetPrompt(prompt string) {
|
||||
lr.hl.SetPrompt(prompt)
|
||||
}
|
||||
|
||||
func (lr *LineReader) AddHistory(cmd string) {
|
||||
return
|
||||
}
|
||||
|
110
shell.go
110
shell.go
|
@ -5,9 +5,8 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
// "github.com/bobappleyard/readline"
|
||||
"github.com/bobappleyard/readline"
|
||||
"github.com/yuin/gopher-lua"
|
||||
"github.com/yuin/gopher-lua/parse"
|
||||
"layeh.com/gopher-luar"
|
||||
|
@ -16,7 +15,18 @@ import (
|
|||
)
|
||||
|
||||
func RunInput(input string) {
|
||||
_, cmdString := splitInput(input)
|
||||
cmdArgs, cmdString := splitInput(input)
|
||||
|
||||
// If alias was found, use command alias
|
||||
for aliases[cmdArgs[0]] != "" {
|
||||
alias := aliases[cmdArgs[0]]
|
||||
cmdString = alias + strings.TrimPrefix(cmdString, cmdArgs[0])
|
||||
cmdArgs, cmdString = splitInput(cmdString)
|
||||
|
||||
if aliases[cmdArgs[0]] != "" {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// First try to load input, essentially compiling to bytecode
|
||||
fn, err := l.LoadString(cmdString)
|
||||
|
@ -35,10 +45,43 @@ func RunInput(input string) {
|
|||
err = l.PCall(0, lua.MultRet, nil)
|
||||
}
|
||||
if err == nil {
|
||||
// If it succeeds, add to history and prompt again
|
||||
HandleHistory(input)
|
||||
|
||||
hooks.Em.Emit("command.exit", 0)
|
||||
return
|
||||
}
|
||||
|
||||
// If command is defined in Lua then run it
|
||||
if commands[cmdArgs[0]] {
|
||||
err := l.CallByParam(lua.P{
|
||||
Fn: l.GetField(
|
||||
l.GetTable(
|
||||
l.GetGlobal("commanding"),
|
||||
lua.LString("__commands")),
|
||||
cmdArgs[0]),
|
||||
NRet: 1,
|
||||
Protect: true,
|
||||
}, luar.New(l, cmdArgs[1:]))
|
||||
luaexitcode := l.Get(-1)
|
||||
exitcode := lua.LNumber(0)
|
||||
|
||||
l.Pop(1)
|
||||
|
||||
if code, ok := luaexitcode.(lua.LNumber); luaexitcode != lua.LNil && ok {
|
||||
exitcode = code
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr,
|
||||
"Error in command:\n\n" + err.Error())
|
||||
}
|
||||
if cmdArgs[0] != "exit" {
|
||||
HandleHistory(cmdString)
|
||||
}
|
||||
hooks.Em.Emit("command.exit", exitcode)
|
||||
return
|
||||
}
|
||||
|
||||
// Last option: use sh interpreter
|
||||
err = execCommand(cmdString)
|
||||
if err != nil {
|
||||
|
@ -70,6 +113,7 @@ func RunInput(input string) {
|
|||
} else {
|
||||
hooks.Em.Emit("command.exit", 0)
|
||||
}
|
||||
HandleHistory(cmdString)
|
||||
}
|
||||
|
||||
// Run command in sh interpreter
|
||||
|
@ -78,60 +122,8 @@ func execCommand(cmd string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
exechandle := func(ctx context.Context, args []string) error {
|
||||
hc := interp.HandlerCtx(ctx)
|
||||
args, argstring := splitInput(strings.Join(args, " "))
|
||||
|
||||
// If alias was found, use command alias
|
||||
for aliases[args[0]] != "" {
|
||||
alias := aliases[args[0]]
|
||||
argstring = alias + strings.TrimPrefix(argstring, args[0])
|
||||
args, argstring = splitInput(argstring)
|
||||
|
||||
if aliases[args[0]] != "" {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// If command is defined in Lua then run it
|
||||
if commands[args[0]] {
|
||||
err := l.CallByParam(lua.P{
|
||||
Fn: l.GetField(
|
||||
l.GetTable(
|
||||
l.GetGlobal("commanding"),
|
||||
lua.LString("__commands")),
|
||||
args[0]),
|
||||
NRet: 1,
|
||||
Protect: true,
|
||||
}, luar.New(l, args[1:]))
|
||||
luaexitcode := l.Get(-1)
|
||||
var exitcode uint8 = 0
|
||||
|
||||
l.Pop(1)
|
||||
|
||||
if code, ok := luaexitcode.(lua.LNumber); luaexitcode != lua.LNil && ok {
|
||||
exitcode = uint8(code)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr,
|
||||
"Error in command:\n\n" + err.Error())
|
||||
}
|
||||
hooks.Em.Emit("command.exit", exitcode)
|
||||
return interp.NewExitStatus(exitcode)
|
||||
}
|
||||
|
||||
if _, err := interp.LookPathDir(hc.Dir, hc.Env, args[0]); err != nil {
|
||||
hooks.Em.Emit("command.not-found", args[0])
|
||||
return interp.NewExitStatus(127)
|
||||
}
|
||||
|
||||
return interp.DefaultExecHandler(2*time.Second)(ctx, args)
|
||||
}
|
||||
runner, _ := interp.New(
|
||||
interp.StdIO(os.Stdin, os.Stdout, os.Stderr),
|
||||
interp.ExecHandler(exechandle),
|
||||
)
|
||||
err = runner.Run(context.TODO(), file)
|
||||
|
||||
|
@ -147,7 +139,7 @@ func splitInput(input string) ([]string, string) {
|
|||
cmdArgs := []string{}
|
||||
sb := &strings.Builder{}
|
||||
cmdstr := &strings.Builder{}
|
||||
lastcmd := "" //readline.GetHistory(readline.HistorySize() - 1)
|
||||
lastcmd := readline.GetHistory(readline.HistorySize() - 1)
|
||||
|
||||
for _, r := range input {
|
||||
if r == '"' {
|
||||
|
@ -191,3 +183,9 @@ func splitInput(input string) ([]string, string) {
|
|||
return cmdArgs, cmdstr.String()
|
||||
}
|
||||
|
||||
func HandleHistory(cmd string) {
|
||||
readline.AddHistory(cmd)
|
||||
readline.SaveHistory(homedir + "/.hilbish-history")
|
||||
// TODO: load history again (history shared between sessions like this ye)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue