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

feat(viewport): gutter column, soft wrap, search highlight #697

Open
wants to merge 28 commits into
base: feature/i236-viewport-horizontal-scroll
Choose a base branch
from

Conversation

caarlos0
Copy link
Member

@caarlos0 caarlos0 commented Jan 7, 2025

This adds a ColumnSign API (name is the worst part of this so far), which allows you to define a left column in the viewport, which isn't taken into account when horizontal scrolling.

You can use to show line numbers, for example:

vp.ColumnSignFn = func(i, total int) string {
	if i >= total {
		return lineNumberStyle.ToLipgloss().Render("   ~ │ ")
	}
	return lineNumberStyle.ToLipgloss().Render(fmt.Sprintf("%4d │ ", i+1))
}

which would render something like:

CleanShot.2025-01-07.at.16.36.30.mp4

As you can notice in the video and code, it also fills to the height if the viewport if the number of lines is lower than the available height, similar to what neovim and other editors do.

It also adds the ability to highlight ranges, e.g. from regex matches. This should work regardless of ansi sequences and/or grapheme clusters.

Finally, it adds options to soft wrap lines and to fill the height with empty lines.

Things to take into account here:

  • we should probably move the soft wrap features into here as well
  • the API for sure needs more changes, e.g. some way to inform it is a soft wrapped line and not a real line
  • no tests so far as I'm just experimenting
  • it depends on feat(viewport): horizontal scroll #240

@caarlos0 caarlos0 requested a review from meowgorithm as a code owner January 7, 2025 19:52
viewport/viewport.go Outdated Show resolved Hide resolved
@caarlos0 caarlos0 requested a review from bashbunni as a code owner January 8, 2025 14:58
@caarlos0 caarlos0 changed the title feat(viewport): column sign feat(viewport): gutter column, soft wrap, search highlight Jan 8, 2025
@caarlos0
Copy link
Member Author

caarlos0 commented Jan 8, 2025

showing off soft wrap and search as well (on gum):

CleanShot.2025-01-08.at.12.00.51.mp4

Signed-off-by: Carlos Alexandro Becker <[email protected]>
go.mod Outdated
@@ -32,3 +32,5 @@ require (
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.3.8 // indirect
)

replace github.com/charmbracelet/lipgloss => ../lipgloss
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should def remember to remove this before merging 😄

viewport/viewport.go Outdated Show resolved Hide resolved
@caarlos0
Copy link
Member Author

caarlos0 commented Jan 9, 2025

looks like this right now (using it in gum):

CleanShot.2025-01-08.at.22.56.32.mp4

caarlos0 and others added 12 commits January 8, 2025 23:07
* horizontal scroll

* rebase branch

* add tests

* add tests with 2 cells symbols

* trimLeft, move to charmbracelete/x/ansi lib

* up ansi package

* Update viewport/viewport.go

Co-authored-by: Carlos Alexandro Becker <[email protected]>

* fix: do not navigate out to the right

* fix: cache line width on setcontent

* fix tests

* fix viewport tests

* add test for preventing right overscroll

* chore(viewport): increase horizontal step to 6

* chore(viewport): make horizontal scroll API better match vertical scroll API

* fix: nolint

* fix: use ansi.Cut

* perf: do not cut anything if not needed

* feat: expose HorizontalScrollPercent

* fix: do not scroll if width is 0

Signed-off-by: Carlos Alexandro Becker <[email protected]>

* fix: visible lines take frame into account

---------

Signed-off-by: Carlos Alexandro Becker <[email protected]>
Co-authored-by: Carlos Alexandro Becker <[email protected]>
Co-authored-by: Christian Rocha <[email protected]>
This reverts commit 933f181.
This reverts commit 0e3e31b.
return m.prependColumn(lines)
}

func (m Model) highlighLines(lines []string, offset int) []string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

highlightLines*

// Use [SetHighligths] to set the highlight ranges, and [HightlightNext]
// and [HihglightPrevious] to navigate.
// Use [ClearHighlights] to remove all highlights.
func (m *Model) SetHighligths(matches [][]int) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*SetHighlights

@bashbunni
Copy link
Member

TODO: create a runnable example with the API changes @bashbunni

return hi.lineStart, 0, 0
}

func makeHilightRanges(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Highlight*

@bashbunni bashbunni added the enhancement New feature or request label Jan 23, 2025
@bashbunni bashbunni added this to the viewport milestone Jan 23, 2025
@bashbunni bashbunni linked an issue Jan 23, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

viewport: horizontal scroll
4 participants