Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Apr 10, 2020
1 parent 10ef521 commit 7875327
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 6 deletions.
1 change: 1 addition & 0 deletions pkg/config/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ keybinding:
toggleDragSelect-alt: 'V'
packages:
link: 'l'
build: 'b'
`)
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/gui/keybindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,12 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
Modifier: gocui.ModNone,
Handler: gui.wrappedHandler(gui.handleInstall),
},
{
ViewName: "packages",
Key: gui.getKey("packages.build"),
Modifier: gocui.ModNone,
Handler: gui.wrappedHandler(gui.handleBuild),
},
}

for _, viewName := range []string{"status", "packages", "deps", "scripts", "menu"} {
Expand Down
48 changes: 45 additions & 3 deletions pkg/gui/packages_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,58 @@ func (gui *Gui) handleLinkPackage(g *gocui.Gui, v *gocui.View) error {
}

cmd := gui.OSCommand.ExecutableFromString(cmdStr)
if err := gui.newPtyTask("main", cmd); err != nil {
if err := gui.newPtyTask("main", cmd, cmdStr); err != nil {
gui.Log.Error(err)
}

return nil
}

func (gui *Gui) handleInstall() error {
cmd := gui.OSCommand.ExecutableFromString("npm install")
if err := gui.newPtyTask("main", cmd); err != nil {
selectedPkg := gui.getSelectedPackage()
if selectedPkg == nil {
return nil
}

currentPkg := gui.currentPackage()
if currentPkg == nil {
return nil
}

var cmdStr string
if selectedPkg == currentPkg {
cmdStr = "npm install"
} else {
cmdStr = "npm install --prefix " + selectedPkg.Path
}

cmd := gui.OSCommand.ExecutableFromString(cmdStr)
if err := gui.newPtyTask("main", cmd, cmdStr); err != nil {
gui.Log.Error(err)
}
return nil
}

func (gui *Gui) handleBuild() error {
selectedPkg := gui.getSelectedPackage()
if selectedPkg == nil {
return nil
}

currentPkg := gui.currentPackage()
if currentPkg == nil {
return nil
}

var cmdStr string
if selectedPkg == currentPkg {
cmdStr = "npm run build"
} else {
cmdStr = "npm run build --prefix " + selectedPkg.Path
}

cmd := gui.OSCommand.ExecutableFromString(cmdStr)
if err := gui.newPtyTask("main", cmd, cmdStr); err != nil {
gui.Log.Error(err)
}
return nil
Expand Down
9 changes: 8 additions & 1 deletion pkg/gui/pty.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
package gui

import (
"fmt"
"io"
"os/exec"

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

func (gui *Gui) onResize() error {
Expand All @@ -31,7 +34,7 @@ func (gui *Gui) onResize() error {
// which is just an io.Reader. the pty package lets us wrap a command in a
// pseudo-terminal meaning we'll get the behaviour we want from the underlying
// command.
func (gui *Gui) newPtyTask(viewName string, cmd *exec.Cmd) error {
func (gui *Gui) newPtyTask(viewName string, cmd *exec.Cmd, cmdStr string) error {
go func() {
view, err := gui.g.View(viewName)
if err != nil {
Expand All @@ -40,6 +43,8 @@ func (gui *Gui) newPtyTask(viewName string, cmd *exec.Cmd) error {

view.Clear()

fmt.Fprint(view, utils.ColoredString(fmt.Sprintf("+ %s\n\n", cmdStr), color.FgYellow))

// _, height := view.Size()
// _, oy := view.Origin()

Expand Down Expand Up @@ -68,6 +73,8 @@ func (gui *Gui) newPtyTask(viewName string, cmd *exec.Cmd) error {

_, _ = io.Copy(view, ptmx)

fmt.Fprint(view, utils.ColoredString("\ncommand has completed", color.FgGreen))

onClose()

// if err := manager.NewTask(manager.NewCmdTask(ptmx, cmd, height+oy+10, onClose)); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/pty_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ func (gui *Gui) onResize() error {
return nil
}

func (gui *Gui) newPtyTask(viewName string, cmd *exec.Cmd) error {
func (gui *Gui) newPtyTask(viewName string, cmd *exec.Cmd, cmdStr string) error {
return gui.newCmdTask(viewName, cmd)
}
2 changes: 1 addition & 1 deletion pkg/gui/view_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (gui *Gui) renderGlobalOptions() error {
fmt.Sprintf("%s %s %s %s", gui.getKeyDisplay("universal.prevBlock"), gui.getKeyDisplay("universal.nextBlock"), gui.getKeyDisplay("universal.prevItem"), gui.getKeyDisplay("universal.nextItem")): gui.Tr.SLocalize("navigate"),
fmt.Sprintf("%s/%s", gui.getKeyDisplay("universal.return"), gui.getKeyDisplay("universal.quit")): gui.Tr.SLocalize("close"),
gui.getKeyDisplay("universal.optionMenu"): gui.Tr.SLocalize("menu"),
"1-5": gui.Tr.SLocalize("jump"),
"1-4": gui.Tr.SLocalize("jump"),
})
}

Expand Down

0 comments on commit 7875327

Please sign in to comment.