gum/pager/pager.go

143 lines
3.8 KiB
Go
Raw Normal View History

// 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/lipgloss"
"github.com/muesli/reflow/truncate"
)
type model struct {
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
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:
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
m.ProcessText(msg)
case tea.KeyMsg:
return m.KeyHandler(msg)
}
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
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(" │ ")
}
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
}
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
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 := truncate.String(line, uint(m.maxWidth))
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
text.WriteString(textStyle.Render(truncatedLine))
text.WriteString("\n")
if m.showLineNumbers {
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
text.WriteString(m.lineNumberStyle.Render(" │ "))
}
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
line = strings.Replace(line, truncatedLine, "", 1)
}
text.WriteString(textStyle.Render(truncate.String(line, uint(m.maxWidth))))
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
text.WriteString("\n")
}
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
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
const HeightOffset = 2
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
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 + HeightOffset, Width: m.viewport.Width})
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
} else {
m.search.Done()
}
case "ctrl+d", "ctrl+c", "esc":
m.search.Done()
default:
m.search.input, cmd = m.search.input.Update(key)
}
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
} else {
switch key.String() {
case "g":
m.viewport.GotoTop()
case "G":
m.viewport.GotoBottom()
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
case "/":
m.search.Begin()
case "p", "N":
m.search.PrevMatch(&m)
m.ProcessText(tea.WindowSizeMsg{Height: m.viewport.Height + HeightOffset, Width: m.viewport.Width})
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
case "n":
m.search.NextMatch(&m)
m.ProcessText(tea.WindowSizeMsg{Height: m.viewport.Height + HeightOffset, Width: m.viewport.Width})
case "q", "ctrl+c", "esc":
return m, tea.Quit
}
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
m.viewport, cmd = m.viewport.Update(key)
}
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
return m, cmd
}
func (m model) View() string {
feature(pager): add search functionality (#321) * 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>
2023-05-15 05:19:07 +02:00
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)
}