Skip to content

Commit

Permalink
wip(textarea): using viewport's left gutter func
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Feb 4, 2025
1 parent eec8e1c commit f3bfbb2
Showing 1 changed file with 40 additions and 42 deletions.
82 changes: 40 additions & 42 deletions textarea/textarea.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"image/color"
"reflect"
"strconv"
"strings"
"time"
"unicode"
Expand Down Expand Up @@ -1306,13 +1305,14 @@ func (m Model) View() string {

var ln string
if m.ShowLineNumbers { //nolint:nestif

Check failure on line 1307 in textarea/textarea.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

directive `//nolint:nestif` is unused for linter "nestif" (nolintlint)

Check failure on line 1307 in textarea/textarea.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

directive `//nolint:nestif` is unused for linter "nestif" (nolintlint)

Check failure on line 1307 in textarea/textarea.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

directive `//nolint:nestif` is unused for linter "nestif" (nolintlint)

Check failure on line 1307 in textarea/textarea.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

directive `//nolint:nestif` is unused for linter "nestif" (nolintlint)

Check failure on line 1307 in textarea/textarea.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

directive `//nolint:nestif` is unused for linter "nestif" (nolintlint)

Check failure on line 1307 in textarea/textarea.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

directive `//nolint:nestif` is unused for linter "nestif" (nolintlint)
if wl == 0 { // normal line
isCursorLine := m.row == l
s.WriteString(m.lineNumberView(l+1, isCursorLine))
} else { // soft wrapped line
isCursorLine := m.row == l
s.WriteString(m.lineNumberView(-1, isCursorLine))
}
m.lineNumberView()
// if wl == 0 { // normal line
// isCursorLine := m.row == l
// // s.WriteString(m.lineNumberView(l+1, isCursorLine))
// } else { // soft wrapped line
// isCursorLine := m.row == l
// // s.WriteString(m.lineNumberView(-1, isCursorLine))
// }
}

// Note the widest line number for padding purposes later.
Expand Down Expand Up @@ -1425,30 +1425,25 @@ func (m Model) promptView(displayLine int) (prompt string) {
//
// The second argument indicates whether this line number is for a 'cursorline'
// line number.
func (m Model) lineNumberView(n int, isCursorLine bool) (str string) {
func (m Model) lineNumberView() {
if !m.ShowLineNumbers {
return ""
}

if n <= 0 {
str = " "
} else {
str = strconv.Itoa(n)
return
}

// XXX: is textStyle really necessary here?
textStyle := m.activeStyle().computedText()
lineNumberStyle := m.activeStyle().computedLineNumber()
if isCursorLine {
textStyle = m.activeStyle().computedCursorLine()
lineNumberStyle = m.activeStyle().computedCursorLineNumber()
m.viewport.LeftGutterFunc = func(info viewport.GutterContext) string {
style := m.activeStyle().LineNumber
if info.Soft {
return style.Render(" │ ")
}
if info.Index >= info.TotalLines {
return style.Render(" ~ │ ")
}
if m.row == info.Index {
return m.activeStyle().CursorLineNumber.Render(fmt.Sprintf("%4d", info.Index+1)) +
style.Render(" │ ")
}
return style.Render(fmt.Sprintf("%4d │ ", info.Index+1))
}

// Format line number dynamically based on the maximum number of lines.
digits := len(strconv.Itoa(m.MaxHeight))
str = fmt.Sprintf(" %*v ", digits, str)

return textStyle.Render(lineNumberStyle.Render(str))
}

// placeholderView returns the prompt and placeholder, if any.
Expand All @@ -1466,7 +1461,7 @@ func (m Model) placeholderView() string {
plines := strings.Split(strings.TrimSpace(pwrap), "\n")

for i := 0; i < m.height; i++ {
isLineNumber := len(plines) > i
// isLineNumber := len(plines) > i

lineStyle := styles.computedPlaceholder()
if len(plines) > i {
Expand All @@ -1482,18 +1477,18 @@ func (m Model) placeholderView() string {
// - render line number for only the cursor line
// - indent other placeholder lines
// this is consistent with vim with line numbers enabled
if m.ShowLineNumbers {
var ln int

switch {
case i == 0:
ln = i + 1
fallthrough
case len(plines) > i:
s.WriteString(m.lineNumberView(ln, isLineNumber))
default:
}
}
// if m.ShowLineNumbers {
// var ln int
//
// switch {
// case i == 0:
// ln = i + 1
// fallthrough
// case len(plines) > i:
// s.WriteString(m.lineNumberView(ln, isLineNumber))
// default:
// }
// }

switch {
// first line
Expand Down Expand Up @@ -1558,11 +1553,14 @@ func (m Model) Cursor() *tea.Cursor {

xOffset := lineInfo.CharOffset +
w(m.promptView(0)) +
w(m.lineNumberView(0, false)) +
baseStyle.GetMarginLeft() +
baseStyle.GetPaddingLeft() +
baseStyle.GetBorderLeftSize()

if m.ShowLineNumbers {
xOffset += lipgloss.Width(m.viewport.LeftGutterFunc(viewport.GutterContext{}))
}

yOffset := m.cursorLineNumber() +
m.viewport.YOffset +
baseStyle.GetMarginTop() +
Expand Down

0 comments on commit f3bfbb2

Please sign in to comment.