Skip to content

drawing styled strings onto a buffer loses escape sequences #95

@gwenya

Description

@gwenya

Description

When drawing a styled string onto a buffer, escape sequences are parsed to detect links and styles. Other escape sequences are lost:

apcSequence := "\x1B_foo\x1B\\"
input := apcSequence + "bar"
styled := uv.NewStyledString(input)
buffer := uv.NewScreenBuffer(20, 1)
styled.Draw(buffer, buffer.Bounds())
firstCell := buffer.CellAt(0, 0)
fmt.Printf("expected: %v\n", []byte(apcSequence+"b"))
fmt.Printf("got: %v\n", []byte(firstCell.Content))

Output:

expected: [27 95 102 111 111 27 92 98]
got: [98]

Digging into the code a bit, the sequence is added to the cell, but then it is overwritten by the character that is written into the cell:

ultraviolet/styled.go

Lines 198 to 199 in 89c142e

default:
cell.Content += string(seq)

ultraviolet/styled.go

Lines 132 to 134 in 89c142e

case 1, 2, 3, 4: // wide cells can go up to 4 cells wide
cell.Width = width
cell.Content = string(seq)

I think the fix should be as easy as replacing that last line with cell.Content += string(seq).
I just tried that for a bubbletea application where I'm using APC sequences to pass information through the view and it works great.
I have a branch with the fix ready if you would like a PR: https://github.com/gwenya/ultraviolet/tree/pass-escape-sequences
With that fix there is still a slight issue: when printing a string that ends with an escape sequence, the sequence becomes part of the next cell after the end of the string, which might be outside the buffer. Semantically it would probably be fine to append that to the previous cell, but that would require a slightly larger change.

Version

master

Environment

all?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions