Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pager #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func init() {
// and all subcommands, e.g.:
// showCmd.PersistentFlags().String("foo", "", "A help for foo")

showCmd.Flags().BoolVarP(&shouldShowPlain, "plain", "p", false, "Only print the raw contents, without terminal decorations")
showCmd.Flags().BoolVarP(&shouldShowPlain, "plain", "", false, "Only print the raw contents, without terminal decorations")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
Expand Down
9 changes: 6 additions & 3 deletions internal/changelog/show.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package changelog

import (
"fmt"
// "fmt"
"os"

"github.com/charmbracelet/glamour"
"github.com/niclasvaneyk/keepac/internal/tui"
"golang.org/x/crypto/ssh/terminal"
)

Expand All @@ -27,8 +28,10 @@ func Show(contents string) error {
return err
}

fmt.Print(out)
return nil
return tui.Paginate(out)

// fmt.Print(out)
// return nil
}

func getWordWrapLimit() int {
Expand Down
14 changes: 7 additions & 7 deletions internal/tui/choice.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

var (
titleStyle = lipgloss.NewStyle().MarginLeft(2).MarginTop(1)
choiceTitleStyle = lipgloss.NewStyle().MarginLeft(2).MarginTop(1)
itemStyle = lipgloss.NewStyle().PaddingLeft(4)
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Bold(true)
)
Expand Down Expand Up @@ -45,16 +45,16 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
fmt.Fprint(w, fn(i.value))
}

type model struct {
type choiceModel struct {
list list.Model
onSelect func(item)
}

func (m model) Init() tea.Cmd {
func (m choiceModel) Init() tea.Cmd {
return nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m choiceModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.list.SetWidth(msg.Width)
Expand All @@ -79,7 +79,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, cmd
}

func (m model) View() string {
func (m choiceModel) View() string {
return m.list.View()
}

Expand All @@ -99,11 +99,11 @@ func Choice(title string, options []string) (string, int) {
l.SetShowHelp(false)

l.Title = title
l.Styles.Title = titleStyle
l.Styles.Title = choiceTitleStyle

choice := ""
index := -1
m := model{list: l, onSelect: func(i item) {
m := choiceModel{list: l, onSelect: func(i item) {
choice = i.value
index = i.index
}}
Expand Down
105 changes: 105 additions & 0 deletions internal/tui/pager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package tui

// An example program demonstrating the pager component from the Bubbles
// component library.

import (
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
)

// You generally won't need this unless you're processing stuff with
// complicated ANSI escape sequences. Turn it on if you notice flickering.
//
// Also keep in mind that high performance rendering only works for programs
// that use the full size of the terminal. We're enabling that below with
// tea.EnterAltScreen().
const useHighPerformanceRenderer = false

type pagerModel struct {
content string
ready bool
viewport viewport.Model
}

func (m pagerModel) Init() tea.Cmd {
return nil
}

func (m pagerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)

switch msg := msg.(type) {
case tea.KeyMsg:
if k := msg.String(); k == "ctrl+c" || k == "q" || k == "esc" {
return m, tea.Quit
}

case tea.WindowSizeMsg:
if !m.ready {
// Since this program is using the full size of the viewport we
// need to wait until we've received the window dimensions before
// we can initialize the viewport. The initial dimensions come in
// quickly, though asynchronously, which is why we wait for them
// here.
m.viewport = viewport.New(msg.Width, msg.Height)
m.viewport.HighPerformanceRendering = useHighPerformanceRenderer
m.viewport.SetContent(m.content)
m.ready = true

// This is only necessary for high performance rendering, which in
// most cases you won't need.
//
// Render the viewport one line below the header.
} else {
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height
}

if useHighPerformanceRenderer {
// Render (or re-render) the whole viewport. Necessary both to
// initialize the viewport and when the window is resized.
//
// This is needed for high-performance rendering only.
cmds = append(cmds, viewport.Sync(m.viewport))
}
}

// Handle keyboard and mouse events in the viewport
m.viewport, cmd = m.viewport.Update(msg)
cmds = append(cmds, cmd)

return m, tea.Batch(cmds...)
}

func (m pagerModel) View() string {
if !m.ready {
return "\n Initializing..."
}

return m.viewport.View()
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func Paginate(content string) error {
p := tea.NewProgram(
pagerModel{content: content},
tea.WithAltScreen(), // use the full size of the terminal in its "alternate screen buffer"
tea.WithMouseCellMotion(), // turn on mouse support so we can track the mouse wheel
)

if _, err := p.Run(); err != nil {
return err
}

return nil
}