Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved output for text selection and copy/paste #7

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions ansitosvg/ansisvg.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ var DefaultOptions = Options{
// Convert reads ANSI input from r and writes SVG to w
func Convert(r io.Reader, w io.Writer, opts Options) error {
ad := ansidecoder.NewDecoder(r)
var chars []svgscreen.Char

lineNr := 0
var lines []svgscreen.Line
line := svgscreen.Line{
Y: lineNr,
}

for {
r, _, err := ad.ReadRune()
if err == io.EOF {
Expand All @@ -44,6 +50,11 @@ func Convert(r io.Reader, w io.Writer, opts Options) error {
}

if r == '\n' {
lines = append(lines, line)
lineNr++
line = svgscreen.Line{
Y: lineNr,
}
continue
}

Expand All @@ -54,17 +65,19 @@ func Convert(r io.Reader, w io.Writer, opts Options) error {
n = 8 - (ad.X % 8)
}
for i := 0; i < n; i++ {
chars = append(chars, svgscreen.Char{
line.Chars = append(line.Chars, svgscreen.Char{
Char: string([]rune{r}),
X: ad.X + i,
Y: ad.Y,
Foreground: ad.Foreground.String(),
Background: ad.Background.String(),
Underline: ad.Underline,
Intensity: ad.Intensity,
})
}
}
if len(line.Chars) > 0 {
lines = append(lines, line)
}
terminalWidth := ad.MaxX + 1
if opts.TerminalWidth != 0 {
terminalWidth = opts.TerminalWidth
Expand Down Expand Up @@ -125,8 +138,8 @@ func Convert(r io.Reader, w io.Writer, opts Options) error {
},
TerminalWidth: terminalWidth,
Columns: ad.MaxX + 1,
Lines: ad.MaxY + 1,
Chars: chars,
NrLines: ad.MaxY + 1,
Lines: lines,
},
)
}
17 changes: 5 additions & 12 deletions ansitosvg/testdata/charboxfontsize.ansi.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading