Skip to content

Commit

Permalink
showing status of a running command
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Apr 12, 2020
1 parent e980428 commit a7287e0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 14 deletions.
31 changes: 31 additions & 0 deletions pkg/commands/command_view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package commands

import (
"os/exec"

"github.com/fatih/color"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazynpm/pkg/utils"
)

type CommandView struct {
// not super keen on having this dependency on gocui here but alas
View *gocui.View
Cmd *exec.Cmd
}

func (cv *CommandView) Status() string {
if cv == nil {
return ""
}

if cv.Cmd.ProcessState == nil {
return utils.ColoredString(utils.Loader(), color.FgCyan)
} else {
if cv.Cmd.ProcessState.Success() {
return utils.ColoredString("!", color.FgGreen)
} else {
return utils.ColoredString("X", color.FgRed)
}
}
}
8 changes: 4 additions & 4 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ type guiState struct {
PrevMainHeight int
OldInformation string
CurrentPackageIdx int
ContextViews map[string]*gocui.View
CommandMap map[string]*commands.CommandView
}

func (gui *Gui) resetState() {
Expand All @@ -145,9 +145,9 @@ func (gui *Gui) resetState() {
Scripts: &scriptsPanelState{SelectedLine: 0},
Menu: &menuPanelState{SelectedLine: 0},
},
SideView: nil,
Ptmx: nil,
ContextViews: map[string]*gocui.View{},
SideView: nil,
Ptmx: nil,
CommandMap: map[string]*commands.CommandView{},
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/gui/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ func (gui *Gui) layout(g *gocui.Gui) error {
v.Autoscroll = true
}

for _, view := range gui.State.ContextViews {
_, _ = g.SetView(view.Name(), mainPanelLeft, mainPanelTop, mainPanelRight, mainPanelBottom, 0)
for _, commandView := range gui.State.CommandMap {
_, _ = g.SetView(commandView.View.Name(), mainPanelLeft, mainPanelTop, mainPanelRight, mainPanelBottom, 0)
}

hiddenViewOffset := 9999
Expand Down
4 changes: 2 additions & 2 deletions pkg/gui/packages_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (gui *Gui) getSelectedPackage() *commands.Package {
}

func (gui *Gui) activateContextView(viewName string) {
if gui.State.ContextViews[viewName] == nil {
if gui.State.CommandMap[viewName] == nil {
viewName = "main"
gui.getMainView().Clear()
}
Expand Down Expand Up @@ -58,7 +58,7 @@ func (gui *Gui) refreshPackages() error {
}

gui.g.Update(func(g *gocui.Gui) error {
displayStrings := presentation.GetPackageListDisplayStrings(gui.State.Packages, gui.linkPathMap())
displayStrings := presentation.GetPackageListDisplayStrings(gui.State.Packages, gui.linkPathMap(), gui.State.CommandMap)
gui.renderDisplayStrings(packagesView, displayStrings)

displayStrings = presentation.GetDependencyListDisplayStrings(gui.State.Deps)
Expand Down
9 changes: 5 additions & 4 deletions pkg/gui/presentation/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import (
"github.com/jesseduffield/lazynpm/pkg/utils"
)

func GetPackageListDisplayStrings(packages []*commands.Package, linkPathMap map[string]bool) [][]string {
func GetPackageListDisplayStrings(packages []*commands.Package, linkPathMap map[string]bool, commandMap map[string]*commands.CommandView) [][]string {
lines := make([][]string, len(packages))

for i := range packages {
pkg := packages[i]
lines[i] = getPackageDisplayStrings(pkg, linkPathMap[pkg.Path])
lines[i] = getPackageDisplayStrings(pkg, linkPathMap[pkg.Path], commandMap[pkg.ID()])
}

return lines
}

func getPackageDisplayStrings(p *commands.Package, linkedToCurrentPackage bool) []string {
func getPackageDisplayStrings(p *commands.Package, linkedToCurrentPackage bool, commandView *commands.CommandView) []string {
attr := theme.DefaultTextColor
if p.LinkedGlobally {
attr = color.FgYellow
Expand All @@ -30,7 +30,8 @@ func getPackageDisplayStrings(p *commands.Package, linkedToCurrentPackage bool)
if linkedToCurrentPackage {
linkedArg = utils.ColoredString("(linked)", color.FgCyan)
}
return []string{line, linkedArg, utils.ColoredString(p.Path, color.FgBlue)}

return []string{commandView.Status(), line, linkedArg, utils.ColoredString(p.Path, color.FgBlue)}
}

func PackageSummary(pkgConfig commands.PackageConfig) string {
Expand Down
1 change: 1 addition & 0 deletions pkg/gui/pty.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (gui *Gui) newPtyTask(viewName string, cmd *exec.Cmd, cmdStr string) error
gui.State.Ptmx = nil
view.Pty = false
view.StdinWriter = nil
_ = cmd.Wait()
}

if err := gui.onResize(); err != nil {
Expand Down
10 changes: 8 additions & 2 deletions pkg/gui/tasks_adapter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gui

import "github.com/jesseduffield/lazynpm/pkg/theme"
import (
"github.com/jesseduffield/lazynpm/pkg/commands"
"github.com/jesseduffield/lazynpm/pkg/theme"
)

func (gui *Gui) newMainCommand(cmdStr string, contextKey string) error {
cmd := gui.OSCommand.ExecutableFromString(cmdStr)
Expand All @@ -24,7 +27,10 @@ func (gui *Gui) newMainCommand(cmdStr string, contextKey string) error {
return err
}

gui.State.ContextViews[contextKey] = v
gui.State.CommandMap[contextKey] = &commands.CommandView{
View: v,
Cmd: cmd,
}

if err := gui.newPtyTask(contextKey, cmd, cmdStr); err != nil {
gui.Log.Error(err)
Expand Down

0 comments on commit a7287e0

Please sign in to comment.