Skip to content

Commit

Permalink
truncate long items in dependencies panel
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Apr 13, 2020
1 parent 885c325 commit bfc1d02
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ func KindFlags() []KindFlag {
}
}

func (d *Dependency) kindKey() string {
func (d *Dependency) KindKey() string {
return KindKeyMap()[d.Kind]
}
2 changes: 1 addition & 1 deletion pkg/commands/npm_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (m *NpmManager) EditDepConstraint(dep *Dependency, packageJsonPath string,
return err
}

updatedConfig, err := jsonparser.Set(config, jsonStringValue(constraint), dep.kindKey(), dep.Name)
updatedConfig, err := jsonparser.Set(config, jsonStringValue(constraint), dep.KindKey(), dep.Name)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/gui/dependencies_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func (gui *Gui) handleDepSelect(g *gocui.Gui, v *gocui.View) error {
}
if dep.PackageConfig != nil {
summary := presentation.PackageSummary(*dep.PackageConfig)
summary = fmt.Sprintf("%s\nConstraint: %s", summary, utils.ColoredString(dep.Constraint, color.FgMagenta))
summary = fmt.Sprintf("%s\nType: %s", summary, utils.ColoredString(dep.KindKey(), presentation.KindColor(dep.Kind)))
if dep.Linked() {
summary = fmt.Sprintf("%s\nLinked to: %s", summary, utils.ColoredString(dep.LinkPath, color.FgCyan))
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/gui/global_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ import (
func (gui *Gui) nextScreenMode(g *gocui.Gui, v *gocui.View) error {
gui.State.ScreenMode = utils.NextIntInCycle([]int{SCREEN_NORMAL, SCREEN_HALF, SCREEN_FULL}, gui.State.ScreenMode)

gui.refreshListViews()

return nil
}

func (gui *Gui) prevScreenMode(g *gocui.Gui, v *gocui.View) error {
gui.State.ScreenMode = utils.PrevIntInCycle([]int{SCREEN_NORMAL, SCREEN_HALF, SCREEN_FULL}, gui.State.ScreenMode)

gui.refreshListViews()

return nil
}

Expand Down
21 changes: 14 additions & 7 deletions pkg/gui/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,33 @@ func (gui *Gui) getViewHeights() map[string]int {
return vHeights
}

func (gui *Gui) getMainViewDimensions() (int, int, int, int, error) {
width, height := gui.g.Size()
func (gui *Gui) getLeftSideWidth() int {
width, _ := gui.g.Size()

sidePanelWidthRatio := gui.Config.GetUserConfig().GetFloat64("gui.sidePanelWidth")

var leftSideWidth int
switch gui.State.ScreenMode {
case SCREEN_NORMAL:
leftSideWidth = int(float64(width) * sidePanelWidthRatio)
return int(float64(width) * sidePanelWidthRatio)
case SCREEN_HALF:
leftSideWidth = width/2 - 2
return width/2 - 2
case SCREEN_FULL:
currentView := gui.g.CurrentView()
if currentView != nil && currentView.Name() == "main" {
leftSideWidth = 0
return 0
} else {
leftSideWidth = width - 1
return width - 1
}
}

return 0
}

func (gui *Gui) getMainViewDimensions() (int, int, int, int, error) {
width, height := gui.g.Size()

leftSideWidth := gui.getLeftSideWidth()

mainPanelLeft := leftSideWidth + 1
mainPanelRight := width - 1
mainPanelTop := 6
Expand Down
12 changes: 8 additions & 4 deletions pkg/gui/packages_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,16 @@ func (gui *Gui) refreshPackages() error {
return err
}

gui.refreshListViews()

return nil
}

func (gui *Gui) refreshListViews() {
displayStrings := presentation.GetPackageListDisplayStrings(gui.State.Packages, gui.linkPathMap(), gui.State.CommandViewMap)
gui.renderDisplayStrings(packagesView, displayStrings)
gui.renderDisplayStrings(gui.getPackagesView(), displayStrings)

displayStrings = presentation.GetDependencyListDisplayStrings(gui.State.Deps, gui.State.CommandViewMap)
displayStrings = presentation.GetDependencyListDisplayStrings(gui.State.Deps, gui.State.CommandViewMap, gui.getLeftSideWidth() > 70)
gui.renderDisplayStrings(gui.getDepsView(), displayStrings)

displayStrings = presentation.GetScriptListDisplayStrings(gui.getScripts(), gui.State.CommandViewMap)
Expand All @@ -73,8 +79,6 @@ func (gui *Gui) refreshPackages() error {
gui.renderDisplayStrings(gui.getTarballsView(), displayStrings)

gui.refreshStatus()

return nil
}

func (gui *Gui) currentPackage() *commands.Package {
Expand Down
34 changes: 24 additions & 10 deletions pkg/gui/presentation/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ import (
"github.com/jesseduffield/semver/v3"
)

func GetDependencyListDisplayStrings(dependencies []*commands.Dependency, commandMap commands.CommandViewMap) [][]string {
func GetDependencyListDisplayStrings(dependencies []*commands.Dependency, commandMap commands.CommandViewMap, wide bool) [][]string {
lines := make([][]string, len(dependencies))

for i := range dependencies {
dep := dependencies[i]
lines[i] = getDepDisplayStrings(dep, commandMap[dep.ID()])
lines[i] = getDepDisplayStrings(dep, commandMap[dep.ID()], wide)
}

return lines
}

func getDepDisplayStrings(d *commands.Dependency, commandView *commands.CommandView) []string {

func getDepDisplayStrings(d *commands.Dependency, commandView *commands.CommandView, wide bool) []string {
localVersionCol := ""
if d.Linked() {
localVersionCol = utils.ColoredString("linked: "+d.LinkPath, color.FgCyan)
Expand All @@ -37,14 +36,20 @@ func getDepDisplayStrings(d *commands.Dependency, commandView *commands.CommandV
localVersionCol = utils.ColoredString("missing", color.FgRed)
}

kindColorMap := map[string]color.Attribute{
"prod": color.FgYellow,
"dev": color.FgGreen,
"peer": color.FgMagenta,
"optional": theme.DefaultTextColor,
return []string{
commandView.Status(),
utils.ColoredString(truncateWithEllipsis(d.Name, 30, wide), KindColor(d.Kind)),
utils.ColoredString(truncateWithEllipsis(d.Constraint, 20, wide), color.FgMagenta),
localVersionCol,
}
}

func truncateWithEllipsis(str string, limit int, wide bool) string {
if wide {
return str
}

return []string{commandView.Status(), d.Name, utils.ColoredString(d.Kind, kindColorMap[d.Kind]), utils.ColoredString(d.Constraint, color.FgMagenta), localVersionCol}
return utils.TruncateWithEllipsis(str, limit)
}

func statusMap() map[int]string {
Expand Down Expand Up @@ -73,3 +78,12 @@ func semverStatus(version, constraint string) (int, bool) {

return status, status == semver.GOOD
}

func KindColor(kind string) color.Attribute {
return map[string]color.Attribute{
"prod": theme.DefaultTextColor,
"dev": color.FgGreen,
"optional": color.FgCyan,
"peer": color.FgMagenta,
}[kind]
}

0 comments on commit bfc1d02

Please sign in to comment.