Skip to content

Commit

Permalink
Webasm terminal fixes.
Browse files Browse the repository at this point in the history
Reverse video, not reverse text.
Provide an XTERM equivalent default palette.
Reset colors should go to silver on black.
Black color 0 should be rendered properly.
  • Loading branch information
gdamore committed Feb 21, 2023
1 parent d78960c commit 7fe9d5f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
11 changes: 8 additions & 3 deletions webfiles/tcell.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,19 @@ function drawCell(x, y, mainc, combc, fg, bg, attrs) {
var span = document.createElement("span")
var use = false

if (fg) { span.style.color = intToHex(fg); use = true }
if (bg) { span.style.backgroundColor = intToHex(bg); use = true }
if ((attrs & (1<<2)) != 0) { // reverse video
var temp = bg
bg = fg
fg = temp
use = true
}
if (fg != -1) { span.style.color = intToHex(fg); use = true }
if (bg != -1) { span.style.backgroundColor = intToHex(bg); use = true }

if (attrs != 0) {
use = true
if ((attrs & 1) != 0) { span.classList.add("bold") }
if ((attrs & (1<<1)) != 0) { span.classList.add("blink") }
if ((attrs & (1<<2)) != 0) { span.classList.add("reverse") }
if ((attrs & (1<<3)) != 0) { span.classList.add("underline") }
if ((attrs & (1<<4)) != 0) { span.classList.add("dim") }
if ((attrs & (1<<5)) != 0) { span.classList.add("italic") }
Expand Down
2 changes: 0 additions & 2 deletions webfiles/termstyle.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

.blink { animation: blinker 1s step-start infinite; }

.reverse { unicode-bidi: bidi-override; direction: rtl; }

.underline { text-decoration: underline; }

.dim { filter: brightness(50) }
Expand Down
41 changes: 40 additions & 1 deletion wscreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,39 @@ func (t *wScreen) SetCell(x, y int, style Style, ch ...rune) {
}
}

// paletteColor gives a more natural palette color actually matching
// typical XTerm. We might in the future want to permit styling these
// via CSS.

var palette = map[Color]int32{
ColorBlack: 0x000000,
ColorMaroon: 0xcd0000,
ColorGreen: 0x00cd00,
ColorOlive: 0xcdcd00,
ColorNavy: 0x0000ee,
ColorPurple: 0xcd00cd,
ColorTeal: 0x00cdcd,
ColorSilver: 0xe5e5e5,
ColorGray: 0x7f7f7f,
ColorRed: 0xff0000,
ColorLime: 0x00ff00,
ColorYellow: 0xffff00,
ColorBlue: 0x5c5cff,
ColorFuchsia: 0xff00ff,
ColorAqua: 0x00ffff,
ColorWhite: 0xffffff,
}

func paletteColor(c Color) int32 {
if (c.IsRGB()) {
return int32(c & 0xffffff);
}
if (c >= ColorBlack && c <= ColorWhite) {
return palette[c]
}
return c.Hex()
}

func (t *wScreen) drawCell(x, y int) int {
mainc, combc, style, width := t.cells.GetContent(x, y)

Expand All @@ -120,7 +153,13 @@ func (t *wScreen) drawCell(x, y int) int {
style = t.style
}

fg, bg := style.fg.Hex(), style.bg.Hex()
fg, bg := paletteColor(style.fg), paletteColor(style.bg)
if (fg == -1) {
fg = 0xe5e5e5;
}
if (bg == -1) {
bg = 0x000000;
}

var combcarr []interface{} = make([]interface{}, len(combc))
for i, c := range combc {
Expand Down

0 comments on commit 7fe9d5f

Please sign in to comment.