mirror of https://github.com/Hilbis/Hilbish
Compare commits
12 Commits
b221921ab7
...
babb870383
Author | SHA1 | Date |
---|---|---|
TorchedSammy | babb870383 | |
TorchedSammy | 8dcd57309f | |
TorchedSammy | f292245c23 | |
TorchedSammy | aaeecceabf | |
TorchedSammy | 3cdd21a4fa | |
TorchedSammy | 496bf616c6 | |
TorchedSammy | df6a2bb0c7 | |
TorchedSammy | 3165552c21 | |
TorchedSammy | f26931d4cb | |
TorchedSammy | 994daba078 | |
TorchedSammy | 311d7d56bd | |
TorchedSammy | d9d2152e04 |
|
@ -6,13 +6,17 @@ on:
|
|||
|
||||
jobs:
|
||||
build:
|
||||
name: ${{ matrix.build }}
|
||||
runs-on: ${{ matrix.os }}
|
||||
name: ${{ matrix.goos }}-${{ matrix.goarch }}
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- build: linux-amd64
|
||||
os: ubuntu-latest
|
||||
goos: [linux, windows, darwin]
|
||||
goarch: ["386", amd64, arm64]
|
||||
exclude:
|
||||
- goarch: "386"
|
||||
goos: darwin
|
||||
- goarch: arm64
|
||||
goos: windows
|
||||
steps:
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v2
|
||||
|
@ -21,11 +25,11 @@ jobs:
|
|||
- name: Setup Go
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: '1.16.2'
|
||||
go-version: '1.17.7'
|
||||
- name: Build
|
||||
run: make hilbiline
|
||||
run: GOARCH=${{ matrix.goarch }} go build
|
||||
- uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: hilbish-${{ matrix.build }}
|
||||
name: hilbish-${{ matrix.goos }}-${{ matrix.goarch }}
|
||||
path: hilbish
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ with Lua
|
|||
- Home dir is now added to recent dirs (the case of cd with no arg)
|
||||
- `index` subdoc will no longer appear
|
||||
- Alias expansion with quotes
|
||||
- Add full command to history in the case of incomplete input
|
||||
|
||||
### Changed
|
||||
- The minimal config is truly minimal now
|
||||
|
|
3
Makefile
3
Makefile
|
@ -9,9 +9,6 @@ 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
|
||||
|
||||
install:
|
||||
@install -v -d "$(DESTDIR)$(BINDIR)/" && install -m 0755 -v hilbish "$(DESTDIR)$(BINDIR)/hilbish"
|
||||
@mkdir -p "$(DESTDIR)$(LIBDIR)"
|
||||
|
|
2
api.go
2
api.go
|
@ -88,7 +88,7 @@ The nice lil shell for {blue}Lua{reset} fanatics!
|
|||
func hlrun(L *lua.LState) int {
|
||||
var exitcode uint8
|
||||
cmd := L.CheckString(1)
|
||||
err := execCommand(cmd)
|
||||
err := execCommand(cmd, cmd)
|
||||
|
||||
if code, ok := interp.IsExitStatus(err); ok {
|
||||
exitcode = code
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"os"
|
||||
)
|
||||
|
||||
func fileComplete(query, ctx string, fields []string) []string {
|
||||
var completions []string
|
||||
|
||||
prefixes := []string{"./", "../", "/", "~/"}
|
||||
for _, prefix := range prefixes {
|
||||
if strings.HasPrefix(query, prefix) {
|
||||
completions, _ = matchPath(strings.Replace(query, "~", curuser.HomeDir, 1), query)
|
||||
}
|
||||
}
|
||||
|
||||
if len(completions) == 0 && len(fields) > 1 {
|
||||
completions, _ = matchPath("./" + query, query)
|
||||
}
|
||||
|
||||
return completions
|
||||
}
|
||||
|
||||
func matchPath(path, pref string) ([]string, error) {
|
||||
var entries []string
|
||||
matches, err := filepath.Glob(path + "*")
|
||||
if err == nil {
|
||||
args := []string{
|
||||
"\"", "\\\"",
|
||||
"'", "\\'",
|
||||
"`", "\\`",
|
||||
" ", "\\ ",
|
||||
"(", "\\(",
|
||||
")", "\\)",
|
||||
"[", "\\[",
|
||||
"]", "\\]",
|
||||
}
|
||||
|
||||
r := strings.NewReplacer(args...)
|
||||
for _, match := range matches {
|
||||
name := filepath.Base(match)
|
||||
p := filepath.Base(pref)
|
||||
if pref == "" {
|
||||
p = ""
|
||||
}
|
||||
name = strings.TrimPrefix(name, p)
|
||||
matchFull, _ := filepath.Abs(match)
|
||||
if info, err := os.Stat(matchFull); err == nil && info.IsDir() {
|
||||
name = name + string(os.PathSeparator)
|
||||
}
|
||||
name = r.Replace(name)
|
||||
entries = append(entries, name)
|
||||
}
|
||||
}
|
||||
|
||||
return entries, err
|
||||
}
|
26
exec.go
26
exec.go
|
@ -15,7 +15,7 @@ import (
|
|||
"mvdan.cc/sh/v3/syntax"
|
||||
)
|
||||
|
||||
func runInput(input string) {
|
||||
func runInput(input, origInput string) {
|
||||
running = true
|
||||
cmdString := aliases.Resolve(input)
|
||||
|
||||
|
@ -39,12 +39,12 @@ func runInput(input string) {
|
|||
err = l.PCall(0, lua.MultRet, nil)
|
||||
}
|
||||
if err == nil {
|
||||
cmdFinish(0, cmdString)
|
||||
cmdFinish(0, cmdString, origInput)
|
||||
return
|
||||
}
|
||||
|
||||
// Last option: use sh interpreter
|
||||
err = execCommand(cmdString)
|
||||
err = execCommand(cmdString, origInput)
|
||||
if err != nil {
|
||||
// If input is incomplete, start multiline prompting
|
||||
if syntax.IsIncomplete(err) {
|
||||
|
@ -53,31 +53,31 @@ func runInput(input string) {
|
|||
if err != nil {
|
||||
break
|
||||
}
|
||||
err = execCommand(cmdString)
|
||||
err = execCommand(cmdString, origInput)
|
||||
if syntax.IsIncomplete(err) || strings.HasSuffix(input, "\\") {
|
||||
continue
|
||||
} else if code, ok := interp.IsExitStatus(err); ok {
|
||||
cmdFinish(code, cmdString)
|
||||
cmdFinish(code, cmdString, origInput)
|
||||
} else if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
cmdFinish(1, cmdString)
|
||||
cmdFinish(1, cmdString, origInput)
|
||||
}
|
||||
break
|
||||
}
|
||||
} else {
|
||||
if code, ok := interp.IsExitStatus(err); ok {
|
||||
cmdFinish(code, cmdString)
|
||||
cmdFinish(code, cmdString, origInput)
|
||||
} else {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cmdFinish(0, cmdString)
|
||||
cmdFinish(0, cmdString, origInput)
|
||||
}
|
||||
}
|
||||
|
||||
// Run command in sh interpreter
|
||||
func execCommand(cmd string) error {
|
||||
func execCommand(cmd, old string) error {
|
||||
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -127,7 +127,7 @@ func execCommand(cmd string) error {
|
|||
exitcode = uint8(code)
|
||||
}
|
||||
|
||||
cmdFinish(exitcode, argstring)
|
||||
cmdFinish(exitcode, argstring, old)
|
||||
return interp.NewExitStatus(exitcode)
|
||||
}
|
||||
|
||||
|
@ -237,6 +237,10 @@ func splitInput(input string) ([]string, string) {
|
|||
return cmdArgs, cmdstr.String()
|
||||
}
|
||||
|
||||
func cmdFinish(code uint8, cmdstr string) {
|
||||
func cmdFinish(code uint8, cmdstr, oldInput string) {
|
||||
// if input has space at the beginning, dont put in history
|
||||
if !strings.HasPrefix(oldInput, " ") || interactive {
|
||||
handleHistory(cmdstr)
|
||||
}
|
||||
hooks.Em.Emit("command.exit", code, cmdstr)
|
||||
}
|
||||
|
|
8
go.mod
8
go.mod
|
@ -3,16 +3,8 @@ module hilbish
|
|||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/Rosettea/Hilbiline v0.0.0-20210624011007-8088a2d84b65
|
||||
github.com/Rosettea/readline v0.0.0-20211207004608-4afb088da503
|
||||
github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9
|
||||
github.com/creack/goselect v0.1.2 // indirect
|
||||
github.com/creack/termios v0.0.0-20160714173321-88d0029e36a1 // indirect
|
||||
github.com/kr/pty v1.1.8 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.13 // indirect
|
||||
github.com/maxlandon/readline v0.1.0-beta.0.20211027085530-2b76cabb8036
|
||||
github.com/pborman/ansi v1.0.0 // indirect
|
||||
github.com/pborman/getopt v1.1.0
|
||||
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9
|
||||
golang.org/x/sys v0.0.0-20220224120231-95c6836cb0e7 // indirect
|
||||
|
|
29
go.sum
29
go.sum
|
@ -1,9 +1,3 @@
|
|||
github.com/Rosettea/Hilbiline v0.0.0-20210624011007-8088a2d84b65 h1:z6alP313nLg1U/V3bKSd6849L/A2LxDSwKv4CPqqzDk=
|
||||
github.com/Rosettea/Hilbiline v0.0.0-20210624011007-8088a2d84b65/go.mod h1:/FFZ4cgR6TXXYaskRUxyLIYdfG0PS4BPtWjWRQms754=
|
||||
github.com/Rosettea/Hilbiline v0.0.0-20210710124707-aa6e3ff34cb2 h1:6f1umn6mkodpGf6rK9LZjr4Gut2uS+b8QLoFBFTeOE8=
|
||||
github.com/Rosettea/Hilbiline v0.0.0-20210710124707-aa6e3ff34cb2/go.mod h1:0J2+sRC+d4a3swcH20sVlFvYUEXASvGTHJnVTTI4S9w=
|
||||
github.com/Rosettea/readline v0.0.0-20211207004608-4afb088da503 h1:hA2kXBwY8SFH3+PT67CkoUjlMuRN08RSEtsKjwQ+of4=
|
||||
github.com/Rosettea/readline v0.0.0-20211207004608-4afb088da503/go.mod h1:OH+WJSCks0t2ISvaCFUT4ZxNGr4Etq4ju9JE/UxH/5o=
|
||||
github.com/Rosettea/readline-1 v0.1.0-beta.0.20211207003625-341c7985ad7d h1:KBttN41h/tPahmpaZavviwQ8q4rCkt5CD0HdVmfgPVA=
|
||||
github.com/Rosettea/readline-1 v0.1.0-beta.0.20211207003625-341c7985ad7d/go.mod h1:QiUAvbhg8PzCA4hlafCUl0bKD/0VmcocM4AjqtszAJs=
|
||||
github.com/Rosettea/sh/v3 v3.4.0-0.dev.0.20211022004519-f67a49cb50f5 h1:ygwVRX8gf5MHA0VzSgOdscCEoAJLjM8joEotfQPgAd0=
|
||||
|
@ -15,14 +9,9 @@ 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/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=
|
||||
github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
|
||||
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/creack/pty v1.1.15 h1:cKRCLMj3Ddm54bKSpemfQ8AtYFBhAI2MPmdys22fBdc=
|
||||
github.com/creack/pty v1.1.15/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
|
||||
github.com/creack/termios v0.0.0-20160714173321-88d0029e36a1 h1:3ZFknr3UZk2E18CuCeA0NMRk226zM/slMEFOmJTtJjI=
|
||||
github.com/creack/termios v0.0.0-20160714173321-88d0029e36a1/go.mod h1:141QqpYDZtzU1VJMRHPU6ZWkn9K5cE6X8elajH9hJk4=
|
||||
github.com/evilsocket/islazy v1.10.6 h1:MFq000a1ByoumoJWlytqg0qon0KlBeUfPsDjY0hK0bo=
|
||||
github.com/evilsocket/islazy v1.10.6/go.mod h1:OrwQGYg3DuZvXUfmH+KIZDjwTCbrjy48T24TUpGqVVw=
|
||||
github.com/google/renameio v1.0.1/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk=
|
||||
|
@ -31,27 +20,16 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
|
|||
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/pty v1.1.8 h1:AkaSdXYQOWeaO3neb8EM634ahkXXe3jYbVh/F9lq+GI=
|
||||
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/layeh/gopher-luar v1.0.10 h1:8NIv4MX1Arz96kK4buGK1D87DyDxKZyq6KKvJ2diHp0=
|
||||
github.com/layeh/gopher-luar v1.0.10/go.mod h1:TPnIVCZ2RJBndm7ohXyaqfhzjlZ+OA2SZR/YwL8tECk=
|
||||
github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
|
||||
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/olekukonko/ts v0.0.0-20171002115256-78ecb04241c0 h1:LiZB1h0GIcudcDci2bxbqI6DXV8bF8POAnArqvRrIyw=
|
||||
github.com/olekukonko/ts v0.0.0-20171002115256-78ecb04241c0/go.mod h1:F/7q8/HZz+TXjlsoZQQKVYvXTZaFH4QRa3y+j1p7MS0=
|
||||
github.com/pborman/ansi v1.0.0 h1:OqjHMhvlSuCCV5JT07yqPuJPQzQl+WXsiZ14gZsqOrQ=
|
||||
github.com/pborman/ansi v1.0.0/go.mod h1:SgWzwMAx1X/Ez7i90VqF8LRiQtx52pWDiQP+x3iGnzw=
|
||||
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-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||
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.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
|
||||
|
@ -63,21 +41,14 @@ github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9/go.mod h1:E1AXubJB
|
|||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210414055047-fe65e336abe0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210925032602-92d5a993a665/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220224120231-95c6836cb0e7 h1:BXxu8t6QN0G1uff4bzZzSkpsax8+ALqTGUtz08QrV00=
|
||||
golang.org/x/sys v0.0.0-20220224120231-95c6836cb0e7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20210406210042-72f3dc4e9b72/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210916214954-140adaaadfaf/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
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=
|
||||
layeh.com/gopher-luar v1.0.10 h1:55b0mpBhN9XSshEd2Nz6WsbYXctyBT35azk4POQNSXo=
|
||||
layeh.com/gopher-luar v1.0.10/go.mod h1:TPnIVCZ2RJBndm7ohXyaqfhzjlZ+OA2SZR/YwL8tECk=
|
||||
mvdan.cc/editorconfig v0.2.0/go.mod h1:lvnnD3BNdBYkhq+B4uBuFFKatfp02eB6HixDvEz91C0=
|
||||
|
|
13
main.go
13
main.go
|
@ -139,12 +139,13 @@ func main() {
|
|||
if fileInfo, _ := os.Stdin.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 {
|
||||
scanner := bufio.NewScanner(bufio.NewReader(os.Stdin))
|
||||
for scanner.Scan() {
|
||||
runInput(scanner.Text())
|
||||
text := scanner.Text()
|
||||
runInput(text, text)
|
||||
}
|
||||
}
|
||||
|
||||
if *cmdflag != "" {
|
||||
runInput(*cmdflag)
|
||||
runInput(*cmdflag, *cmdflag)
|
||||
}
|
||||
|
||||
if getopt.NArgs() > 0 {
|
||||
|
@ -188,7 +189,7 @@ input:
|
|||
|
||||
if strings.HasSuffix(input, "\\") {
|
||||
for {
|
||||
input, err = continuePrompt(strings.TrimSuffix(input, "\\"))
|
||||
input, err = continuePrompt(input)
|
||||
if err != nil {
|
||||
goto input // continue inside nested loop
|
||||
}
|
||||
|
@ -198,11 +199,7 @@ input:
|
|||
}
|
||||
}
|
||||
|
||||
// if input has space at the beginning, dont put in history
|
||||
if !strings.HasPrefix(oldInput, " ") {
|
||||
handleHistory(input)
|
||||
}
|
||||
runInput(input)
|
||||
runInput(input, oldInput)
|
||||
|
||||
termwidth, _, err := term.GetSize(0)
|
||||
if err != nil {
|
||||
|
|
134
rl.go
134
rl.go
|
@ -1,59 +1,62 @@
|
|||
//go:build !hilbiline && !goreadline
|
||||
// +build !hilbiline,!goreadline
|
||||
|
||||
package main
|
||||
|
||||
// Here we define a generic interface for readline and hilbiline,
|
||||
// making them interchangable during build time
|
||||
// this is normal readline
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"os"
|
||||
|
||||
"github.com/Rosettea/readline"
|
||||
"github.com/maxlandon/readline"
|
||||
"github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
type lineReader struct {
|
||||
Prompt string
|
||||
rl *readline.Instance
|
||||
}
|
||||
|
||||
// other gophers might hate this naming but this is local, shut up
|
||||
func newLineReader(prompt string) *lineReader {
|
||||
readline.Init()
|
||||
|
||||
readline.Completer = func(query string, ctx string) []string {
|
||||
rl := readline.NewInstance()
|
||||
rl.Multiline = true
|
||||
rl.TabCompleter = func(line []rune, pos int, _ readline.DelayedTabContext) (string, []*readline.CompletionGroup) {
|
||||
ctx := string(line)
|
||||
var completions []string
|
||||
// trim whitespace from ctx
|
||||
|
||||
compGroup := []*readline.CompletionGroup{
|
||||
&readline.CompletionGroup{
|
||||
TrimSlash: false,
|
||||
NoSpace: true,
|
||||
},
|
||||
}
|
||||
|
||||
ctx = strings.TrimLeft(ctx, " ")
|
||||
if len(ctx) == 0 {
|
||||
return []string{}
|
||||
return "", compGroup
|
||||
}
|
||||
|
||||
fields := strings.Split(ctx, " ")
|
||||
if len(fields) == 0 {
|
||||
return []string{}
|
||||
return "", compGroup
|
||||
}
|
||||
query := fields[len(fields) - 1]
|
||||
|
||||
ctx = aliases.Resolve(ctx)
|
||||
|
||||
|
||||
if len(fields) == 1 {
|
||||
prefixes := []string{"./", "../", "/", "~/"}
|
||||
for _, prefix := range prefixes {
|
||||
if strings.HasPrefix(fields[0], prefix) {
|
||||
fileCompletions := append(completions, readline.FilenameCompleter(query, ctx)...)
|
||||
// filter out executables
|
||||
for _, f := range fileCompletions {
|
||||
name := strings.Replace(f, "~", curuser.HomeDir, 1)
|
||||
if info, err := os.Stat(name); err == nil && info.Mode().Perm() & 0100 == 0 {
|
||||
continue
|
||||
}
|
||||
completions = append(completions, f)
|
||||
fileCompletions := fileComplete(query, ctx, fields)
|
||||
if len(fileCompletions) != 0 {
|
||||
for _, f := range fileCompletions {
|
||||
name := strings.Replace(query + f, "~", curuser.HomeDir, 1)
|
||||
if info, err := os.Stat(name); err == nil && info.Mode().Perm() & 0100 == 0 {
|
||||
continue
|
||||
}
|
||||
return completions
|
||||
completions = append(completions, f)
|
||||
}
|
||||
compGroup[0].Suggestions = completions
|
||||
return "", compGroup
|
||||
}
|
||||
|
||||
|
||||
// filter out executables, but in path
|
||||
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
|
||||
// print dir to stderr for debugging
|
||||
|
@ -73,12 +76,16 @@ func newLineReader(prompt string) *lineReader {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// add lua registered commands to completions
|
||||
for cmdName := range commands {
|
||||
if strings.HasPrefix(cmdName, query) {
|
||||
completions = append(completions, cmdName)
|
||||
}
|
||||
}
|
||||
|
||||
compGroup[0].Suggestions = completions
|
||||
return query, compGroup
|
||||
} else {
|
||||
if completecb, ok := luaCompletions["command." + fields[0]]; ok {
|
||||
err := l.CallByParam(lua.P{
|
||||
|
@ -88,7 +95,7 @@ func newLineReader(prompt string) *lineReader {
|
|||
})
|
||||
|
||||
if err != nil {
|
||||
return []string{}
|
||||
return "", compGroup
|
||||
}
|
||||
|
||||
luacompleteTable := l.Get(-1)
|
||||
|
@ -123,7 +130,7 @@ func newLineReader(prompt string) *lineReader {
|
|||
val := value.String()
|
||||
if val == "<file>" {
|
||||
// complete files
|
||||
completions = append(completions, readline.FilenameCompleter(query, ctx)...)
|
||||
completions = append(completions, fileComplete(query, ctx, fields)...)
|
||||
} else {
|
||||
if strings.HasPrefix(val, query) {
|
||||
completions = append(completions, val)
|
||||
|
@ -171,39 +178,53 @@ func newLineReader(prompt string) *lineReader {
|
|||
}
|
||||
|
||||
if len(completions) == 0 {
|
||||
completions = readline.FilenameCompleter(query, ctx)
|
||||
completions = fileComplete(query, ctx, fields)
|
||||
}
|
||||
}
|
||||
return completions
|
||||
|
||||
compGroup[0].Suggestions = completions
|
||||
return "", compGroup
|
||||
}
|
||||
readline.LoadHistory(defaultHistPath)
|
||||
|
||||
return &lineReader{
|
||||
Prompt: prompt,
|
||||
rl,
|
||||
}
|
||||
}
|
||||
|
||||
func (lr *lineReader) Read() (string, error) {
|
||||
hooks.Em.Emit("command.precmd", nil)
|
||||
return readline.String(lr.Prompt)
|
||||
s, err := lr.rl.Readline()
|
||||
// this is so dumb
|
||||
if err == readline.EOF {
|
||||
return "", io.EOF
|
||||
}
|
||||
|
||||
return s, err // might get another error
|
||||
}
|
||||
|
||||
func (lr *lineReader) SetPrompt(prompt string) {
|
||||
lr.Prompt = prompt
|
||||
halfPrompt := strings.Split(prompt, "\n")
|
||||
if len(halfPrompt) > 1 {
|
||||
lr.rl.SetPrompt(strings.Join(halfPrompt[:len(halfPrompt) - 1], "\n"))
|
||||
lr.rl.MultilinePrompt = halfPrompt[len(halfPrompt) - 1:][0]
|
||||
} else {
|
||||
// print cursor up ansi code
|
||||
fmt.Printf("\033[1A")
|
||||
lr.rl.SetPrompt("")
|
||||
lr.rl.MultilinePrompt = halfPrompt[len(halfPrompt) - 1:][0]
|
||||
}
|
||||
}
|
||||
|
||||
func (lr *lineReader) AddHistory(cmd string) {
|
||||
readline.AddHistory(cmd)
|
||||
readline.SaveHistory(defaultHistPath)
|
||||
return
|
||||
}
|
||||
|
||||
func (lr *lineReader) ClearInput() {
|
||||
readline.ReplaceLine("", 0)
|
||||
readline.RefreshLine()
|
||||
return
|
||||
}
|
||||
|
||||
func (lr *lineReader) Resize() {
|
||||
readline.Resize()
|
||||
return
|
||||
}
|
||||
|
||||
// lua module
|
||||
|
@ -216,7 +237,7 @@ func (lr *lineReader) Loader(L *lua.LState) *lua.LTable {
|
|||
"size": lr.luaSize,
|
||||
}
|
||||
|
||||
mod := L.SetFuncs(L.NewTable(), lrLua)
|
||||
mod := l.SetFuncs(l.NewTable(), lrLua)
|
||||
|
||||
return mod
|
||||
}
|
||||
|
@ -229,36 +250,17 @@ func (lr *lineReader) luaAddHistory(l *lua.LState) int {
|
|||
}
|
||||
|
||||
func (lr *lineReader) luaSize(l *lua.LState) int {
|
||||
l.Push(lua.LNumber(readline.HistorySize()))
|
||||
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (lr *lineReader) luaGetHistory(l *lua.LState) int {
|
||||
idx := l.CheckInt(1)
|
||||
cmd := readline.GetHistory(idx)
|
||||
l.Push(lua.LString(cmd))
|
||||
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (lr *lineReader) luaAllHistory(l *lua.LState) int {
|
||||
tbl := l.NewTable()
|
||||
size := readline.HistorySize()
|
||||
|
||||
for i := 0; i < size; i++ {
|
||||
cmd := readline.GetHistory(i)
|
||||
tbl.Append(lua.LString(cmd))
|
||||
}
|
||||
|
||||
l.Push(tbl)
|
||||
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (lr *lineReader) luaClearHistory(l *lua.LState) int {
|
||||
readline.ClearHistory()
|
||||
readline.SaveHistory(defaultHistPath)
|
||||
|
||||
return 0
|
||||
}
|
||||
|
|
|
@ -1,83 +0,0 @@
|
|||
// +build hilbiline,!goreadline
|
||||
|
||||
package main
|
||||
|
||||
// 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
|
||||
|
||||
import (
|
||||
"github.com/Rosettea/Hilbiline"
|
||||
"github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (lr *lineReader) ClearInput() {
|
||||
return
|
||||
}
|
||||
|
||||
func (lr *lineReader) Resize() {
|
||||
return
|
||||
}
|
||||
|
||||
// lua module
|
||||
func (lr *lineReader) Loader(L *lua.LState) *lua.LTable {
|
||||
lrLua := map[string]lua.LGFunction{
|
||||
"add": lr.luaAddHistory,
|
||||
"all": lr.luaAllHistory,
|
||||
"clear": lr.luaClearHistory,
|
||||
"get": lr.luaGetHistory,
|
||||
"size": lr.luaSize,
|
||||
}
|
||||
|
||||
mod := l.SetFuncs(l.NewTable(), lrLua)
|
||||
|
||||
return mod
|
||||
}
|
||||
|
||||
func (lr *lineReader) luaAddHistory(l *lua.LState) int {
|
||||
cmd := l.CheckString(1)
|
||||
lr.AddHistory(cmd)
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func (lr *lineReader) luaSize(l *lua.LState) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (lr *lineReader) luaGetHistory(l *lua.LState) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (lr *lineReader) luaAllHistory(l *lua.LState) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (lr *lineReader) luaClearHistory(l *lua.LState) int {
|
||||
return 0
|
||||
}
|
|
@ -1,103 +0,0 @@
|
|||
// +build goreadline,!hilbiline
|
||||
|
||||
package main
|
||||
|
||||
// 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
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
"fmt"
|
||||
|
||||
"github.com/maxlandon/readline"
|
||||
"github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
type lineReader struct {
|
||||
rl *readline.Instance
|
||||
}
|
||||
|
||||
// other gophers might hate this naming but this is local, shut up
|
||||
func newLineReader(prompt string) *lineReader {
|
||||
rl := readline.NewInstance()
|
||||
rl.Multiline = true
|
||||
|
||||
return &lineReader{
|
||||
rl,
|
||||
}
|
||||
}
|
||||
|
||||
func (lr *lineReader) Read() (string, error) {
|
||||
s, err := lr.rl.Readline()
|
||||
// this is so dumb
|
||||
if err == readline.EOF {
|
||||
return "", io.EOF
|
||||
}
|
||||
|
||||
return s, err // might get another error
|
||||
}
|
||||
|
||||
func (lr *lineReader) SetPrompt(prompt string) {
|
||||
halfPrompt := strings.Split(prompt, "\n")
|
||||
if len(halfPrompt) > 1 {
|
||||
lr.rl.SetPrompt(strings.Join(halfPrompt[:len(halfPrompt) - 1], "\n"))
|
||||
lr.rl.MultilinePrompt = halfPrompt[len(halfPrompt) - 1:][0]
|
||||
} else {
|
||||
// print cursor up ansi code
|
||||
fmt.Printf("\033[1A")
|
||||
lr.rl.SetPrompt("")
|
||||
lr.rl.MultilinePrompt = halfPrompt[len(halfPrompt) - 1:][0]
|
||||
}
|
||||
}
|
||||
|
||||
func (lr *lineReader) AddHistory(cmd string) {
|
||||
return
|
||||
}
|
||||
|
||||
func (lr *lineReader) ClearInput() {
|
||||
return
|
||||
}
|
||||
|
||||
func (lr *lineReader) Resize() {
|
||||
return
|
||||
}
|
||||
|
||||
// lua module
|
||||
func (lr *lineReader) Loader(L *lua.LState) *lua.LTable {
|
||||
lrLua := map[string]lua.LGFunction{
|
||||
"add": lr.luaAddHistory,
|
||||
"all": lr.luaAllHistory,
|
||||
"clear": lr.luaClearHistory,
|
||||
"get": lr.luaGetHistory,
|
||||
"size": lr.luaSize,
|
||||
}
|
||||
|
||||
mod := l.SetFuncs(l.NewTable(), lrLua)
|
||||
|
||||
return mod
|
||||
}
|
||||
|
||||
func (lr *lineReader) luaAddHistory(l *lua.LState) int {
|
||||
cmd := l.CheckString(1)
|
||||
lr.AddHistory(cmd)
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func (lr *lineReader) luaSize(l *lua.LState) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (lr *lineReader) luaGetHistory(l *lua.LState) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (lr *lineReader) luaAllHistory(l *lua.LState) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (lr *lineReader) luaClearHistory(l *lua.LState) int {
|
||||
return 0
|
||||
}
|
Loading…
Reference in New Issue