Skip to content

Commit

Permalink
fix: buggy newline issue when highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
TypicalAM committed Jul 23, 2023
1 parent e941820 commit b837889
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
4 changes: 2 additions & 2 deletions internal/model/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m.createNewTab(msg)

case backend.NewItemMsg:
bg := lipgloss.NewStyle().Width(m.width).Height((m.height)).Render(m.View())
bg := m.View()
width := m.width / 2
height := 17

Expand All @@ -194,7 +194,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m.downloadItem(msg)

case backend.MakeChoiceMsg:
bg := lipgloss.NewStyle().Width(m.width).Height((m.height)).Render(m.View())
bg := m.View()
width := m.width / 2
m.popup = popup.NewChoice(m.style.colors, bg, width, msg.Question, msg.Default)

Expand Down
44 changes: 19 additions & 25 deletions internal/model/popup/popup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/ansi"
)

Expand All @@ -15,13 +14,12 @@ type Popup interface {

// Default is a default popup window.
type Default struct {
prefix string
suffix string
ogSection []string
section []string
textAbove string
textBelow string
rowPrefix []string
rowSuffix []string
width int
height int
startCol int
}

// New creates a new default popup window.
Expand All @@ -33,21 +31,24 @@ func New(bgRaw string, width, height int) Default {
startRow := (bgHeight - height) / 2
startCol := (bgWidth - width) / 2

ogSection := make([]string, height)
section := make([]string, height)
rowPrefix := make([]string, height)
rowSuffix := make([]string, height)

for i, text := range bg[startRow : startRow+height] {
rowPrefix[i] = text[:findPrintableIndex(text, startCol)]
rowSuffix[i] = text[findPrintableIndex(text, startCol+width):]
}

prefix := strings.Join(bg[:startRow], "\n")
suffix := strings.Join(bg[startRow+height:], "\n")
copy(ogSection, bg[startRow:startRow+height])

return Default{
ogSection: ogSection,
section: section,
rowPrefix: rowPrefix,
rowSuffix: rowSuffix,
width: width,
height: height,
prefix: prefix,
suffix: suffix,
startCol: startCol,
textAbove: prefix,
textBelow: suffix,
}
}

Expand All @@ -57,19 +58,12 @@ func (p Default) Overlay(text string) string {
lines := strings.Split(text, "\n")

// Overlay the background with the styled text.
// TODO: Use a string builder
for i, text := range p.ogSection {
p.section[i] = text[:findPrintableIndex(text, p.startCol)] +
lines[i] +
text[findPrintableIndex(text, p.startCol+p.width):]
output := make([]string, len(lines))
for i := 0; i < len(lines); i++ {
output[i] = p.rowPrefix[i] + lines[i] + p.rowSuffix[i]
}

return lipgloss.JoinVertical(
lipgloss.Top,
p.prefix,
strings.Join(p.section, "\n"),
p.suffix,
)
return p.textAbove + "\n" + strings.Join(output, "\n") + "\n" + p.textBelow
}

// Width returns the width of the popup window.
Expand Down
3 changes: 1 addition & 2 deletions internal/model/simplelist/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/reflow/ansi"
)

// Item is an item in the list
Expand Down Expand Up @@ -142,7 +141,7 @@ func (m Model) View() string {

if m.showDesc {
if item, ok := m.items[i].(list.DefaultItem); ok {
if ansi.PrintableRuneWidth(item.Description()) != 0 {
if len(item.Description()) != 0 {
sections = append(sections, m.style.styleDescription(item.Description()))
} else {
sections = append(sections, "")
Expand Down

0 comments on commit b837889

Please sign in to comment.