Skip to content

Commit

Permalink
Fixes #8 - Allow breaking out of the UI to run external commands (eg …
Browse files Browse the repository at this point in the history
…vim)

This isn't perfect, but it does work acceptably. By turning off event handlers
(using empty func() as the handler for an event) and by readding echo and opost to the
tty, we get a functional terminal.

(NB: the stty isn't strictly necessary for vim, but does cause a problem if you enter vim
and then get to a shell from inside it. So screw it, make it work for interactive shell too via
:shell command.)
  • Loading branch information
Mike Pountney committed May 10, 2016
1 parent 48910ef commit e1432da
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
2 changes: 2 additions & 0 deletions command_bar_fragment.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func handleCommand(command string) {
if len(command) > 10 {
handleCommentCommand(string(command[9:]))
}
case action == "shell":
runShell()
case action == "search" || action == "search-open" || action == "so":
handleSearchOpen(args)
case action == "search-all" || action == "sa":
Expand Down
2 changes: 1 addition & 1 deletion run.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ Query Options:
}
defer ui.Close()

registerKeyboardHandlers()
registerEventHandlers()

helpPage = new(HelpPage)
commandBar = new(CommandBar)
Expand Down
8 changes: 7 additions & 1 deletion ui_controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
ui "github.com/gizak/termui"
)

func registerKeyboardHandlers() {
func registerEventHandlers() {
ui.Handle("/sys/kbd/", func(ev ui.Event) {
handleAnyKey(ev)
})
Expand All @@ -18,6 +18,12 @@ func registerKeyboardHandlers() {
})
}

func deregisterEventHandlers() {
ui.Handle("/sys/kbd/", func(ev ui.Event) {})
ui.Handle("/sys/kbd/C-c", func(ev ui.Event) {})
ui.Handle("/sys/wnd/resize", func(ev ui.Event) {})
}

func handleLabelViewKey() {
switch page := currentPage.(type) {
case *TicketListPage:
Expand Down
43 changes: 37 additions & 6 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strings"

Expand Down Expand Up @@ -38,13 +39,43 @@ func countLabelsFromQueryData(data interface{}) map[string]int {
return counts
}

func RunExternalCommand(fn func() error) error {
log.Debugf("ShellOut() called with %q", fn)
deregisterEventHandlers()
ui.Clear()
stty := exec.Command("stty", "-f", "/dev/tty", "echo", "opost")
_ = stty.Run()
err := fn() // magic happens
stty = exec.Command("stty", "-f", "/dev/tty", "-echo", "-opost")
_ = stty.Run()
registerEventHandlers()
if err != nil {
return err
}
return nil
}

func runShell() {
_ = RunExternalCommand(
func() error {
cmd := exec.Command("bash")
cmd.Stdout, cmd.Stderr, cmd.Stdin = os.Stdout, os.Stderr, os.Stdin
return cmd.Run()
})
}

func runJiraCmdEdit(ticketId string) {
opts := getJiraOpts()
c := jira.New(opts)
ui.Close()
c.CmdEdit(ticketId)
log.Notice("Regrettably, need to exit after edit. See https://github.com/mikepea/go-jira-ui/issues/8")
os.Exit(0)
_ = RunExternalCommand(
func() error {
opts := getJiraOpts()
c := jira.New(opts)
return c.CmdEdit(ticketId)
})
switch c := currentPage.(type) {
case Refresher:
c.Refresh()
}
changePage()
}

func runJiraCmdCommentNoEditor(ticketId string, comment string) {
Expand Down

0 comments on commit e1432da

Please sign in to comment.