Add 256 colour support

This commit is contained in:
Liam Galvin 2018-10-22 10:36:55 +01:00
parent d1f460b567
commit 561358a6ac
3 changed files with 204 additions and 108 deletions

12
demo/256.sh Executable file
View File

@ -0,0 +1,12 @@
#!/bin/bash
for i in {0..255} ; do
printf "\x1b[48;5;%sm%3d\e[0m " "$i" "$i"
if (( i == 15 )) || (( i > 15 )) && (( (i-15) % 6 == 0 )); then
printf "\n";
fi
done

View File

@ -52,8 +52,7 @@ func csiInsertLinesHandler(params []string, intermediate string, terminal *Termi
}
}
terminal.logger.Debugf("Inserting %d lines", count)
panic("Not supported")
return nil
return fmt.Errorf("Not supported")
}
func csiScrollDownHandler(params []string, intermediate string, terminal *Terminal) error {

View File

@ -2,14 +2,19 @@ package terminal
import (
"fmt"
"strconv"
"github.com/liamg/aminal/buffer"
)
func sgrSequenceHandler(params []string, intermediate string, terminal *Terminal) error {
for i := range params {
param := params[i]
if len(params) == 0 {
return nil
}
param := params[0]
switch param {
case "00", "0", "":
attr := terminal.ActiveBuffer().CursorAttr()
@ -109,11 +114,91 @@ func sgrSequenceHandler(params []string, intermediate string, terminal *Terminal
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.LightCyan
case "107":
terminal.ActiveBuffer().CursorAttr().BgColour = terminal.config.ColourScheme.White
case "38": // set foreground
if len(params) > 2 {
switch params[1] {
case "5":
// 8 bit colour
colNum, err := strconv.Atoi(params[2])
if err == nil && colNum < 256 && colNum >= 0 {
terminal.ActiveBuffer().CursorAttr().FgColour = get8BitSGRColour(uint8(colNum), terminal)
}
case "2":
// 24 bit colour
}
}
case "48": // set background
if len(params) > 2 {
switch params[1] {
case "5":
// 8 bit colour
colNum, err := strconv.Atoi(params[2])
if err == nil && colNum < 256 && colNum >= 0 {
terminal.ActiveBuffer().CursorAttr().BgColour = get8BitSGRColour(uint8(colNum), terminal)
}
case "2":
// 24 bit colour
}
}
default:
return fmt.Errorf("Unknown SGR control sequence: (ESC[%s%sm)", param, intermediate)
}
//terminal.logger.Debugf("SGR control sequence: (ESC[%s%sm)", param, intermediate)
}
return nil
}
func get8BitSGRColour(colNum uint8, terminal *Terminal) [3]float32 {
// https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
switch colNum {
case 0:
return terminal.config.ColourScheme.Black
case 1:
return terminal.config.ColourScheme.Red
case 2:
return terminal.config.ColourScheme.Green
case 3:
return terminal.config.ColourScheme.Yellow
case 4:
return terminal.config.ColourScheme.Blue
case 5:
return terminal.config.ColourScheme.Magenta
case 6:
return terminal.config.ColourScheme.Cyan
case 7:
return terminal.config.ColourScheme.White
case 8:
return terminal.config.ColourScheme.DarkGrey
case 9:
return terminal.config.ColourScheme.LightRed
case 10:
return terminal.config.ColourScheme.LightGreen
case 11:
return terminal.config.ColourScheme.LightYellow
case 12:
return terminal.config.ColourScheme.LightBlue
case 13:
return terminal.config.ColourScheme.LightMagenta
case 14:
return terminal.config.ColourScheme.LightCyan
case 15:
return terminal.config.ColourScheme.White
}
if colNum < 232 {
index := int(colNum - 16) // 0-216
rgb := (index * 0xffffff) / 216
r := float32((rgb&0xff0000)>>16) / 0xff
g := float32((rgb&0xff00)>>8) / 0xff
b := float32(rgb&0xff) / 0xff
return [3]float32{r, g, b}
}
c := float32(colNum-232) / 0x18
return [3]float32{c, c, c}
}