mirror of
https://github.com/charmbracelet/gum.git
synced 2024-10-26 15:20:05 +03:00
c8710071ad
* Added initial search functionality * Added a handler for the key presses * Added a searchbar at the bottom of the screen * Made search results cycleable by pressing n * correct start pos and ignore keys while searching * fix out of bound error when pressing n * made the matching pattern relative to the current pos * added p for searching for previous match * added highlighting to search matches * dynamically replaced all matches * fixed string highlight issue * fixed truncation issue * small simplifaction in ypos logic * made prev and next behave the same atBottom * simplified logic and fixed linter errors * updated help text * style changes * added comments * fixed truncation issue * fixes infinte loop on very long lines * added simple lipgloss truncate function * updated colors for better contrast * lint fix * initial commit for soft-wrap functionality * linter corrections and added for pager with new model * added generic functions to a utility package * fix soft lint errors * made N match previous as well as p * replaced help text when search is active * ran gofmt -w * reimplemented search and next to enabled support for dynamic highlights * made the highlight move as you progress through the search * simplified highlighter * improvements to the clean up of the highlight function * semi working reverse search * reverse search without highlight * added semi working highlight for reverser search * working version of previous match * fixed issue with single letter matches in next * added support for softwrapping * respond to soft lint warnings * removed unused function * lint * simplified matchers and fixed duplicate highlights * optimisations and change in matching pattern * fixed bug in lipglosspadding and allowed matching 1 etc. * make highlight respect user settings * fixed logic error in slice * made prev match wrap around * fix: show next/prev match help when active * updated how view port line is set * avoid crashes when regex doesn't compile * fix: spelling previous --------- Co-authored-by: Maas Lalani <maas@lalani.dev>
142 lines
3.8 KiB
Go
142 lines
3.8 KiB
Go
// Package pager provides a pager (similar to less) for the terminal.
|
|
//
|
|
// $ cat file.txt | gum page
|
|
package pager
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/charmbracelet/bubbles/viewport"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/gum/internal/utils"
|
|
"github.com/charmbracelet/lipgloss"
|
|
)
|
|
|
|
type model struct {
|
|
content string
|
|
origContent string
|
|
viewport viewport.Model
|
|
helpStyle lipgloss.Style
|
|
showLineNumbers bool
|
|
lineNumberStyle lipgloss.Style
|
|
softWrap bool
|
|
search search
|
|
matchStyle lipgloss.Style
|
|
matchHighlightStyle lipgloss.Style
|
|
maxWidth int
|
|
}
|
|
|
|
func (m model) Init() tea.Cmd {
|
|
return nil
|
|
}
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
switch msg := msg.(type) {
|
|
case tea.WindowSizeMsg:
|
|
m.ProcessText(msg)
|
|
case tea.KeyMsg:
|
|
return m.KeyHandler(msg)
|
|
}
|
|
|
|
return m, nil
|
|
}
|
|
|
|
func (m *model) ProcessText(msg tea.WindowSizeMsg) {
|
|
m.viewport.Height = msg.Height - lipgloss.Height(m.helpStyle.Render("?")) - 1
|
|
m.viewport.Width = msg.Width
|
|
textStyle := lipgloss.NewStyle().Width(m.viewport.Width)
|
|
var text strings.Builder
|
|
|
|
// Determine max width of a line.
|
|
m.maxWidth = m.viewport.Width
|
|
if m.softWrap {
|
|
vpStyle := m.viewport.Style
|
|
m.maxWidth -= vpStyle.GetHorizontalBorderSize() + vpStyle.GetHorizontalMargins() + vpStyle.GetHorizontalPadding()
|
|
if m.showLineNumbers {
|
|
m.maxWidth -= lipgloss.Width(" │ ")
|
|
}
|
|
}
|
|
|
|
for i, line := range strings.Split(m.content, "\n") {
|
|
line = strings.ReplaceAll(line, "\t", " ")
|
|
if m.showLineNumbers {
|
|
text.WriteString(m.lineNumberStyle.Render(fmt.Sprintf("%4d │ ", i+1)))
|
|
}
|
|
for m.softWrap && lipgloss.Width(line) > m.maxWidth {
|
|
truncatedLine := utils.LipglossTruncate(line, m.maxWidth)
|
|
text.WriteString(textStyle.Render(truncatedLine))
|
|
text.WriteString("\n")
|
|
if m.showLineNumbers {
|
|
text.WriteString(m.lineNumberStyle.Render(" │ "))
|
|
}
|
|
line = strings.Replace(line, truncatedLine, "", 1)
|
|
}
|
|
text.WriteString(textStyle.Render(utils.LipglossTruncate(line, m.maxWidth)))
|
|
text.WriteString("\n")
|
|
}
|
|
|
|
diffHeight := m.viewport.Height - lipgloss.Height(text.String())
|
|
if diffHeight > 0 && m.showLineNumbers {
|
|
remainingLines := " ~ │ " + strings.Repeat("\n ~ │ ", diffHeight-1)
|
|
text.WriteString(m.lineNumberStyle.Render(remainingLines))
|
|
}
|
|
m.viewport.SetContent(text.String())
|
|
}
|
|
|
|
func (m model) KeyHandler(key tea.KeyMsg) (model, func() tea.Msg) {
|
|
var cmd tea.Cmd
|
|
if m.search.active {
|
|
switch key.String() {
|
|
case "enter":
|
|
if m.search.input.Value() != "" {
|
|
m.content = m.origContent
|
|
m.search.Execute(&m)
|
|
|
|
// Trigger a view update to highlight the found matches.
|
|
m.search.NextMatch(&m)
|
|
m.ProcessText(tea.WindowSizeMsg{Height: m.viewport.Height + 2, Width: m.viewport.Width})
|
|
} else {
|
|
m.search.Done()
|
|
}
|
|
case "ctrl+d", "ctrl+c", "esc":
|
|
m.search.Done()
|
|
default:
|
|
m.search.input, cmd = m.search.input.Update(key)
|
|
}
|
|
} else {
|
|
switch key.String() {
|
|
case "g":
|
|
m.viewport.GotoTop()
|
|
case "G":
|
|
m.viewport.GotoBottom()
|
|
case "/":
|
|
m.search.Begin()
|
|
case "p", "N":
|
|
m.search.PrevMatch(&m)
|
|
m.ProcessText(tea.WindowSizeMsg{Height: m.viewport.Height + 2, Width: m.viewport.Width})
|
|
case "n":
|
|
m.search.NextMatch(&m)
|
|
m.ProcessText(tea.WindowSizeMsg{Height: m.viewport.Height + 2, Width: m.viewport.Width})
|
|
case "q", "ctrl+c", "esc":
|
|
return m, tea.Quit
|
|
}
|
|
m.viewport, cmd = m.viewport.Update(key)
|
|
}
|
|
|
|
return m, cmd
|
|
}
|
|
|
|
func (m model) View() string {
|
|
helpMsg := "\n ↑/↓: Navigate • q: Quit • /: Search "
|
|
if m.search.query != nil {
|
|
helpMsg += "• n: Next Match "
|
|
helpMsg += "• N: Prev Match "
|
|
}
|
|
if m.search.active {
|
|
return m.viewport.View() + "\n " + m.search.input.View()
|
|
}
|
|
|
|
return m.viewport.View() + m.helpStyle.Render(helpMsg)
|
|
}
|