Use ANSI-aware truncation

This commit is contained in:
Christian Rocha 2020-12-14 18:42:33 -05:00
parent 59f8a002d0
commit 28ccb85b2e
6 changed files with 14 additions and 18 deletions

2
go.mod
View file

@ -17,7 +17,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/muesli/gitcha v0.1.2-0.20200908172931-5aa4fdccf2f6
github.com/muesli/go-app-paths v0.2.1
github.com/muesli/reflow v0.2.0
github.com/muesli/reflow v0.2.1-0.20201126184510-3bcb929042f2
github.com/muesli/termenv v0.7.4
github.com/sahilm/fuzzy v0.1.0
github.com/segmentio/ksuid v1.0.3

2
go.sum
View file

@ -202,6 +202,8 @@ github.com/muesli/go-app-paths v0.2.1/go.mod h1:SxS3Umca63pcFcLtbjVb+J0oD7cl4ixQ
github.com/muesli/reflow v0.1.0/go.mod h1:I9bWAt7QTg/que/qmUCJBGlj7wEq8OAFBjPNjc6xK4I=
github.com/muesli/reflow v0.2.0 h1:2o0UBJPHHH4fa2GCXU4Rg4DwOtWPMekCeyc5EWbAQp0=
github.com/muesli/reflow v0.2.0/go.mod h1:qT22vjVmM9MIUeLgsVYe/Ye7eZlbv9dZjL3dVhUqLX8=
github.com/muesli/reflow v0.2.1-0.20201126184510-3bcb929042f2 h1:+cpkcmASpeBn4qXz2tU+x+njyKe91NoHXqrJdhoDnZo=
github.com/muesli/reflow v0.2.1-0.20201126184510-3bcb929042f2/go.mod h1:qT22vjVmM9MIUeLgsVYe/Ye7eZlbv9dZjL3dVhUqLX8=
github.com/muesli/sasquatch v0.0.0-20200811221207-66979d92330a h1:Hw/15RYEOUD6T9UCRkUmNBa33kJkH33Fui6hE4sRLKU=
github.com/muesli/sasquatch v0.0.0-20200811221207-66979d92330a/go.mod h1:+XG0ne5zXWBTSbbe7Z3/RWxaT8PZY6zaZ1dX6KjprYY=
github.com/muesli/termenv v0.7.0/go.mod h1:SohX91w6swWA4AYU+QmPx+aSgXhWO0juiyID9UZmbpA=

View file

@ -16,6 +16,7 @@ import (
"github.com/charmbracelet/glamour"
runewidth "github.com/mattn/go-runewidth"
"github.com/muesli/reflow/ansi"
"github.com/muesli/reflow/truncate"
te "github.com/muesli/termenv"
)
@ -403,13 +404,13 @@ func (m pagerModel) statusBarView(b *strings.Builder) {
note = "(No memo)"
}
}
note = truncate(" "+note+" ", max(0,
note = truncate.StringWithTail(" "+note+" ", uint(max(0,
m.common.width-
ansi.PrintableRuneWidth(logo)-
ansi.PrintableRuneWidth(statusIndicator)-
ansi.PrintableRuneWidth(scrollPercent)-
ansi.PrintableRuneWidth(helpNote),
))
)), ellipsis)
if showStatusMessage {
note = statusBarMessageStyle(note)
} else {

View file

@ -1034,8 +1034,7 @@ func (m stashModel) view() string {
// Rules for the logo, filter and status message.
var logoOrFilter string
if m.showStatusMessage {
const gutter = 3
logoOrFilter = m.statusMessage.String() // TODO: auto-truncate
logoOrFilter = m.statusMessage.String()
} else if m.isFiltering() {
logoOrFilter = m.filterInput.View()
} else {

View file

@ -6,7 +6,8 @@ import (
"strings"
lib "github.com/charmbracelet/charm/ui/common"
rw "github.com/mattn/go-runewidth"
"github.com/muesli/reflow/ansi"
"github.com/muesli/reflow/truncate"
"github.com/muesli/termenv"
"github.com/sahilm/fuzzy"
)
@ -19,7 +20,7 @@ const (
func stashItemView(b *strings.Builder, m stashModel, index int, md *markdown) {
var (
truncateTo = m.common.width - stashViewHorizontalPadding*2
truncateTo = uint(m.common.width - stashViewHorizontalPadding*2)
gutter string
title = md.Note
date = md.relativeTime()
@ -31,16 +32,16 @@ func stashItemView(b *strings.Builder, m stashModel, index int, md *markdown) {
if title == "" {
title = "News"
} else {
title = truncate(title, truncateTo)
title = truncate.StringWithTail(title, truncateTo, ellipsis)
}
case StashedDoc, ConvertedDoc:
icon = fileListingStashIcon
if title == "" {
title = noMemoTitle
}
title = truncate(title, truncateTo-rw.StringWidth(icon))
title = truncate.StringWithTail(title, truncateTo-uint(ansi.PrintableRuneWidth(icon)), ellipsis)
default:
title = truncate(title, truncateTo)
title = truncate.StringWithTail(title, truncateTo, ellipsis)
}
isSelected := index == m.cursor()

View file

@ -17,7 +17,6 @@ import (
"github.com/charmbracelet/charm/ui/common"
lib "github.com/charmbracelet/charm/ui/common"
"github.com/charmbracelet/glow/utils"
runewidth "github.com/mattn/go-runewidth"
"github.com/muesli/gitcha"
te "github.com/muesli/termenv"
"github.com/segmentio/ksuid"
@ -26,6 +25,7 @@ import (
const (
noteCharacterLimit = 256 // should match server
statusMessageTimeout = time.Second * 2 // how long to show status messages like "stashed!"
ellipsis = "…"
)
var (
@ -743,13 +743,6 @@ func indent(s string, n int) string {
return b.String()
}
func truncate(str string, num int) string {
if num < 1 {
return str
}
return runewidth.Truncate(str, num, "…")
}
func min(a, b int) int {
if a < b {
return a