Skip to content

Commit 4cdedc2

Browse files
authored
Merge pull request #1 from nsumbadze/modernize/lazynpm
Modernize/lazynpm
2 parents a4cba1f + a505873 commit 4cdedc2

7 files changed

Lines changed: 117 additions & 11 deletions

File tree

pkg/config/app_config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ func GetDefaultConfig() []byte {
240240
- blue
241241
selectedLineBgColor:
242242
- blue
243+
selectedLineFgColor:
244+
- white
245+
- bold
243246
update:
244247
method: prompt # can be: prompt | background | never
245248
days: 14 # how often a update is checked for

pkg/gui/keybindings_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,41 @@ import (
66
"github.com/jesseduffield/gocui"
77
)
88

9+
func TestOptionsMapToString(t *testing.T) {
10+
gui := &Gui{}
11+
12+
actual := gui.optionsMapToString(map[string]string{
13+
"j/k": "navigate",
14+
"?": "menu",
15+
})
16+
expected := "[?] menu • [j/k] navigate"
17+
18+
if actual != expected {
19+
t.Fatalf("optionsMapToString() = %q, want %q", actual, expected)
20+
}
21+
}
22+
23+
func TestFormatPanelTitle(t *testing.T) {
24+
testCases := []struct {
25+
name string
26+
focused bool
27+
expected string
28+
}{
29+
{name: "focused", focused: true, expected: "> 2 Packages"},
30+
{name: "unfocused", focused: false, expected: " 2 Packages"},
31+
}
32+
33+
for _, testCase := range testCases {
34+
testCase := testCase
35+
t.Run(testCase.name, func(t *testing.T) {
36+
actual := formatPanelTitle(2, "Packages", testCase.focused)
37+
if actual != testCase.expected {
38+
t.Fatalf("formatPanelTitle() = %q, want %q", actual, testCase.expected)
39+
}
40+
})
41+
}
42+
}
43+
944
func TestGetKeyDisplay(t *testing.T) {
1045
testCases := []struct {
1146
name string

pkg/gui/layout.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import (
77
"github.com/jesseduffield/lazynpm/pkg/utils"
88
)
99

10+
// applyListHighlight keeps the focused row readable across all list panels.
11+
// gocui does not automatically use the configured theme value for selected rows.
12+
func (gui *Gui) applyListHighlight(view *gocui.View) {
13+
view.SelBgColor = theme.SelectedLineBgColor
14+
view.SelFgColor = theme.SelectedLineFgColor
15+
}
16+
1017
// getFocusLayout returns a manager function for when view gain and lose focus
1118
func (gui *Gui) getFocusLayout() func(g *gocui.Gui) error {
1219
var previousView *gocui.View
@@ -236,7 +243,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
236243
if err.Error() != "unknown view" {
237244
return err
238245
}
239-
v.Title = gui.Tr.SLocalize("StatusTitle")
246+
v.Title = gui.panelTitle("status", 1, gui.Tr.SLocalize("StatusTitle"))
240247
v.FgColor = textColor
241248
}
242249

@@ -246,39 +253,43 @@ func (gui *Gui) layout(g *gocui.Gui) error {
246253
return err
247254
}
248255
packagesView.Highlight = true
249-
packagesView.Title = gui.Tr.SLocalize("PackagesTitle")
256+
packagesView.Title = gui.panelTitle("packages", 2, gui.Tr.SLocalize("PackagesTitle"))
250257
packagesView.ContainsList = true
251258
}
259+
gui.applyListHighlight(packagesView)
252260

253261
depsView, err := g.SetViewBeneath("deps", "packages", vHeights["deps"])
254262
if err != nil {
255263
if err.Error() != "unknown view" {
256264
return err
257265
}
258-
depsView.Title = gui.Tr.SLocalize("DepsTitle")
266+
depsView.Title = gui.panelTitle("deps", 3, gui.Tr.SLocalize("DepsTitle"))
259267
depsView.FgColor = textColor
260268
depsView.ContainsList = true
261269
}
270+
gui.applyListHighlight(depsView)
262271

263272
scriptsView, err := g.SetViewBeneath("scripts", "deps", vHeights["scripts"])
264273
if err != nil {
265274
if err.Error() != "unknown view" {
266275
return err
267276
}
268-
scriptsView.Title = gui.Tr.SLocalize("ScriptsTitle")
277+
scriptsView.Title = gui.panelTitle("scripts", 4, gui.Tr.SLocalize("ScriptsTitle"))
269278
scriptsView.FgColor = textColor
270279
scriptsView.ContainsList = true
271280
}
281+
gui.applyListHighlight(scriptsView)
272282

273283
tarballsView, err := g.SetViewBeneath("tarballs", "scripts", vHeights["tarballs"])
274284
if err != nil {
275285
if err.Error() != "unknown view" {
276286
return err
277287
}
278-
tarballsView.Title = gui.Tr.SLocalize("TarballsTitle")
288+
tarballsView.Title = gui.panelTitle("tarballs", 5, gui.Tr.SLocalize("TarballsTitle"))
279289
tarballsView.FgColor = textColor
280290
tarballsView.ContainsList = true
281291
}
292+
gui.applyListHighlight(tarballsView)
282293
tarballsView.Visible = gui.showTarballsView()
283294

284295
if v, err := g.SetView("options", appStatusOptionsBoundary-1, height-2, optionsVersionBoundary-1, height, 0); err != nil {

pkg/gui/menu_panel.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func (gui *Gui) createMenu(title string, items []*menuItem, createMenuOptions cr
7878
menuView.Title = title
7979
menuView.FgColor = theme.GocuiDefaultTextColor
8080
menuView.ContainsList = true
81+
gui.applyListHighlight(menuView)
8182
menuView.Clear()
8283
fmt.Fprint(menuView, list)
8384
gui.State.Panels.Menu.SelectedLine = 0

pkg/gui/view_helpers.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,27 @@ func (gui *Gui) renderString(viewName, s string) {
200200
func (gui *Gui) optionsMapToString(optionsMap map[string]string) string {
201201
optionsArray := make([]string, 0)
202202
for key, description := range optionsMap {
203-
optionsArray = append(optionsArray, key+": "+description)
203+
optionsArray = append(optionsArray, fmt.Sprintf("[%s] %s", key, description))
204204
}
205205
sort.Strings(optionsArray)
206-
return strings.Join(optionsArray, ", ")
206+
return strings.Join(optionsArray, " • ")
207+
}
208+
209+
func (gui *Gui) panelTitle(viewName string, number int, title string) string {
210+
currentView := gui.g.CurrentView()
211+
focused := currentView != nil && currentView.Name() == viewName
212+
if currentView == nil {
213+
focused = viewName == "packages"
214+
}
215+
return formatPanelTitle(number, title, focused)
216+
}
217+
218+
func formatPanelTitle(number int, title string, focused bool) string {
219+
marker := " "
220+
if focused {
221+
marker = ">"
222+
}
223+
return fmt.Sprintf("%s %d %s", marker, number, title)
207224
}
208225

209226
func (gui *Gui) renderOptionsMap(optionsMap map[string]string) error {
@@ -328,12 +345,17 @@ func (gui *Gui) renderPanelOptions() error {
328345
}
329346

330347
func (gui *Gui) renderGlobalOptions() error {
348+
jumpRange := "1-4"
349+
if gui.showTarballsView() {
350+
jumpRange = "1-5"
351+
}
352+
331353
return gui.renderOptionsMap(map[string]string{
332354
fmt.Sprintf("%s/%s", gui.getKeyDisplay("universal.scrollUpMain"), gui.getKeyDisplay("universal.scrollDownMain")): gui.Tr.SLocalize("scroll"),
333355
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"),
334356
fmt.Sprintf("%s/%s", gui.getKeyDisplay("universal.return"), gui.getKeyDisplay("universal.quit")): gui.Tr.SLocalize("close"),
335357
gui.getKeyDisplay("universal.optionMenu"): gui.Tr.SLocalize("menu"),
336-
"1-4": gui.Tr.SLocalize("jump"),
358+
jumpRange: gui.Tr.SLocalize("jump"),
337359
})
338360
}
339361

pkg/theme/theme.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ var (
2222
InactiveBorderColor gocui.Attribute
2323

2424
// SelectedLineBgColor is the background color for the selected line
25-
SelectedLineBgColor color.Attribute
25+
SelectedLineBgColor gocui.Attribute
26+
// SelectedLineFgColor is the foreground color for the selected line
27+
SelectedLineFgColor gocui.Attribute
2628

2729
OptionsFgColor color.Attribute
2830

@@ -35,7 +37,12 @@ var (
3537
func UpdateTheme(userConfig *viper.Viper) {
3638
ActiveBorderColor = GetGocuiColor(userConfig.GetStringSlice("gui.theme.activeBorderColor"))
3739
InactiveBorderColor = GetGocuiColor(userConfig.GetStringSlice("gui.theme.inactiveBorderColor"))
38-
SelectedLineBgColor = GetBgColor(userConfig.GetStringSlice("gui.theme.selectedLineBgColor"))
40+
SelectedLineBgColor = GetGocuiBgColor(userConfig.GetStringSlice("gui.theme.selectedLineBgColor"))
41+
selectedLineFgColor := userConfig.GetStringSlice("gui.theme.selectedLineFgColor")
42+
if len(selectedLineFgColor) == 0 {
43+
selectedLineFgColor = []string{"white", "bold"}
44+
}
45+
SelectedLineFgColor = GetGocuiColor(selectedLineFgColor)
3946
OptionsColor = GetGocuiColor(userConfig.GetStringSlice("gui.theme.optionsTextColor"))
4047
OptionsFgColor = GetFgColor(userConfig.GetStringSlice("gui.theme.optionsTextColor"))
4148

@@ -129,6 +136,29 @@ func GetGocuiColor(keys []string) gocui.Attribute {
129136
return attribute
130137
}
131138

139+
// GetGocuiBgColor converts configured background colors to gocui attributes.
140+
func GetGocuiBgColor(keys []string) gocui.Attribute {
141+
colorMap := map[string]gocui.Attribute{
142+
"default": gocui.ColorDefault,
143+
"black": gocui.ColorBlack,
144+
"red": gocui.ColorRed,
145+
"green": gocui.ColorGreen,
146+
"yellow": gocui.ColorYellow,
147+
"blue": gocui.ColorBlue,
148+
"magenta": gocui.ColorMagenta,
149+
"cyan": gocui.ColorCyan,
150+
"white": gocui.ColorWhite,
151+
"bold": gocui.AttrBold,
152+
}
153+
var attribute gocui.Attribute
154+
for _, key := range keys {
155+
if value, present := colorMap[key]; present {
156+
attribute |= value
157+
}
158+
}
159+
return attribute
160+
}
161+
132162
// GetColor bitwise OR's a list of attributes obtained via the given keys
133163
func GetBgColor(keys []string) color.Attribute {
134164
var attribute color.Attribute

vendor/github.com/jesseduffield/gocui/view.go

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)