mirror of
https://github.com/walles/moar.git
synced 2024-09-11 12:15:43 +03:00
Trim leading whitespace from wrapped lines
This commit is contained in:
parent
0a40404070
commit
e605199d92
@ -7,15 +7,28 @@ func wrapLine(width int, line []twin.Cell) [][]twin.Cell {
|
||||
return [][]twin.Cell{{}}
|
||||
}
|
||||
|
||||
// Unclear how to represent trailing whitespace while wrapping
|
||||
// Trailing space risks showing up by itself on a line, which would just
|
||||
// look weird.
|
||||
line = twin.TrimSpaceRight(line)
|
||||
|
||||
wrapped := make([][]twin.Cell, 0, len(line)/width)
|
||||
for len(line) > width {
|
||||
firstPart := line[:width]
|
||||
if len(wrapped) > 0 {
|
||||
// Leading whitespace on wrapped lines would just look like
|
||||
// indentation, which would be weird for wrapped text.
|
||||
firstPart = twin.TrimSpaceLeft(firstPart)
|
||||
}
|
||||
|
||||
wrapped = append(wrapped, firstPart)
|
||||
|
||||
line = line[width:]
|
||||
line = twin.TrimSpaceLeft(line[width:])
|
||||
}
|
||||
|
||||
if len(wrapped) > 0 {
|
||||
// Leading whitespace on wrapped lines would just look like
|
||||
// indentation, which would be weird for wrapped text.
|
||||
line = twin.TrimSpaceLeft(line)
|
||||
}
|
||||
|
||||
if len(line) > 0 {
|
||||
|
@ -57,14 +57,32 @@ func TestWordLongerThanLine(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestLeadingSpaceNoWrap(t *testing.T) {
|
||||
toWrap := tokenize(" abc")
|
||||
wrapped := wrapLine(20, toWrap)
|
||||
assertEqual(t, wrapped, [][]twin.Cell{
|
||||
tokenize(" abc"),
|
||||
})
|
||||
}
|
||||
|
||||
func TestLeadingSpaceWithWrap(t *testing.T) {
|
||||
toWrap := tokenize(" abc")
|
||||
wrapped := wrapLine(2, toWrap)
|
||||
assertEqual(t, wrapped, [][]twin.Cell{
|
||||
tokenize(" a"),
|
||||
tokenize("bc"),
|
||||
})
|
||||
}
|
||||
|
||||
func TestLeadingWrappedSpace(t *testing.T) {
|
||||
toWrap := tokenize("ab cd")
|
||||
wrapped := wrapLine(2, toWrap)
|
||||
assertEqual(t, wrapped, [][]twin.Cell{
|
||||
tokenize("ab"),
|
||||
tokenize("cd"),
|
||||
})
|
||||
}
|
||||
|
||||
// FIXME: Test word wrapping
|
||||
|
||||
// FIXME: Test wrapping with multiple consecutive spaces
|
||||
|
||||
// FIXME: Test wrapping on single dashes
|
||||
|
||||
// FIXME: Test wrapping with double dashes (not sure what we should do with those)
|
||||
|
||||
// FIXME: Test wrapping formatted strings, is there formatting that should affect the wrapping
|
||||
|
||||
// FIXME: Test wrapping with trailing whitespace
|
||||
|
15
twin/cell.go
15
twin/cell.go
@ -36,3 +36,18 @@ func TrimSpaceRight(cells []Cell) []Cell {
|
||||
// All whitespace, return empty
|
||||
return []Cell{}
|
||||
}
|
||||
|
||||
// Returns a slice of cells with leading whitespace cells removed
|
||||
func TrimSpaceLeft(cells []Cell) []Cell {
|
||||
for i := 0; i < len(cells); i++ {
|
||||
cell := cells[i]
|
||||
if !unicode.IsSpace(cell.Rune) {
|
||||
return cells[i:]
|
||||
}
|
||||
|
||||
// That was a space, keep looking
|
||||
}
|
||||
|
||||
// All whitespace, return empty
|
||||
return []Cell{}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user