-
Notifications
You must be signed in to change notification settings - Fork 2
/
Color.go
82 lines (71 loc) · 1.81 KB
/
Color.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Licensed under the GPL-v3
// Copyright: Sebastian Tilders <[email protected]> (c) 2019
package ncurses
import (
// #include <curses.h>
"C"
"errors"
"fmt"
)
// A ncurses base color
type Color int
type pairId int
type pairMap map [string]pairId
var numPairs int = 1
var pairs pairMap = make(pairMap)
const (
ColorBlack Color = iota
ColorRed
ColorGreen
ColorYellow
ColorBlue
ColorMagenta
ColorCyan
ColorWhite
)
// Changes terminal to color mode. If the terminal does not support colors. StartColor returns an error.
func StartColor() error {
if !C.has_colors() {
return errors.New("Terminal does not support colors")
}
C.assume_default_colors(-1,-1)
pairs["std"] = pairId(0)
C.start_color()
return nil
}
// Adds a new pair of colors, which can be used to manipulate the color of terminal outputs.
// Choose one color of type Color.
func AddColorPair(name string, fg,bg Color) {
pairs[name] = pairId(numPairs)
C.init_pair(C.short(numPairs),C.short(fg),C.short(bg))
numPairs++
}
// Selects a font foreground/background color pair. Every write uses the new color pair.
//
// Use AddColorPair to create ncurses Color-Pairs. There is one default color-pair which defaults to the terminal color-set for foreground and background.
func SetColor(name string) error {
val, ok := pairs[name]
if !ok {
return fmt.Errorf("Color \"%s\"pair does not exist!",name)
}
GetComChannel() <- Command{
Name: SETCOLOR,
Scope:GLOBAL,
Value:val,
}
return nil
}
// Changes foreground/background color-pair of the associated window.
func (w *Window) Wbkgd(pairName string) error {
val,ok := pairs[pairName]
if !ok {
return fmt.Errorf("Color pair \"%s\" does not exist!",pairName)
}
w.sendCommand(Command{
Name:WBKGD,
Window:w,
Scope:LOCAL,
Value:val,
},w.AutoRefresh)
return nil
}