From a2ca47a496503d1b863cf515e5fe37f92ce527c5 Mon Sep 17 00:00:00 2001 From: aerth Date: Tue, 19 Apr 2016 04:33:54 +0000 Subject: [PATCH 1/4] Boss mode --- cmd/micro/command.go | 18 ++++++++++++++++++ cmd/micro/messenger.go | 3 ++- cmd/micro/micro.go | 10 ++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/cmd/micro/command.go b/cmd/micro/command.go index 7fcf41d6..faed87f4 100644 --- a/cmd/micro/command.go +++ b/cmd/micro/command.go @@ -2,10 +2,28 @@ package main import ( "os" + "os/exec" "regexp" "strings" ) +func HandleShellCommand(input string, view *View) { + inputCmd := strings.Split(input, " ")[0] + args := strings.Split(input, " ")[1:] + + // Execute Command + cmdout := exec.Command(inputCmd, args...) + output, err := cmdout.Output() + if err != nil { + messenger.Error("Error: " + err.Error()) + return + } + + // Display last line of output + messenger.Message(string(output)) + +} + // HandleCommand handles input from the user func HandleCommand(input string, view *View) { inputCmd := strings.Split(input, " ")[0] diff --git a/cmd/micro/messenger.go b/cmd/micro/messenger.go index bb30ba2f..ff164c3b 100644 --- a/cmd/micro/messenger.go +++ b/cmd/micro/messenger.go @@ -3,9 +3,10 @@ package main import ( "bufio" "fmt" - "github.com/gdamore/tcell" "os" "strconv" + + "github.com/gdamore/tcell" ) // TermMessage sends a message to the user in the terminal. This usually occurs before diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 6dbdb76a..962d8959 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -2,13 +2,14 @@ package main import ( "fmt" + "io/ioutil" + "os" + "github.com/gdamore/tcell" "github.com/gdamore/tcell/encoding" "github.com/go-errors/errors" "github.com/mattn/go-isatty" "github.com/mitchellh/go-homedir" - "io/ioutil" - "os" ) const ( @@ -200,6 +201,11 @@ func main() { if !canceled { HandleCommand(input, view) } + case tcell.KeyCtrlB: + input, canceled := messenger.Prompt("$ ") + if !canceled { + HandleShellCommand(input, view) + } case tcell.KeyCtrlG: DisplayHelp() // Make sure to resize the view if the user resized the terminal while looking at the help text From bbe78fbdff2758a7d20fa0f92626a9a7185824df Mon Sep 17 00:00:00 2001 From: aerth Date: Tue, 19 Apr 2016 04:53:56 +0000 Subject: [PATCH 2/4] Display nonblank output on full screen --- cmd/micro/command.go | 56 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/cmd/micro/command.go b/cmd/micro/command.go index faed87f4..b49491ed 100644 --- a/cmd/micro/command.go +++ b/cmd/micro/command.go @@ -5,6 +5,8 @@ import ( "os/exec" "regexp" "strings" + + "github.com/gdamore/tcell" ) func HandleShellCommand(input string, view *View) { @@ -18,12 +20,62 @@ func HandleShellCommand(input string, view *View) { messenger.Error("Error: " + err.Error()) return } + outstring := string(output) - // Display last line of output - messenger.Message(string(output)) + // Display last line of output if not empty + if outstring != "" { + DisplayBlock(outstring) + // } else { + // messenger.Message("No errors.") + } } +// DisplayBlock displays txt +// It blocks the main loop +func DisplayBlock(text string) { + topline := 0 + _, height := screen.Size() + screen.HideCursor() + totalLines := strings.Split(text, "\n") + for { + screen.Clear() + + lineEnd := topline + height + if lineEnd > len(totalLines) { + lineEnd = len(totalLines) + } + lines := totalLines[topline:lineEnd] + for y, line := range lines { + for x, ch := range line { + st := defStyle + screen.SetContent(x, y, ch, nil, st) + } + } + + screen.Show() + + event := screen.PollEvent() + switch e := event.(type) { + case *tcell.EventResize: + _, height = e.Size() + case *tcell.EventKey: + switch e.Key() { + case tcell.KeyUp: + if topline > 0 { + topline-- + } + case tcell.KeyDown: + if topline < len(totalLines)-height { + topline++ + } + case tcell.KeyCtrlQ, tcell.KeyCtrlW, tcell.KeyEscape, tcell.KeyCtrlC: + return + } + } + } +} + // HandleCommand handles input from the user func HandleCommand(input string, view *View) { inputCmd := strings.Split(input, " ")[0] From f3e9271cae5a2ab064645ff1395f4e0d9ae10d8c Mon Sep 17 00:00:00 2001 From: aerth Date: Tue, 19 Apr 2016 05:10:53 +0000 Subject: [PATCH 3/4] boss mode: Single line output stays in messenger, multiline output goes to a help-style screen --- cmd/micro/command.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/cmd/micro/command.go b/cmd/micro/command.go index b49491ed..2c6f1ecf 100644 --- a/cmd/micro/command.go +++ b/cmd/micro/command.go @@ -15,20 +15,17 @@ func HandleShellCommand(input string, view *View) { // Execute Command cmdout := exec.Command(inputCmd, args...) - output, err := cmdout.Output() - if err != nil { - messenger.Error("Error: " + err.Error()) + output, _ := cmdout.CombinedOutput() + outstring := string(output) + totalLines := strings.Split(outstring, "\n") + if len(totalLines) == 2 { + messenger.Message(outstring) return } - outstring := string(output) - - // Display last line of output if not empty if outstring != "" { + // Display nonblank output DisplayBlock(outstring) - // } else { - // messenger.Message("No errors.") } - } // DisplayBlock displays txt From 54f00cb93707c4b70a742c98fad392767cc64080 Mon Sep 17 00:00:00 2001 From: aerth Date: Tue, 19 Apr 2016 13:40:05 +0000 Subject: [PATCH 4/4] shell command output is held in buffer until completion --- cmd/micro/command.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/cmd/micro/command.go b/cmd/micro/command.go index 2c6f1ecf..8600d7a1 100644 --- a/cmd/micro/command.go +++ b/cmd/micro/command.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "os" "os/exec" "regexp" @@ -9,19 +10,26 @@ import ( "github.com/gdamore/tcell" ) +// HandleShellCommand runs the shell command and outputs to DisplayBlock func HandleShellCommand(input string, view *View) { inputCmd := strings.Split(input, " ")[0] args := strings.Split(input, " ")[1:] // Execute Command - cmdout := exec.Command(inputCmd, args...) - output, _ := cmdout.CombinedOutput() - outstring := string(output) + cmd := exec.Command(inputCmd, args...) + outputBytes := &bytes.Buffer{} + + cmd.Stdout = outputBytes // send output to buffer + cmd.Start() + cmd.Wait() // wait for command to finish + outstring := outputBytes.String() totalLines := strings.Split(outstring, "\n") - if len(totalLines) == 2 { + + if len(totalLines) < 3 { messenger.Message(outstring) return } + if outstring != "" { // Display nonblank output DisplayBlock(outstring) @@ -58,6 +66,16 @@ func DisplayBlock(text string) { _, height = e.Size() case *tcell.EventKey: switch e.Key() { + case tcell.KeyPgUp: + if topline > height { + topline = topline - height + } else { + topline = 0 + } + case tcell.KeyPgDn: + if topline < len(totalLines)-height { + topline = topline + height + } case tcell.KeyUp: if topline > 0 { topline-- @@ -68,6 +86,8 @@ func DisplayBlock(text string) { } case tcell.KeyCtrlQ, tcell.KeyCtrlW, tcell.KeyEscape, tcell.KeyCtrlC: return + default: + return } } }