-
Notifications
You must be signed in to change notification settings - Fork 3
/
example2.go
65 lines (53 loc) · 1.37 KB
/
example2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//go:build run
package main
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/mattn/go-colorable"
"github.com/nyaosorg/go-readline-ny"
"github.com/nyaosorg/go-readline-ny/coloring"
"github.com/nyaosorg/go-readline-ny/completion"
"github.com/nyaosorg/go-readline-ny/keys"
"github.com/nyaosorg/go-readline-ny/simplehistory"
)
func main() {
history := simplehistory.New()
editor := &readline.Editor{
PromptWriter: func(w io.Writer) (int, error) {
return io.WriteString(w, "\x1B[1;36m$ \x1B[0m") // print `$ ` with cyan
},
Writer: colorable.NewColorableStdout(),
History: history,
Coloring: &coloring.VimBatch{},
HistoryCycling: true,
PredictColor: readline.PredictColorBlueItalic,
}
editor.BindKey(keys.CtrlI, completion.CmdCompletionOrList{
Completion: completion.File{},
Postfix: " ",
})
// If you do not want to list files with double-tab-key,
// use `CmdCompletion` instead of `CmdCompletionOrList`
fmt.Println("Tiny Shell. Type Ctrl-D to quit.")
for {
text, err := editor.ReadLine(context.Background())
if err != nil {
fmt.Printf("ERR=%s\n", err.Error())
return
}
fields := strings.Fields(text)
if len(fields) <= 0 {
continue
}
cmd := exec.Command(fields[0], fields[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Run()
history.Add(text)
}
}