| 1 |
package vt |
| 2 |
|
| 3 |
import ( |
| 4 |
"unicode/utf8" |
| 5 |
|
| 6 |
uv "github.com/charmbracelet/ultraviolet" |
| 7 |
"github.com/charmbracelet/x/ansi" |
| 8 |
"github.com/mattn/go-runewidth" |
| 9 |
"github.com/rivo/uniseg" |
| 10 |
) |
| 11 |
|
| 12 |
// handlePrint handles printable characters. |
| 13 |
func (e *Emulator) handlePrint(r rune) { |
| 14 |
if r >= ansi.SP && r < ansi.DEL { |
| 15 |
if len(e.grapheme) > 0 { |
| 16 |
// If we have a grapheme buffer, flush it before handling the ASCII character. |
| 17 |
e.flushGrapheme() |
| 18 |
} |
| 19 |
e.handleGrapheme(string(r), 1) |
| 20 |
} else { |
| 21 |
e.grapheme = append(e.grapheme, r) |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
// flushGrapheme flushes the current grapheme buffer, if any, and handles the |
| 26 |
// grapheme as a single unit. |
| 27 |
func (e *Emulator) flushGrapheme() { |
| 28 |
if len(e.grapheme) == 0 { |
| 29 |
return |
| 30 |
} |
| 31 |
|
| 32 |
unicode := e.isModeSet(ansi.UnicodeCoreMode) |
| 33 |
gr := string(e.grapheme) |
| 34 |
|
| 35 |
var cl string |
| 36 |
var w int |
| 37 |
state := -1 |
| 38 |
for len(gr) > 0 { |
| 39 |
cl, gr, w, state = uniseg.FirstGraphemeClusterInString(gr, state) |
| 40 |
if !unicode { |
| 41 |
//nolint:godox |
| 42 |
// TODO: Investigate this further, runewidth.StringWidth doesn't |
| 43 |
// report the correct width for some edge cases such as variation |
| 44 |
// selectors. |
| 45 |
w = 0 |
| 46 |
for _, r := range cl { |
| 47 |
if r >= 0xFE00 && r <= 0xFE0F { |
| 48 |
// Variation Selectors 1 - 16 |
| 49 |
continue |
| 50 |
} |
| 51 |
if r >= 0xE0100 && r <= 0xE01EF { |
| 52 |
// Variation Selectors 17-256 |
| 53 |
continue |
| 54 |
} |
| 55 |
w += runewidth.RuneWidth(r) |
| 56 |
} |
| 57 |
} |
| 58 |
e.handleGrapheme(cl, w) |
| 59 |
} |
| 60 |
e.grapheme = e.grapheme[:0] // Reset the grapheme buffer. |
| 61 |
} |
| 62 |
|
| 63 |
// handleGrapheme handles UTF-8 graphemes. |
| 64 |
func (e *Emulator) handleGrapheme(content string, width int) { |
| 65 |
awm := e.isModeSet(ansi.AutoWrapMode) |
| 66 |
cell := uv.Cell{ |
| 67 |
Content: content, |
| 68 |
Width: width, |
| 69 |
Style: e.scr.cursorPen(), |
| 70 |
Link: e.scr.cursorLink(), |
| 71 |
} |
| 72 |
|
| 73 |
x, y := e.scr.CursorPosition() |
| 74 |
if e.atPhantom && awm { |
| 75 |
// moves cursor down similar to [Terminal.linefeed] except it doesn't |
| 76 |
// respects [ansi.LNM] mode. |
| 77 |
// This will reset the phantom state i.e. pending wrap state. |
| 78 |
e.index() |
| 79 |
_, y = e.scr.CursorPosition() |
| 80 |
x = 0 |
| 81 |
} |
| 82 |
|
| 83 |
// Handle character set mappings |
| 84 |
if len(content) == 1 { //nolint:nestif |
| 85 |
var charset CharSet |
| 86 |
c := content[0] |
| 87 |
if e.gsingle > 1 && e.gsingle < 4 { |
| 88 |
charset = e.charsets[e.gsingle] |
| 89 |
e.gsingle = 0 |
| 90 |
} else if c < 128 { |
| 91 |
charset = e.charsets[e.gl] |
| 92 |
} else { |
| 93 |
charset = e.charsets[e.gr] |
| 94 |
} |
| 95 |
|
| 96 |
if charset != nil { |
| 97 |
if r, ok := charset[c]; ok { |
| 98 |
cell.Content = r |
| 99 |
cell.Width = 1 |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if cell.Width == 1 && len(content) == 1 { |
| 105 |
e.lastChar, _ = utf8.DecodeRuneInString(content) |
| 106 |
} |
| 107 |
|
| 108 |
e.scr.SetCell(x, y, &cell) |
| 109 |
|
| 110 |
// Handle phantom state at the end of the line |
| 111 |
e.atPhantom = awm && x >= e.scr.Width()-1 |
| 112 |
if !e.atPhantom { |
| 113 |
x += cell.Width |
| 114 |
} |
| 115 |
|
| 116 |
// NOTE: We don't reset the phantom state here, we handle it up above. |
| 117 |
e.scr.setCursor(x, y, false) |
| 118 |
} |