[ncurses6] Use wcolor_set to support more than 256 color pairs

To build fzf with ncurses 6 on macOS:

    brew install homebrew/dupes/ncurses
    LDFLAGS="-L/usr/local/opt/ncurses/lib" make install
This commit is contained in:
Junegunn Choi 2016-11-23 22:46:04 +09:00
parent 43425158f4
commit af31088481
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
2 changed files with 22 additions and 20 deletions

View File

@ -70,10 +70,10 @@ clean:
cd fzf && rm -f fzf-*
fzf/$(BINARY32): deps
cd fzf && GOARCH=386 CGO_ENABLED=1 go build -a -ldflags -w -tags "$(TAGS)" -o $(BINARY32)
cd fzf && GOARCH=386 CGO_ENABLED=1 go build -a -ldflags "-w -extldflags=$(LDFLAGS)" -tags "$(TAGS)" -o $(BINARY32)
fzf/$(BINARY64): deps
cd fzf && go build -a -ldflags -w -tags "$(TAGS)" -o $(BINARY64)
cd fzf && go build -a -ldflags "-w -extldflags=$(LDFLAGS)" -tags "$(TAGS)" -o $(BINARY64)
$(BINDIR)/fzf: fzf/$(BINARY) | $(BINDIR)
cp -f fzf/$(BINARY) $(BINDIR)

View File

@ -65,7 +65,7 @@ const (
var (
_screen *C.SCREEN
_colorMap map[int]ColorPair
_colorFn func(ColorPair, Attr) C.int
_colorFn func(ColorPair, Attr) (C.short, C.int)
)
func init() {
@ -164,10 +164,12 @@ func NewWindow(top int, left int, width int, height int, border bool) *Window {
C.wbkgd(win, C.chtype(C.COLOR_PAIR(C.int(ColNormal))))
}
if border {
attr := _colorFn(ColBorder, 0)
pair, attr := _colorFn(ColBorder, 0)
C.wcolor_set(win, pair, nil)
C.wattron(win, attr)
C.box(win, 0, 0)
C.wattroff(win, attr)
C.wcolor_set(win, 0, nil)
}
return &Window{
@ -179,15 +181,11 @@ func NewWindow(top int, left int, width int, height int, border bool) *Window {
}
}
func attrColored(pair ColorPair, a Attr) C.int {
var attr C.int
if pair > 0 {
attr = C.COLOR_PAIR(C.int(pair))
}
return attr | C.int(a)
func attrColored(pair ColorPair, a Attr) (C.short, C.int) {
return C.short(pair), C.int(a)
}
func attrMono(pair ColorPair, a Attr) C.int {
func attrMono(pair ColorPair, a Attr) (C.short, C.int) {
var attr C.int
switch pair {
case ColCurrent:
@ -200,7 +198,7 @@ func attrMono(pair ColorPair, a Attr) C.int {
if C.int(a)&C.A_BOLD == C.A_BOLD {
attr = attr | C.A_BOLD
}
return attr
return 0, attr
}
func MaxX() int {
@ -241,11 +239,13 @@ func (w *Window) Print(text string) {
}, text)))
}
func (w *Window) CPrint(pair ColorPair, a Attr, text string) {
attr := _colorFn(pair, a)
C.wattron(w.win(), attr)
func (w *Window) CPrint(pair ColorPair, attr Attr, text string) {
p, a := _colorFn(pair, attr)
C.wcolor_set(w.win(), p, nil)
C.wattron(w.win(), a)
w.Print(text)
C.wattroff(w.win(), attr)
C.wattroff(w.win(), a)
C.wcolor_set(w.win(), 0, nil)
}
func Clear() {
@ -265,11 +265,13 @@ func (w *Window) Fill(str string) bool {
return C.waddstr(w.win(), C.CString(str)) == C.OK
}
func (w *Window) CFill(str string, fg Color, bg Color, a Attr) bool {
attr := _colorFn(PairFor(fg, bg), a)
C.wattron(w.win(), attr)
func (w *Window) CFill(str string, fg Color, bg Color, attr Attr) bool {
pair := PairFor(fg, bg)
C.wcolor_set(w.win(), C.short(pair), nil)
C.wattron(w.win(), C.int(attr))
ret := w.Fill(str)
C.wattroff(w.win(), attr)
C.wattroff(w.win(), C.int(attr))
C.wcolor_set(w.win(), 0, nil)
return ret
}