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

❇️ ability to search in viewport modes using / #161

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
91 changes: 85 additions & 6 deletions pkg/ui/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ var (
)

type model struct {
index string
content string
ready bool
viewport viewport.Model
index string
content string
ready bool
searching bool
searchQuery string
searchIndex int
viewport viewport.Model
}

func (m model) Init() tea.Cmd {
Expand All @@ -44,9 +47,65 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
)

switch msg := msg.(type) {

case tea.KeyMsg:
if k := msg.String(); k == "ctrl+c" || k == "q" || k == "esc" {
return m, tea.Quit
switch msg.String() {
case "ctrl+c", "q", "esc":
if m.searching || m.searchQuery != "" {
m.searching = false
m.searchQuery = ""
m.searchIndex = 0

} else {
return m, tea.Quit
}
case "/":
m.searching = true
m.searchQuery = ""
m.searchIndex = 0

case "backspace":
if m.searching && len(m.searchQuery) > 0 {
m.searchQuery = m.searchQuery[:len(m.searchQuery)-1]
}

case "enter":
if m.searching {
m.searching = false
if m.searchQuery != "" {
m.searchIndex = strings.Index(m.content, m.searchQuery)
if m.searchIndex != -1 {
// Scroll to the search result
lineNumber := strings.Count(m.content[:m.searchIndex], "\n")
m.viewport.GotoTop()
m.viewport.LineDown(lineNumber)
}
}
}

case "n":
if m.searching {
m.searchQuery += "n"
} else if m.searchQuery != "" {
nextIndex := strings.Index(m.content[m.searchIndex+1:], m.searchQuery)
if nextIndex != -1 {
m.searchIndex += nextIndex + 1
// Scroll to the next search result
lineNumber := strings.Count(m.content[:m.searchIndex], "\n")
m.viewport.GotoTop()
m.viewport.LineDown(lineNumber)
} else {
// If not found, wrap around to the beginning
m.searchIndex = strings.Index(m.content, m.searchQuery)
lineNumber := strings.Count(m.content[:m.searchIndex], "\n")
m.viewport.GotoTop()
m.viewport.LineDown(lineNumber)
}
}
default:
if m.searching {
m.searchQuery += msg.String()
}
}

case tea.WindowSizeMsg:
Expand Down Expand Up @@ -99,11 +158,31 @@ func (m model) View() string {
return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.viewport.View(), m.footerView())
}

/*
func (m model) headerView() string {
title := titleStyle.Render("Browsing index: " + m.index)
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(title)))
return lipgloss.JoinHorizontal(lipgloss.Center, title, line)
}
*/

func (m model) headerView() string {
var title string
if m.searching {
title = titleStyle.Render(fmt.Sprintf("Search: %s", m.searchQuery))
} else if m.searchQuery != "" {
occurrences := strings.Count(m.content, m.searchQuery)
currentOccurrence := 0
if m.searchIndex != -1 {
currentOccurrence = strings.Count(m.content[:m.viewport.YOffset+m.searchIndex], m.searchQuery)
}
title = titleStyle.Render(fmt.Sprintf("Search: %s (%d/%d) - \"n\" = next, \"q\" = clear ", m.searchQuery, currentOccurrence, occurrences))
} else {
title = titleStyle.Render("Browsing index: " + m.index)
}
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(title)))
return lipgloss.JoinHorizontal(lipgloss.Center, title, line)
}

func (m model) footerView() string {
info := infoStyle.Render(fmt.Sprintf("%3.f%%", m.viewport.ScrollPercent()*100))
Expand Down
Loading