feat: using nav bar instead of help
This commit is contained in:
111
main.go
111
main.go
@ -12,8 +12,6 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/charmbracelet/bubbles/help"
|
|
||||||
"github.com/charmbracelet/bubbles/key"
|
|
||||||
"github.com/charmbracelet/bubbles/list"
|
"github.com/charmbracelet/bubbles/list"
|
||||||
"github.com/charmbracelet/bubbles/viewport"
|
"github.com/charmbracelet/bubbles/viewport"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
@ -54,58 +52,6 @@ func getPostWithoutMetaData(post string) (string, error) {
|
|||||||
return strings.Join(splitPost[index+2:], "\n"), nil
|
return strings.Join(splitPost[index+2:], "\n"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type keyMap struct {
|
|
||||||
Up key.Binding
|
|
||||||
Down key.Binding
|
|
||||||
Left key.Binding
|
|
||||||
Right key.Binding
|
|
||||||
Blog key.Binding
|
|
||||||
Help key.Binding
|
|
||||||
Quit key.Binding
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k keyMap) ShortHelp() []key.Binding {
|
|
||||||
return []key.Binding{k.Help, k.Quit}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k keyMap) FullHelp() [][]key.Binding {
|
|
||||||
return [][]key.Binding{
|
|
||||||
{k.Up, k.Down, k.Left, k.Right},
|
|
||||||
{k.Blog, k.Help, k.Quit},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var keys = keyMap{
|
|
||||||
Up: key.NewBinding(
|
|
||||||
key.WithKeys("up", "k"),
|
|
||||||
key.WithHelp("↑/k", "move up"),
|
|
||||||
),
|
|
||||||
Down: key.NewBinding(
|
|
||||||
key.WithKeys("down", "j"),
|
|
||||||
key.WithHelp("↓/j", "move down"),
|
|
||||||
),
|
|
||||||
Left: key.NewBinding(
|
|
||||||
key.WithKeys("left", "h"),
|
|
||||||
key.WithHelp("←/h", "move left"),
|
|
||||||
),
|
|
||||||
Right: key.NewBinding(
|
|
||||||
key.WithKeys("right", "l"),
|
|
||||||
key.WithHelp("→/l", "move right"),
|
|
||||||
),
|
|
||||||
Blog: key.NewBinding(
|
|
||||||
key.WithKeys("b"),
|
|
||||||
key.WithHelp("b", "go to blog"),
|
|
||||||
),
|
|
||||||
Help: key.NewBinding(
|
|
||||||
key.WithKeys("?"),
|
|
||||||
key.WithHelp("?", "toggle help"),
|
|
||||||
),
|
|
||||||
Quit: key.NewBinding(
|
|
||||||
key.WithKeys("q", "esc", "ctrl+c"),
|
|
||||||
key.WithHelp("q", "quit"),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
type Page int
|
type Page int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -118,12 +64,11 @@ type model struct {
|
|||||||
list list.Model
|
list list.Model
|
||||||
allPosts []string
|
allPosts []string
|
||||||
|
|
||||||
keys keyMap
|
|
||||||
help help.Model
|
|
||||||
|
|
||||||
width int
|
width int
|
||||||
height int
|
height int
|
||||||
|
|
||||||
|
navModel navModel
|
||||||
|
|
||||||
frontPageContent string
|
frontPageContent string
|
||||||
frontPageModel postModel
|
frontPageModel postModel
|
||||||
postModel postModel
|
postModel postModel
|
||||||
@ -170,8 +115,8 @@ func initialModel() model {
|
|||||||
list := list.New(listItems, list.NewDefaultDelegate(), w, h)
|
list := list.New(listItems, list.NewDefaultDelegate(), w, h)
|
||||||
list.Title = "Blog Posts"
|
list.Title = "Blog Posts"
|
||||||
|
|
||||||
// -1 to allow for the initial height of the help model
|
// -1 to allow for the initial height of the nav model
|
||||||
vp := getViewPort(w, h-1)
|
vp := getViewPort(w, h-3)
|
||||||
|
|
||||||
// We need to adjust the width of the glamour render from our main width
|
// We need to adjust the width of the glamour render from our main width
|
||||||
// to account for a few things:
|
// to account for a few things:
|
||||||
@ -207,7 +152,18 @@ func initialModel() model {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return model{list: list, allPosts: posts, page: FRONT, termRenderer: renderer, viewport: vp, frontPageModel: frontPageModel, frontPageContent: processedFrontPage, help: help.New(), keys: keys, width: w, height: h}
|
return model{
|
||||||
|
list: list,
|
||||||
|
allPosts: posts,
|
||||||
|
page: FRONT,
|
||||||
|
termRenderer: renderer,
|
||||||
|
viewport: vp,
|
||||||
|
frontPageModel: frontPageModel,
|
||||||
|
frontPageContent: processedFrontPage,
|
||||||
|
width: w,
|
||||||
|
height: h,
|
||||||
|
navModel: createNavModel(w),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) Init() tea.Cmd {
|
func (m model) Init() tea.Cmd {
|
||||||
@ -225,33 +181,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch msg := msg.(type) {
|
|
||||||
case tea.KeyMsg:
|
|
||||||
switch {
|
|
||||||
case key.Matches(msg, m.keys.Help):
|
|
||||||
m.help.ShowAll = !m.help.ShowAll
|
|
||||||
|
|
||||||
adjustedViewportHeight := 0
|
|
||||||
if m.help.ShowAll {
|
|
||||||
adjustedViewportHeight = 4
|
|
||||||
} else {
|
|
||||||
adjustedViewportHeight = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
viewportYOffset := m.frontPageModel.viewport.YOffset
|
|
||||||
m.viewport = getViewPort(m.width, m.height-adjustedViewportHeight)
|
|
||||||
|
|
||||||
frontPageModel, err := createMarkdownPostModel(m.frontPageContent, m.termRenderer, m.viewport)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
m.frontPageModel = frontPageModel
|
|
||||||
m.frontPageModel.viewport.SetYOffset(viewportYOffset)
|
|
||||||
case key.Matches(msg, m.keys.Blog):
|
|
||||||
m.page = POST_LIST
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return m, cmd
|
return m, cmd
|
||||||
case POST_LIST:
|
case POST_LIST:
|
||||||
@ -307,13 +236,11 @@ func (m model) View() string {
|
|||||||
// TODO: better type safety would be to do this over the type.
|
// TODO: better type safety would be to do this over the type.
|
||||||
switch m.page {
|
switch m.page {
|
||||||
case FRONT:
|
case FRONT:
|
||||||
helpView := m.help.View(m.keys)
|
return m.navModel.View() + "\n" + m.frontPageModel.View()
|
||||||
|
|
||||||
return m.frontPageModel.View() + "\n" + helpView
|
|
||||||
case POST_LIST:
|
case POST_LIST:
|
||||||
return docStyle.Render(m.list.View())
|
return m.navModel.View() + docStyle.Render(m.list.View())
|
||||||
case POST:
|
case POST:
|
||||||
return m.postModel.View()
|
return m.navModel.View() + m.postModel.View()
|
||||||
default:
|
default:
|
||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
|
60
nav.go
Normal file
60
nav.go
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
"github.com/charmbracelet/lipgloss"
|
||||||
|
)
|
||||||
|
|
||||||
|
type navModel struct {
|
||||||
|
style lipgloss.Style
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m navModel) Init() tea.Cmd {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m navModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m navModel) View() string {
|
||||||
|
nameStyle := lipgloss.NewStyle().Bold(true)
|
||||||
|
|
||||||
|
blogStyle := lipgloss.NewStyle().
|
||||||
|
Bold(true).
|
||||||
|
Underline(true)
|
||||||
|
|
||||||
|
shortcutStyle := lipgloss.NewStyle().
|
||||||
|
Faint(true)
|
||||||
|
|
||||||
|
name := nameStyle.Render("John Costa")
|
||||||
|
nameShortcut := shortcutStyle.Render("(h)")
|
||||||
|
|
||||||
|
blog := blogStyle.Render("Blog")
|
||||||
|
blogShortcut := shortcutStyle.Render("(b)")
|
||||||
|
|
||||||
|
rightSide := lipgloss.JoinHorizontal(
|
||||||
|
lipgloss.Center,
|
||||||
|
nameShortcut,
|
||||||
|
" ",
|
||||||
|
name,
|
||||||
|
" ",
|
||||||
|
blogShortcut,
|
||||||
|
" ",
|
||||||
|
blog,
|
||||||
|
)
|
||||||
|
|
||||||
|
return m.style.Render(rightSide)
|
||||||
|
}
|
||||||
|
|
||||||
|
func createNavModel(width int) navModel {
|
||||||
|
style := lipgloss.NewStyle().
|
||||||
|
BorderStyle(lipgloss.RoundedBorder()).
|
||||||
|
BorderForeground(lipgloss.Color("62"))
|
||||||
|
|
||||||
|
style = style.Width(width - style.GetHorizontalFrameSize())
|
||||||
|
|
||||||
|
return navModel{
|
||||||
|
style: style,
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user