Skip to content

Commit

Permalink
feat: add \r cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudingcity committed Sep 9, 2020
1 parent c50b55a commit 29186ce
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
20 changes: 19 additions & 1 deletion internal/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var cmds = []cmd{
{cmd: `\h`, desc: "show help"},
{cmd: `\l`, desc: "list available scripts"},
{cmd: `\s`, desc: "switch to the specified script"},
{cmd: `\r`, desc: "run the specified script"},
{cmd: `\c`, desc: "clean the terminal screen"},
{cmd: `\q`, desc: "to quit"},
}
Expand Down Expand Up @@ -115,6 +116,8 @@ func (s *Shell) execCommand(cmd string) {
s.listCmd()
case "s":
s.switchCmd(cmd)
case "r":
s.runCmd(cmd)
case "c":
s.cleanCmd()
case "q":
Expand Down Expand Up @@ -151,6 +154,21 @@ func (s *Shell) switchCmd(cmd string) {
s.reader.SetPrompt(yellow(s.current) + blue(prompt))
}

func (s *Shell) runCmd(cmd string) {
fields := strings.Fields(cmd)
if len(fields) < 3 {
s.println("run script failed")
return
}
name := fields[1]
script, ok := s.scripts[name]
if !ok {
s.printf("script %q does not exists\n", name)
return
}
script.Run(script, fields[2:])
}

func (s *Shell) cleanCmd() {
cmd := exec.Command("clear")
cmd.Stdout = s.out
Expand Down Expand Up @@ -180,7 +198,7 @@ func (s *Shell) printf(format string, a ...interface{}) {
func (s *Shell) autoCompleter() readline.AutoCompleter {
var pcs []readline.PrefixCompleterInterface
for _, cmd := range cmds {
if cmd.cmd == "\\s" {
if cmd.cmd == "\\s" || cmd.cmd == "\\r" {
var subs []readline.PrefixCompleterInterface
for _, name := range s.names {
subs = append(subs, readline.PcItem(name))
Expand Down
29 changes: 29 additions & 0 deletions internal/shell/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,35 @@ func TestInteractive(t *testing.T) {
})
})

t.Run("enter \\r", func(t *testing.T) {
t.Run("without script", func(t *testing.T) {
in := ioutil.NopCloser(bytes.NewBufferString("\\r\n"))
out := &bytes.Buffer{}
New(in, out).Run()
got := out.String()

assert.Contains(t, got, "run script failed")
})
t.Run("script not found", func(t *testing.T) {
in := ioutil.NopCloser(bytes.NewBufferString("\\r not-exists-script foo\n"))
out := &bytes.Buffer{}
New(in, out).Run()
got := out.String()

assert.Contains(t, got, `script "not-exists-script" does not exists`)
})
t.Run("exec script", func(t *testing.T) {
in := ioutil.NopCloser(bytes.NewBufferString("\\r fake enter something\n"))
out := &bytes.Buffer{}
shell := New(in, out)
shell.Register(fakeCmd)
shell.Run()

assert.Contains(t, fakeOut.String(), "fake exec")
fakeOut = &bytes.Buffer{}
})
})

t.Run("enter \\q", func(t *testing.T) {
in := ioutil.NopCloser(bytes.NewBufferString("\\q\n"))
out := &bytes.Buffer{}
Expand Down

0 comments on commit 29186ce

Please sign in to comment.