e47f9889ca93575661518610a368bfa9fc572e22

Author
Ayman Bagabas <ayman.bagabas@gmail.com>
Committer
Ayman Bagabas <ayman.bagabas@gmail.com>
Date

Message

feat(ui): navigate to top/botton of readme and files

Diff

  1diff --git a/ui/components/code/code.go b/ui/components/code/code.go
  2index 8b442db8209023b34cc2167f43477ef0a16a2286..44aec42f84d3a681de75fff5150f5205f3ef7a75 100644
  3--- a/ui/components/code/code.go
  4+++ b/ui/components/code/code.go
  5@@ -6,6 +6,7 @@ import (
  6 	"sync"
  7 
  8 	"github.com/alecthomas/chroma/lexers"
  9+	"github.com/charmbracelet/bubbles/key"
 10 	tea "github.com/charmbracelet/bubbletea"
 11 	"github.com/charmbracelet/glamour"
 12 	gansi "github.com/charmbracelet/glamour/ansi"
 13@@ -98,10 +99,18 @@ func (r *Code) Init() tea.Cmd {
 14 // Update implements tea.Model.
 15 func (r *Code) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 16 	cmds := make([]tea.Cmd, 0)
 17-	switch msg.(type) {
 18+	switch msg := msg.(type) {
 19 	case tea.WindowSizeMsg:
 20 		// Recalculate content width and line wrap.
 21 		cmds = append(cmds, r.Init())
 22+	case tea.KeyMsg:
 23+		// Viewport doesn't handle these keys, so we do it here.
 24+		switch {
 25+		case key.Matches(msg, r.common.KeyMap.GotoTop):
 26+			r.GotoTop()
 27+		case key.Matches(msg, r.common.KeyMap.GotoBottom):
 28+			r.GotoBottom()
 29+		}
 30 	}
 31 	v, cmd := r.Viewport.Update(msg)
 32 	r.Viewport = v.(*vp.Viewport)
 33diff --git a/ui/keymap/keymap.go b/ui/keymap/keymap.go
 34index cf2cc2f74c166b0c628360b379c845613f79d3e9..ae09eaad7344c615d91fd92c92c1b36d634341a1 100644
 35--- a/ui/keymap/keymap.go
 36+++ b/ui/keymap/keymap.go
 37@@ -4,18 +4,20 @@ import "github.com/charmbracelet/bubbles/key"
 38 
 39 // KeyMap is a map of key bindings for the UI.
 40 type KeyMap struct {
 41-	Quit      key.Binding
 42-	Up        key.Binding
 43-	Down      key.Binding
 44-	UpDown    key.Binding
 45-	LeftRight key.Binding
 46-	Arrows    key.Binding
 47-	Select    key.Binding
 48-	Section   key.Binding
 49-	Back      key.Binding
 50-	PrevPage  key.Binding
 51-	NextPage  key.Binding
 52-	Help      key.Binding
 53+	Quit       key.Binding
 54+	Up         key.Binding
 55+	Down       key.Binding
 56+	UpDown     key.Binding
 57+	LeftRight  key.Binding
 58+	Arrows     key.Binding
 59+	GotoTop    key.Binding
 60+	GotoBottom key.Binding
 61+	Select     key.Binding
 62+	Section    key.Binding
 63+	Back       key.Binding
 64+	PrevPage   key.Binding
 65+	NextPage   key.Binding
 66+	Help       key.Binding
 67 
 68 	SelectItem key.Binding
 69 	BackItem   key.Binding
 70@@ -103,6 +105,28 @@ func DefaultKeyMap() *KeyMap {
 71 		),
 72 	)
 73 
 74+	km.GotoTop = key.NewBinding(
 75+		key.WithKeys(
 76+			"home",
 77+			"g",
 78+		),
 79+		key.WithHelp(
 80+			"g/home",
 81+			"goto top",
 82+		),
 83+	)
 84+
 85+	km.GotoBottom = key.NewBinding(
 86+		key.WithKeys(
 87+			"end",
 88+			"G",
 89+		),
 90+		key.WithHelp(
 91+			"G/end",
 92+			"goto bottom",
 93+		),
 94+	)
 95+
 96 	km.Select = key.NewBinding(
 97 		key.WithKeys(
 98 			"enter",
 99diff --git a/ui/pages/repo/files.go b/ui/pages/repo/files.go
100index a281c1e879c932daacae7c8491d9e3966687b4bb..f519c558602aaf3169063e50df739b03557b13b4 100644
101--- a/ui/pages/repo/files.go
102+++ b/ui/pages/repo/files.go
103@@ -131,9 +131,9 @@ func (f *Files) ShortHelp() []key.Binding {
104 // FullHelp implements help.KeyMap.
105 func (f *Files) FullHelp() [][]key.Binding {
106 	b := make([][]key.Binding, 0)
107+	copyKey := f.common.KeyMap.Copy
108 	switch f.activeView {
109 	case filesViewFiles:
110-		copyKey := f.common.KeyMap.Copy
111 		copyKey.SetHelp("c", "copy name")
112 		k := f.selector.KeyMap
113 		b = append(b, []key.Binding{
114@@ -154,7 +154,6 @@ func (f *Files) FullHelp() [][]key.Binding {
115 			},
116 		}...)
117 	case filesViewContent:
118-		copyKey := f.common.KeyMap.Copy
119 		copyKey.SetHelp("c", "copy content")
120 		k := f.code.KeyMap
121 		b = append(b, []key.Binding{
122@@ -171,6 +170,8 @@ func (f *Files) FullHelp() [][]key.Binding {
123 		lc := []key.Binding{
124 			k.Down,
125 			k.Up,
126+			f.common.KeyMap.GotoTop,
127+			f.common.KeyMap.GotoBottom,
128 			copyKey,
129 		}
130 		lexer := lexers.Match(f.currentContent.ext)
131diff --git a/ui/pages/repo/readme.go b/ui/pages/repo/readme.go
132index 2743a9f831927f0714b39eb7f1caa9abe1fbfbec..48cf849f24e017e0be445e844afebae780de8c62 100644
133--- a/ui/pages/repo/readme.go
134+++ b/ui/pages/repo/readme.go
135@@ -62,6 +62,8 @@ func (r *Readme) FullHelp() [][]key.Binding {
136 		{
137 			k.Down,
138 			k.Up,
139+			r.common.KeyMap.GotoTop,
140+			r.common.KeyMap.GotoBottom,
141 		},
142 	}
143 	return b