From d0aa2213b726b10a93b94317f1cf550e97582e8e Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Tue, 14 Apr 2020 21:33:42 +1000 Subject: [PATCH] switch to new dependency when changing type --- pkg/gui/dependencies_panel.go | 27 ++++++++++++++++++++------- pkg/gui/packages_panel.go | 15 +++++++-------- pkg/gui/pty.go | 13 ++++++++++--- pkg/gui/scripts_panel.go | 2 +- pkg/gui/tarballs_panel.go | 2 +- 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/pkg/gui/dependencies_panel.go b/pkg/gui/dependencies_panel.go index 60ed9fe..d4616ef 100644 --- a/pkg/gui/dependencies_panel.go +++ b/pkg/gui/dependencies_panel.go @@ -54,12 +54,12 @@ func (gui *Gui) linkPathMap() map[string]bool { func (gui *Gui) handleDepInstall(dep *commands.Dependency) error { cmdStr := fmt.Sprintf("npm install %s", dep.Name) - return gui.newMainCommand(cmdStr, dep.ID()) + return gui.newMainCommand(cmdStr, dep.ID(), newMainCommandOptions{}) } func (gui *Gui) handleDepUpdate(dep *commands.Dependency) error { cmdStr := fmt.Sprintf("npm update %s", dep.Name) - return gui.newMainCommand(cmdStr, dep.ID()) + return gui.newMainCommand(cmdStr, dep.ID(), newMainCommandOptions{}) } func (gui *Gui) handleOpenDepPackageConfig(dep *commands.Dependency) error { @@ -81,7 +81,7 @@ func (gui *Gui) handleDepUninstall(dep *commands.Dependency) error { { displayStrings: []string{"uninstall", utils.ColoredString(uninstallStr, color.FgYellow)}, onPress: func() error { - return gui.newMainCommand(uninstallStr, dep.ID()) + return gui.newMainCommand(uninstallStr, dep.ID(), newMainCommandOptions{}) }, }, } @@ -99,13 +99,13 @@ func (gui *Gui) handleDepUninstall(dep *commands.Dependency) error { { displayStrings: []string{"uninstall and save", utils.ColoredString(uninstallAndSaveCmdStr, color.FgYellow)}, onPress: func() error { - return gui.newMainCommand(uninstallAndSaveCmdStr, dep.ID()) + return gui.newMainCommand(uninstallAndSaveCmdStr, dep.ID(), newMainCommandOptions{}) }, }, { displayStrings: []string{"just uninstall", utils.ColoredString(uninstallCmdStr, color.FgYellow)}, onPress: func() error { - return gui.newMainCommand(uninstallCmdStr, dep.ID()) + return gui.newMainCommand(uninstallCmdStr, dep.ID(), newMainCommandOptions{}) }, }, } @@ -144,7 +144,15 @@ func (gui *Gui) handleChangeDepType(dep *commands.Dependency) error { menuItems = append(menuItems, &menuItem{ displayStrings: []string{kindKeyMap[kindFlag.Kind], utils.ColoredString(cmdStr, color.FgYellow)}, onPress: func() error { - return gui.newMainCommand(cmdStr, dep.ID()) + return gui.newMainCommand(cmdStr, dep.ID(), newMainCommandOptions{onSuccess: func() { + for i, newDep := range gui.State.Deps { + if newDep.Name == dep.Name && newDep.Kind == kindFlag.Kind { + gui.State.Panels.Deps.SelectedLine = i + gui.refreshDepsView() + break + } + } + }}) }, }) } @@ -158,7 +166,7 @@ func (gui *Gui) handleAddDependency(dep *commands.Dependency) error { prompt := func(cmdStr string) error { return gui.createPromptPanel(gui.getDepsView(), "enter dependency name", "", func(input string) error { newCmdStr := fmt.Sprintf("%s %s", cmdStr, input) - return gui.newMainCommand(newCmdStr, dep.ID()) + return gui.newMainCommand(newCmdStr, dep.ID(), newMainCommandOptions{}) }) } @@ -186,3 +194,8 @@ func (gui *Gui) handleEditDepConstraint(dep *commands.Dependency) error { return gui.finalStep(gui.NpmManager.EditDepConstraint(dep, packageConfigPath, input)) }) } + +func (gui *Gui) refreshDepsView() { + displayStrings := presentation.GetDependencyListDisplayStrings(gui.State.Deps, gui.State.CommandViewMap, gui.getLeftSideWidth() > 70) + gui.renderDisplayStrings(gui.getDepsView(), displayStrings) +} diff --git a/pkg/gui/packages_panel.go b/pkg/gui/packages_panel.go index 34d75f6..71428ab 100644 --- a/pkg/gui/packages_panel.go +++ b/pkg/gui/packages_panel.go @@ -69,8 +69,7 @@ func (gui *Gui) refreshListViews() { displayStrings := presentation.GetPackageListDisplayStrings(gui.State.Packages, gui.linkPathMap(), gui.State.CommandViewMap) gui.renderDisplayStrings(gui.getPackagesView(), displayStrings) - displayStrings = presentation.GetDependencyListDisplayStrings(gui.State.Deps, gui.State.CommandViewMap, gui.getLeftSideWidth() > 70) - gui.renderDisplayStrings(gui.getDepsView(), displayStrings) + gui.refreshDepsView() displayStrings = presentation.GetScriptListDisplayStrings(gui.getScripts(), gui.State.CommandViewMap) gui.renderDisplayStrings(gui.getScriptsView(), displayStrings) @@ -147,7 +146,7 @@ func (gui *Gui) handleLinkPackage() error { } } - return gui.newMainCommand(cmdStr, selectedPkg.ID()) + return gui.newMainCommand(cmdStr, selectedPkg.ID(), newMainCommandOptions{}) } func (gui *Gui) handleGlobalLinkPackage(pkg *commands.Package) error { @@ -162,7 +161,7 @@ func (gui *Gui) handleGlobalLinkPackage(pkg *commands.Package) error { cmdStr = "npm link" } - return gui.newMainCommand(cmdStr, pkg.ID()) + return gui.newMainCommand(cmdStr, pkg.ID(), newMainCommandOptions{}) } func (gui *Gui) handleInstall(pkg *commands.Package) error { @@ -173,7 +172,7 @@ func (gui *Gui) handleInstall(pkg *commands.Package) error { cmdStr = "npm install --prefix " + pkg.Path } - return gui.newMainCommand(cmdStr, pkg.ID()) + return gui.newMainCommand(cmdStr, pkg.ID(), newMainCommandOptions{}) } func (gui *Gui) handleBuild(pkg *commands.Package) error { @@ -184,7 +183,7 @@ func (gui *Gui) handleBuild(pkg *commands.Package) error { cmdStr = "npm run build --prefix " + pkg.Path } - return gui.newMainCommand(cmdStr, pkg.ID()) + return gui.newMainCommand(cmdStr, pkg.ID(), newMainCommandOptions{}) } func (gui *Gui) handleOpenPackageConfig(pkg *commands.Package) error { @@ -227,7 +226,7 @@ func (gui *Gui) handlePackPackage(pkg *commands.Package) error { cmdStr = fmt.Sprintf("npm pack %s", pkg.Path) } - return gui.newMainCommand(cmdStr, pkg.ID()) + return gui.newMainCommand(cmdStr, pkg.ID(), newMainCommandOptions{}) } func (gui *Gui) selectedPackageID() string { @@ -252,7 +251,7 @@ func (gui *Gui) handlePublish(name string, scoped bool, id string) error { cmdStr = fmt.Sprintf("%s --tag=%s", cmdStr, tag) } cmdStr = fmt.Sprintf("%s %s", cmdStr, name) - return gui.newMainCommand(cmdStr, id) + return gui.newMainCommand(cmdStr, id, newMainCommandOptions{}) }) } diff --git a/pkg/gui/pty.go b/pkg/gui/pty.go index ba137d0..1575e4c 100644 --- a/pkg/gui/pty.go +++ b/pkg/gui/pty.go @@ -14,7 +14,11 @@ import ( "github.com/jesseduffield/lazynpm/pkg/utils" ) -func (gui *Gui) newMainCommand(cmdStr string, contextKey string) error { +type newMainCommandOptions struct { + onSuccess func() +} + +func (gui *Gui) newMainCommand(cmdStr string, contextKey string, opts newMainCommandOptions) error { cmd := gui.OSCommand.ExecutableFromString(cmdStr) mainPanelLeft, mainPanelTop, mainPanelRight, mainPanelBottom, err := gui.getMainViewDimensions() @@ -124,7 +128,7 @@ func (gui *Gui) newMainCommand(cmdStr string, contextKey string) error { gui.State.CommandViewMap[contextKey] = commandView - if err := gui.newPtyTask(contextKey, commandView, cmdStr); err != nil { + if err := gui.newPtyTask(contextKey, commandView, cmdStr, opts); err != nil { gui.Log.Error(err) } @@ -161,7 +165,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, commandView *commands.CommandView, cmdStr string) error { +func (gui *Gui) newPtyTask(viewName string, commandView *commands.CommandView, cmdStr string, opts newMainCommandOptions) error { go func() { view, err := gui.g.View(viewName) if err != nil { @@ -202,6 +206,9 @@ func (gui *Gui) newPtyTask(viewName string, commandView *commands.CommandView, c if commandView.Cancelled { fmt.Fprint(view, utils.ColoredString("\n\ncommand cancelled", color.FgRed)) } else if commandView.Cmd.ProcessState.Success() { + if opts.onSuccess != nil { + opts.onSuccess() + } fmt.Fprint(view, utils.ColoredString("\n\ncommand completed successfully", color.FgGreen)) } else { fmt.Fprint(view, utils.ColoredString("\n\ncommand failed", color.FgRed)) diff --git a/pkg/gui/scripts_panel.go b/pkg/gui/scripts_panel.go index c3d858c..f04cfee 100644 --- a/pkg/gui/scripts_panel.go +++ b/pkg/gui/scripts_panel.go @@ -37,7 +37,7 @@ func (gui *Gui) handleScriptSelect(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) handleRunScript(script *commands.Script) error { return gui.createPromptPanel(gui.getScriptsView(), "run script", fmt.Sprintf("npm run %s", script.Name), func(input string) error { - return gui.newMainCommand(input, script.ID()) + return gui.newMainCommand(input, script.ID(), newMainCommandOptions{}) }) } diff --git a/pkg/gui/tarballs_panel.go b/pkg/gui/tarballs_panel.go index 48f7acd..8299694 100644 --- a/pkg/gui/tarballs_panel.go +++ b/pkg/gui/tarballs_panel.go @@ -69,7 +69,7 @@ func (gui *Gui) handleDeleteTarball(tarball *commands.Tarball) error { func (gui *Gui) handleInstallTarball(tarball *commands.Tarball) error { cmdStr := fmt.Sprintf("npm install %s", tarball.Name) - return gui.newMainCommand(cmdStr, tarball.ID()) + return gui.newMainCommand(cmdStr, tarball.ID(), newMainCommandOptions{}) } func (gui *Gui) handlePublishTarball(tarball *commands.Tarball) error {