Skip to content

Commit

Permalink
Much simplified cropping
Browse files Browse the repository at this point in the history
  • Loading branch information
Evertras committed Jan 30, 2024
1 parent 62bb6c8 commit 79407ed
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/evertras/yakdash
go 1.21.5

require (
github.com/charmbracelet/bubbles v0.17.1
github.com/charmbracelet/bubbletea v0.25.0
github.com/charmbracelet/lipgloss v0.9.1
github.com/mitchellh/mapstructure v1.5.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbles v0.17.1 h1:0SIyjOnkrsfDo88YvPgAWvZMwXe26TP6drRvmkjyUu4=
github.com/charmbracelet/bubbles v0.17.1/go.mod h1:9HxZWlkCqz2PRwsCbYl7a3KXvGzFaDHpYbSYMJ+nE3o=
github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt5dywy4TcM=
github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg=
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
Expand Down
36 changes: 36 additions & 0 deletions pkg/panes/crop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package panes

import (
"bufio"
"strings"
)

func (m Pane) crop(str string) string {
reader := strings.NewReader(str)
scanner := bufio.NewScanner(reader)

cropped := strings.Builder{}

maxCrop := m.width - 2
first := true

for i := 0; i < m.height-2; i++ {
if !scanner.Scan() {
break
}
if !first {
cropped.WriteRune('\n')
}
first = false
line := scanner.Text()

crop := maxCrop
if len(line) < maxCrop {
cropped.WriteString(line)
} else {
cropped.WriteString(scanner.Text()[:crop])
}
}

return cropped.String()
}
15 changes: 3 additions & 12 deletions pkg/panes/panes.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package panes

import (
"strings"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
Expand All @@ -26,18 +23,15 @@ type Pane struct {

width int
height int

textarea viewport.Model
}

// NewModel creates a new pane containing the given model.
func NewLeaf(m tea.Model) Pane {
borderStyle := lipgloss.NewStyle().Border(lipgloss.RoundedBorder())

return Pane{
model: m,
style: borderStyle.Align(lipgloss.Center, lipgloss.Center),
textarea: viewport.New(0, 0),
model: m,
style: borderStyle.Align(lipgloss.Top, lipgloss.Left),
}
}

Expand Down Expand Up @@ -94,10 +88,7 @@ func (m Pane) Update(msg tea.Msg) (Pane, tea.Cmd) {
func (m Pane) View() string {
if m.model != nil {
style := m.style.Copy().Width(m.width - 2).Height(m.height - 2)
m.textarea.Width = m.width - 2
m.textarea.Height = m.height - 2
m.textarea.SetContent(m.model.View())
innerView := strings.TrimSpace(m.textarea.View())
innerView := m.crop(m.model.View())
if m.name != "" {
return m.genTop() + "\n" + style.Render(innerView)
}
Expand Down
19 changes: 17 additions & 2 deletions pkg/panes/panes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestViewLeafNodeShowsInnerModel(t *testing.T) {
assert.Contains(t, pane.View(), "testing", "View should return inner model's view")
}

func TestViewLeafNodeCropsInnerModel(t *testing.T) {
func TestViewLeafNodeCropsInnerModelVertically(t *testing.T) {
// Given a leaf node with a lot of lines of text,
// the view should crop the text to the pane's dimensions.
const expectedLines = 2
Expand All @@ -101,7 +101,22 @@ func TestViewLeafNodeCropsInnerModel(t *testing.T) {

numTesting := strings.Count(pane.View(), "testing")

assert.Equal(t, expectedLines, numTesting, "View should only have two lines of text given the dimensions")
assert.Equal(t, expectedLines, numTesting, "View should only have two lines of text given the dimensions: "+pane.View())
}

func TestViewLeafNodeCropsInnerModel(t *testing.T) {
// Given a leaf node with a lot of lines of text,
// the view should crop the text to the pane's dimensions.
const expectedView = `╭────╮
│test│
│test│
╰────╯`

text := strings.Repeat("testing\n", 100)
pane, _ := panes.NewLeaf(newDummyModel(text, nil, nil)).
WithDimensions(6, 4) // Account for border

assert.Equal(t, expectedView, pane.View())
}

func TestViewParentNodeShowsInnerModelsOfChildren(t *testing.T) {
Expand Down

0 comments on commit 79407ed

Please sign in to comment.