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
Provenance
- Commit
e3e7b33- Tree
c83144b- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
655dfb8
1 file changed+0 added−505 removed
deleted:src/tui/ncurses.go-505 removed
1-// +build ncurses2-// +build !windows3-// +build !tcell4-5-package tui6-7-/*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-mismatch13-14-FILE* c_tty() {15- return fopen("/dev/tty", "r");16-}17-18-SCREEN* c_newterm(FILE* tty) {19- return newterm(NULL, stderr, tty);20-}21-22-int c_getcurx(WINDOW* win) {23- return getcurx(win);24-}25-*/26-import "C"27-28-import (29- "os"30- "strconv"31- "strings"32- "time"33- "unicode/utf8"34-)35-36-func HasFullscreenRenderer() bool {37- return true38-}39-40-type Attr C.uint41-42-type CursesWindow struct {43- impl *C.WINDOW44- top int45- left int46- width int47- height int48-}49-50-func (w *CursesWindow) Top() int {51- return w.top52-}53-54-func (w *CursesWindow) Left() int {55- return w.left56-}57-58-func (w *CursesWindow) Width() int {59- return w.width60-}61-62-func (w *CursesWindow) Height() int {63- return w.height64-}65-66-func (w *CursesWindow) Refresh() {67- C.wnoutrefresh(w.impl)68-}69-70-func (w *CursesWindow) FinishFill() {71- // NO-OP72-}73-74-const (75- Bold Attr = C.A_BOLD76- Dim = C.A_DIM77- Blink = C.A_BLINK78- Reverse = C.A_REVERSE79- Underline = C.A_UNDERLINE80-)81-82-var Italic Attr = C.A_VERTICAL << 1 // FIXME83-84-const (85- AttrRegular Attr = 086-)87-88-var (89- _screen *C.SCREEN90- _colorMap map[int]int1691- _colorFn func(ColorPair, Attr) (C.short, C.int)92-)93-94-func init() {95- _colorMap = make(map[int]int16)96- if strings.HasPrefix(C.GoString(C.curses_version()), "ncurses 5") {97- Italic = C.A_NORMAL98- }99-}100-101-func (a Attr) Merge(b Attr) Attr {102- return a | b103-}104-105-func (r *FullscreenRenderer) defaultTheme() *ColorTheme {106- if C.tigetnum(C.CString("colors")) >= 256 {107- return Dark256108- }109- return Default16110-}111-112-func (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)131-132- 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))141-142- 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- }152-153- 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-}160-161-func 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-}178-179-func (r *FullscreenRenderer) Pause(bool) {180- C.endwin()181-}182-183-func (r *FullscreenRenderer) Resume(bool) {184-}185-186-func (r *FullscreenRenderer) Close() {187- C.endwin()188- C.delscreen(_screen)189-}190-191-func (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- }205-206- return &CursesWindow{207- impl: win,208- top: top,209- left: left,210- width: width,211- height: height,212- }213-}214-215-func attrColored(color ColorPair, a Attr) (C.short, C.int) {216- return C.short(color.index()), C.int(a)217-}218-219-func attrMono(color ColorPair, a Attr) (C.short, C.int) {220- return 0, C.int(attrFor(color, a))221-}222-223-func (r *FullscreenRenderer) MaxX() int {224- return int(C.COLS)225-}226-227-func (r *FullscreenRenderer) MaxY() int {228- return int(C.LINES)229-}230-231-func (w *CursesWindow) Close() {232- C.delwin(w.impl)233-}234-235-func (w *CursesWindow) Enclose(y int, x int) bool {236- return bool(C.wenclose(w.impl, C.int(y), C.int(x)))237-}238-239-func (w *CursesWindow) Move(y int, x int) {240- C.wmove(w.impl, C.int(y), C.int(x))241-}242-243-func (w *CursesWindow) MoveAndClear(y int, x int) {244- w.Move(y, x)245- C.wclrtoeol(w.impl)246-}247-248-func (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-}256-257-func (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-}265-266-func (r *FullscreenRenderer) Clear() {267- C.clear()268- C.endwin()269-}270-271-func (r *FullscreenRenderer) Refresh() {272- C.refresh()273-}274-275-func (w *CursesWindow) Erase() {276- C.werase(w.impl)277-}278-279-func (w *CursesWindow) X() int {280- return int(C.c_getcurx(w.impl))281-}282-283-func (r *FullscreenRenderer) DoesAutoWrap() bool {284- return true285-}286-287-func (r *FullscreenRenderer) IsOptimized() bool {288- return true289-}290-291-func (w *CursesWindow) Fill(str string) FillReturn {292- if C.waddstr(w.impl, C.CString(str)) == C.OK {293- return FillContinue294- }295- return FillSuspend296-}297-298-func (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-}307-308-func (r *FullscreenRenderer) RefreshWindows(windows []Window) {309- for _, w := range windows {310- w.Refresh()311- }312- C.doupdate()313-}314-315-func (p ColorPair) index() int16 {316- if p.id >= 0 {317- return p.id318- }319-320- // ncurses does not support 24-bit colors321- if p.is24() {322- return ColDefault.index()323- }324-325- key := p.key()326- if found, prs := _colorMap[key]; prs {327- return found328- }329-330- 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-}335-336-func consume(expects ...rune) bool {337- for _, r := range expects {338- if int(C.getch()) != int(r) {339- return false340- }341- }342- return true343-}344-345-func 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- }372-373- if c >= '0' && c <= '9' {374- return Event{Alt0 + int(c) - '0', 0, nil}375- }376-377- // Don't care. Ignore the rest.378- for ; c != C.ERR; c = C.getch() {379- }380- return Event{Invalid, 0, nil}381-}382-383-func (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- }485-486- // 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- }493-494- 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-}