Skip to content
Myr

Delete ncurses implementation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Junegunn Choi · committed by Junegunn Choi · parent 655dfb8 · Browse files · 1 file+0 added−505 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+0 added−505 removed

  • other:+0 added−505 removed

1 file+0 added−505 removed

deleted:src/tui/ncurses.go-505 removed
1// +build ncurses2// +build !windows3// +build !tcell45package tui67/*8#include <ncurses.h>9#include <locale.h>10#cgo !static LDFLAGS: -lncurses11#cgo static LDFLAGS: -l:libncursesw.a -l:libtinfo.a -l:libgpm.a -ldl12#cgo android static LDFLAGS: -l:libncurses.a -fPIE -march=armv7-a -mfpu=neon -mhard-float -Wl,--no-warn-mismatch1314FILE* c_tty() {15 return fopen("/dev/tty", "r");16}1718SCREEN* c_newterm(FILE* tty) {19 return newterm(NULL, stderr, tty);20}2122int c_getcurx(WINDOW* win) {23 return getcurx(win);24}25*/26import "C"2728import (29 "os"30 "strconv"31 "strings"32 "time"33 "unicode/utf8"34)3536func HasFullscreenRenderer() bool {37 return true38}3940type Attr C.uint4142type CursesWindow struct {43 impl *C.WINDOW44 top int45 left int46 width int47 height int48}4950func (w *CursesWindow) Top() int {51 return w.top52}5354func (w *CursesWindow) Left() int {55 return w.left56}5758func (w *CursesWindow) Width() int {59 return w.width60}6162func (w *CursesWindow) Height() int {63 return w.height64}6566func (w *CursesWindow) Refresh() {67 C.wnoutrefresh(w.impl)68}6970func (w *CursesWindow) FinishFill() {71 // NO-OP72}7374const (75 Bold Attr = C.A_BOLD76 Dim = C.A_DIM77 Blink = C.A_BLINK78 Reverse = C.A_REVERSE79 Underline = C.A_UNDERLINE80)8182var Italic Attr = C.A_VERTICAL << 1 // FIXME8384const (85 AttrRegular Attr = 086)8788var (89 _screen *C.SCREEN90 _colorMap map[int]int1691 _colorFn func(ColorPair, Attr) (C.short, C.int)92)9394func init() {95 _colorMap = make(map[int]int16)96 if strings.HasPrefix(C.GoString(C.curses_version()), "ncurses 5") {97 Italic = C.A_NORMAL98 }99}100101func (a Attr) Merge(b Attr) Attr {102 return a | b103}104105func (r *FullscreenRenderer) defaultTheme() *ColorTheme {106 if C.tigetnum(C.CString("colors")) >= 256 {107 return Dark256108 }109 return Default16110}111112func (r *FullscreenRenderer) Init() {113 C.setlocale(C.LC_ALL, C.CString(""))114 tty := C.c_tty()115 if tty == nil {116 errorExit("Failed to open /dev/tty")117 }118 _screen = C.c_newterm(tty)119 if _screen == nil {120 errorExit("Invalid $TERM: " + os.Getenv("TERM"))121 }122 C.set_term(_screen)123 if r.mouse {124 C.mousemask(C.ALL_MOUSE_EVENTS, nil)125 C.mouseinterval(0)126 }127 C.noecho()128 C.raw() // stty dsusp undef129 C.nonl()130 C.keypad(C.stdscr, true)131132 delay := 50133 delayEnv := os.Getenv("ESCDELAY")134 if len(delayEnv) > 0 {135 num, err := strconv.Atoi(delayEnv)136 if err == nil && num >= 0 {137 delay = num138 }139 }140 C.set_escdelay(C.int(delay))141142 if r.theme != nil {143 C.start_color()144 initTheme(r.theme, r.defaultTheme(), r.forceBlack)145 initPairs(r.theme)146 C.bkgd(C.chtype(C.COLOR_PAIR(C.int(ColNormal.index()))))147 _colorFn = attrColored148 } else {149 initTheme(r.theme, nil, r.forceBlack)150 _colorFn = attrMono151 }152153 C.nodelay(C.stdscr, true)154 ch := C.getch()155 if ch != C.ERR {156 C.ungetch(ch)157 }158 C.nodelay(C.stdscr, false)159}160161func initPairs(theme *ColorTheme) {162 C.assume_default_colors(C.int(theme.Fg), C.int(theme.Bg))163 for _, pair := range []ColorPair{164 ColNormal,165 ColPrompt,166 ColMatch,167 ColCurrent,168 ColCurrentMatch,169 ColSpinner,170 ColInfo,171 ColCursor,172 ColSelected,173 ColHeader,174 ColBorder} {175 C.init_pair(C.short(pair.index()), C.short(pair.Fg()), C.short(pair.Bg()))176 }177}178179func (r *FullscreenRenderer) Pause(bool) {180 C.endwin()181}182183func (r *FullscreenRenderer) Resume(bool) {184}185186func (r *FullscreenRenderer) Close() {187 C.endwin()188 C.delscreen(_screen)189}190191func (r *FullscreenRenderer) NewWindow(top int, left int, width int, height int, borderStyle BorderStyle) Window {192 win := C.newwin(C.int(height), C.int(width), C.int(top), C.int(left))193 if r.theme != nil {194 C.wbkgd(win, C.chtype(C.COLOR_PAIR(C.int(ColNormal.index()))))195 }196 // FIXME Does not implement BorderHorizontal197 if borderStyle != BorderNone {198 pair, attr := _colorFn(ColBorder, 0)199 C.wcolor_set(win, pair, nil)200 C.wattron(win, attr)201 C.box(win, 0, 0)202 C.wattroff(win, attr)203 C.wcolor_set(win, 0, nil)204 }205206 return &CursesWindow{207 impl: win,208 top: top,209 left: left,210 width: width,211 height: height,212 }213}214215func attrColored(color ColorPair, a Attr) (C.short, C.int) {216 return C.short(color.index()), C.int(a)217}218219func attrMono(color ColorPair, a Attr) (C.short, C.int) {220 return 0, C.int(attrFor(color, a))221}222223func (r *FullscreenRenderer) MaxX() int {224 return int(C.COLS)225}226227func (r *FullscreenRenderer) MaxY() int {228 return int(C.LINES)229}230231func (w *CursesWindow) Close() {232 C.delwin(w.impl)233}234235func (w *CursesWindow) Enclose(y int, x int) bool {236 return bool(C.wenclose(w.impl, C.int(y), C.int(x)))237}238239func (w *CursesWindow) Move(y int, x int) {240 C.wmove(w.impl, C.int(y), C.int(x))241}242243func (w *CursesWindow) MoveAndClear(y int, x int) {244 w.Move(y, x)245 C.wclrtoeol(w.impl)246}247248func (w *CursesWindow) Print(text string) {249 C.waddstr(w.impl, C.CString(strings.Map(func(r rune) rune {250 if r < 32 {251 return -1252 }253 return r254 }, text)))255}256257func (w *CursesWindow) CPrint(color ColorPair, attr Attr, text string) {258 p, a := _colorFn(color, attr)259 C.wcolor_set(w.impl, p, nil)260 C.wattron(w.impl, a)261 w.Print(text)262 C.wattroff(w.impl, a)263 C.wcolor_set(w.impl, 0, nil)264}265266func (r *FullscreenRenderer) Clear() {267 C.clear()268 C.endwin()269}270271func (r *FullscreenRenderer) Refresh() {272 C.refresh()273}274275func (w *CursesWindow) Erase() {276 C.werase(w.impl)277}278279func (w *CursesWindow) X() int {280 return int(C.c_getcurx(w.impl))281}282283func (r *FullscreenRenderer) DoesAutoWrap() bool {284 return true285}286287func (r *FullscreenRenderer) IsOptimized() bool {288 return true289}290291func (w *CursesWindow) Fill(str string) FillReturn {292 if C.waddstr(w.impl, C.CString(str)) == C.OK {293 return FillContinue294 }295 return FillSuspend296}297298func (w *CursesWindow) CFill(fg Color, bg Color, attr Attr, str string) FillReturn {299 index := ColorPair{fg, bg, -1}.index()300 C.wcolor_set(w.impl, C.short(index), nil)301 C.wattron(w.impl, C.int(attr))302 ret := w.Fill(str)303 C.wattroff(w.impl, C.int(attr))304 C.wcolor_set(w.impl, 0, nil)305 return ret306}307308func (r *FullscreenRenderer) RefreshWindows(windows []Window) {309 for _, w := range windows {310 w.Refresh()311 }312 C.doupdate()313}314315func (p ColorPair) index() int16 {316 if p.id >= 0 {317 return p.id318 }319320 // ncurses does not support 24-bit colors321 if p.is24() {322 return ColDefault.index()323 }324325 key := p.key()326 if found, prs := _colorMap[key]; prs {327 return found328 }329330 id := int16(len(_colorMap)) + ColUser.id331 C.init_pair(C.short(id), C.short(p.Fg()), C.short(p.Bg()))332 _colorMap[key] = id333 return id334}335336func consume(expects ...rune) bool {337 for _, r := range expects {338 if int(C.getch()) != int(r) {339 return false340 }341 }342 return true343}344345func escSequence() Event {346 C.nodelay(C.stdscr, true)347 defer func() {348 C.nodelay(C.stdscr, false)349 }()350 c := C.getch()351 switch c {352 case C.ERR:353 return Event{ESC, 0, nil}354 case CtrlM:355 return Event{CtrlAltM, 0, nil}356 case '/':357 return Event{AltSlash, 0, nil}358 case ' ':359 return Event{AltSpace, 0, nil}360 case 127, C.KEY_BACKSPACE:361 return Event{AltBS, 0, nil}362 case '[':363 // Bracketed paste mode (printf "\e[?2004h")364 // \e[200~ TEXT \e[201~365 if consume('2', '0', '0', '~') {366 return Event{Invalid, 0, nil}367 }368 }369 if c >= 'a' && c <= 'z' {370 return Event{AltA + int(c) - 'a', 0, nil}371 }372373 if c >= '0' && c <= '9' {374 return Event{Alt0 + int(c) - '0', 0, nil}375 }376377 // Don't care. Ignore the rest.378 for ; c != C.ERR; c = C.getch() {379 }380 return Event{Invalid, 0, nil}381}382383func (r *FullscreenRenderer) GetChar() Event {384 c := C.getch()385 switch c {386 case C.ERR:387 // Unexpected error from blocking read388 r.Close()389 errorExit("Failed to read /dev/tty")390 case C.KEY_UP:391 return Event{Up, 0, nil}392 case C.KEY_DOWN:393 return Event{Down, 0, nil}394 case C.KEY_LEFT:395 return Event{Left, 0, nil}396 case C.KEY_RIGHT:397 return Event{Right, 0, nil}398 case C.KEY_HOME:399 return Event{Home, 0, nil}400 case C.KEY_END:401 return Event{End, 0, nil}402 case C.KEY_BACKSPACE:403 return Event{BSpace, 0, nil}404 case C.KEY_F0 + 1:405 return Event{F1, 0, nil}406 case C.KEY_F0 + 2:407 return Event{F2, 0, nil}408 case C.KEY_F0 + 3:409 return Event{F3, 0, nil}410 case C.KEY_F0 + 4:411 return Event{F4, 0, nil}412 case C.KEY_F0 + 5:413 return Event{F5, 0, nil}414 case C.KEY_F0 + 6:415 return Event{F6, 0, nil}416 case C.KEY_F0 + 7:417 return Event{F7, 0, nil}418 case C.KEY_F0 + 8:419 return Event{F8, 0, nil}420 case C.KEY_F0 + 9:421 return Event{F9, 0, nil}422 case C.KEY_F0 + 10:423 return Event{F10, 0, nil}424 case C.KEY_F0 + 11:425 return Event{F11, 0, nil}426 case C.KEY_F0 + 12:427 return Event{F12, 0, nil}428 case C.KEY_DC:429 return Event{Del, 0, nil}430 case C.KEY_PPAGE:431 return Event{PgUp, 0, nil}432 case C.KEY_NPAGE:433 return Event{PgDn, 0, nil}434 case C.KEY_BTAB:435 return Event{BTab, 0, nil}436 case C.KEY_ENTER:437 return Event{CtrlM, 0, nil}438 case C.KEY_SLEFT:439 return Event{SLeft, 0, nil}440 case C.KEY_SRIGHT:441 return Event{SRight, 0, nil}442 case C.KEY_MOUSE:443 var me C.MEVENT444 if C.getmouse(&me) != C.ERR {445 mod := ((me.bstate & C.BUTTON_SHIFT) | (me.bstate & C.BUTTON_CTRL) | (me.bstate & C.BUTTON_ALT)) > 0446 x := int(me.x)447 y := int(me.y)448 /* Cannot use BUTTON1_DOUBLE_CLICKED due to mouseinterval(0) */449 if (me.bstate & C.BUTTON1_PRESSED) > 0 {450 now := time.Now()451 if now.Sub(r.prevDownTime) < doubleClickDuration {452 r.clickY = append(r.clickY, y)453 } else {454 r.clickY = []int{y}455 r.prevDownTime = now456 }457 return Event{Mouse, 0, &MouseEvent{y, x, 0, true, false, mod}}458 } else if (me.bstate & C.BUTTON1_RELEASED) > 0 {459 double := false460 if len(r.clickY) > 1 && r.clickY[0] == r.clickY[1] &&461 time.Now().Sub(r.prevDownTime) < doubleClickDuration {462 double = true463 }464 return Event{Mouse, 0, &MouseEvent{y, x, 0, false, double, mod}}465 } else if (me.bstate&0x8000000) > 0 || (me.bstate&0x80) > 0 {466 return Event{Mouse, 0, &MouseEvent{y, x, -1, false, false, mod}}467 } else if (me.bstate & C.BUTTON4_PRESSED) > 0 {468 return Event{Mouse, 0, &MouseEvent{y, x, 1, false, false, mod}}469 }470 }471 return Event{Invalid, 0, nil}472 case C.KEY_RESIZE:473 return Event{Resize, 0, nil}474 case ESC:475 return escSequence()476 case 127:477 return Event{BSpace, 0, nil}478 case 0:479 return Event{CtrlSpace, 0, nil}480 }481 // CTRL-A ~ CTRL-Z482 if c >= CtrlA && c <= CtrlZ {483 return Event{int(c), 0, nil}484 }485486 // Multi-byte character487 buffer := []byte{byte(c)}488 for {489 r, _ := utf8.DecodeRune(buffer)490 if r != utf8.RuneError {491 return Event{Rune, r, nil}492 }493494 c := C.getch()495 if c == C.ERR {496 break497 }498 if c >= C.KEY_CODE_YES {499 C.ungetch(c)500 break501 }502 buffer = append(buffer, byte(c))503 }504 return Event{Invalid, 0, nil}505}