Prepare for termbox/windows build
Before adoption: this commit came in with the history the project adopted, so no landing records it.
`TAGS=termbox make` (or `go build -tags termbox`)
authored by Junegunn Choi · committed by Junegunn Choi · parent 2cff00d · Browse files · 23 files·+1271 added−1006 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
23 files+1271 added−1006 removed
- other:+1271 added−1006 removed
Provenance
- Commit
0c573b3- Tree
1fba66a- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
2cff00d
23 files changed+1271 added−1006 removed
deleted:src/curses/curses_test.go-14 removed
1-package curses2-3-import (4- "testing"5-)6-7-func TestPairFor(t *testing.T) {8- if PairFor(30, 50) != PairFor(30, 50) {9- t.Fail()10- }11- if PairFor(-1, 10) != PairFor(-1, 10) {12- t.Fail()13- }14-}deleted:src/curses/curses.go-714 removed
1-package curses2-3-/*4-#include <ncurses.h>5-#include <locale.h>6-#cgo !static LDFLAGS: -lncurses7-#cgo static LDFLAGS: -l:libncursesw.a -l:libtinfo.a -l:libgpm.a -ldl8-#cgo android static LDFLAGS: -l:libncurses.a -fPIE -march=armv7-a -mfpu=neon -mhard-float -Wl,--no-warn-mismatch9-10-SCREEN *c_newterm () {11- return newterm(NULL, stderr, stdin);12-}13-14-*/15-import "C"16-17-import (18- "fmt"19- "os"20- "strings"21- "syscall"22- "time"23- "unicode/utf8"24-)25-26-const (27- Bold = C.A_BOLD28- Dim = C.A_DIM29- Blink = C.A_BLINK30- Reverse = C.A_REVERSE31- Underline = C.A_UNDERLINE32-)33-34-type Attr C.int35-36-// Types of user action37-const (38- Rune = iota39-40- CtrlA41- CtrlB42- CtrlC43- CtrlD44- CtrlE45- CtrlF46- CtrlG47- CtrlH48- Tab49- CtrlJ50- CtrlK51- CtrlL52- CtrlM53- CtrlN54- CtrlO55- CtrlP56- CtrlQ57- CtrlR58- CtrlS59- CtrlT60- CtrlU61- CtrlV62- CtrlW63- CtrlX64- CtrlY65- CtrlZ66- ESC67-68- Invalid69- Mouse70- DoubleClick71-72- BTab73- BSpace74-75- Del76- PgUp77- PgDn78-79- Up80- Down81- Left82- Right83- Home84- End85-86- SLeft87- SRight88-89- F190- F291- F392- F493- F594- F695- F796- F897- F998- F1099-100- AltEnter101- AltSpace102- AltSlash103- AltBS104- AltA105- AltB106- AltC107- AltD108- AltE109- AltF110-111- AltZ = AltA + 'z' - 'a'112-)113-114-// Pallete115-const (116- _ = iota117- ColNormal118- ColPrompt119- ColMatch120- ColCurrent121- ColCurrentMatch122- ColSpinner123- ColInfo124- ColCursor125- ColSelected126- ColHeader127- ColBorder128- ColUser // Should be the last entry129-)130-131-const (132- doubleClickDuration = 500 * time.Millisecond133- colDefault = -1134- colUndefined = -2135-)136-137-type ColorTheme struct {138- Fg int16139- Bg int16140- DarkBg int16141- Prompt int16142- Match int16143- Current int16144- CurrentMatch int16145- Spinner int16146- Info int16147- Cursor int16148- Selected int16149- Header int16150- Border int16151-}152-153-type Event struct {154- Type int155- Char rune156- MouseEvent *MouseEvent157-}158-159-type MouseEvent struct {160- Y int161- X int162- S int163- Down bool164- Double bool165- Mod bool166-}167-168-var (169- _buf []byte170- _in *os.File171- _color bool172- _colorFn func(int, Attr) C.int173- _colorMap map[int]int174- _prevDownTime time.Time175- _clickY []int176- _screen *C.SCREEN177- Default16 *ColorTheme178- Dark256 *ColorTheme179- Light256 *ColorTheme180-)181-182-type Window struct {183- win *C.WINDOW184- Top int185- Left int186- Width int187- Height int188-}189-190-func NewWindow(top int, left int, width int, height int, border bool) *Window {191- win := C.newwin(C.int(height), C.int(width), C.int(top), C.int(left))192- if _color {193- C.wbkgd(win, C.chtype(C.COLOR_PAIR(ColNormal)))194- }195- if border {196- attr := _colorFn(ColBorder, 0)197- C.wattron(win, attr)198- C.box(win, 0, 0)199- C.wattroff(win, attr)200- }201-202- return &Window{203- win: win,204- Top: top,205- Left: left,206- Width: width,207- Height: height,208- }209-}210-211-func EmptyTheme() *ColorTheme {212- return &ColorTheme{213- Fg: colUndefined,214- Bg: colUndefined,215- DarkBg: colUndefined,216- Prompt: colUndefined,217- Match: colUndefined,218- Current: colUndefined,219- CurrentMatch: colUndefined,220- Spinner: colUndefined,221- Info: colUndefined,222- Cursor: colUndefined,223- Selected: colUndefined,224- Header: colUndefined,225- Border: colUndefined}226-}227-228-func init() {229- _prevDownTime = time.Unix(0, 0)230- _clickY = []int{}231- _colorMap = make(map[int]int)232- Default16 = &ColorTheme{233- Fg: colDefault,234- Bg: colDefault,235- DarkBg: C.COLOR_BLACK,236- Prompt: C.COLOR_BLUE,237- Match: C.COLOR_GREEN,238- Current: C.COLOR_YELLOW,239- CurrentMatch: C.COLOR_GREEN,240- Spinner: C.COLOR_GREEN,241- Info: C.COLOR_WHITE,242- Cursor: C.COLOR_RED,243- Selected: C.COLOR_MAGENTA,244- Header: C.COLOR_CYAN,245- Border: C.COLOR_BLACK}246- Dark256 = &ColorTheme{247- Fg: colDefault,248- Bg: colDefault,249- DarkBg: 236,250- Prompt: 110,251- Match: 108,252- Current: 254,253- CurrentMatch: 151,254- Spinner: 148,255- Info: 144,256- Cursor: 161,257- Selected: 168,258- Header: 109,259- Border: 59}260- Light256 = &ColorTheme{261- Fg: colDefault,262- Bg: colDefault,263- DarkBg: 251,264- Prompt: 25,265- Match: 66,266- Current: 237,267- CurrentMatch: 23,268- Spinner: 65,269- Info: 101,270- Cursor: 161,271- Selected: 168,272- Header: 31,273- Border: 145}274-}275-276-func attrColored(pair int, a Attr) C.int {277- var attr C.int278- if pair > 0 {279- attr = C.COLOR_PAIR(C.int(pair))280- }281- return attr | C.int(a)282-}283-284-func attrMono(pair int, a Attr) C.int {285- var attr C.int286- switch pair {287- case ColCurrent:288- if a&C.A_BOLD == C.A_BOLD {289- attr = C.A_REVERSE290- }291- case ColMatch:292- attr = C.A_UNDERLINE293- case ColCurrentMatch:294- attr = C.A_UNDERLINE | C.A_REVERSE295- }296- if a&C.A_BOLD == C.A_BOLD {297- attr = attr | C.A_BOLD298- }299- return attr300-}301-302-func MaxX() int {303- return int(C.COLS)304-}305-306-func MaxY() int {307- return int(C.LINES)308-}309-310-func getch(nonblock bool) int {311- b := make([]byte, 1)312- syscall.SetNonblock(int(_in.Fd()), nonblock)313- _, err := _in.Read(b)314- if err != nil {315- return -1316- }317- return int(b[0])318-}319-320-func Init(theme *ColorTheme, black bool, mouse bool) {321- {322- in, err := os.OpenFile("/dev/tty", syscall.O_RDONLY, 0)323- if err != nil {324- panic("Failed to open /dev/tty")325- }326- _in = in327- // Break STDIN328- // syscall.Dup2(int(in.Fd()), int(os.Stdin.Fd()))329- }330-331- C.setlocale(C.LC_ALL, C.CString(""))332- _screen = C.c_newterm()333- if _screen == nil {334- fmt.Println("Invalid $TERM: " + os.Getenv("TERM"))335- os.Exit(2)336- }337- C.set_term(_screen)338- if mouse {339- C.mousemask(C.ALL_MOUSE_EVENTS, nil)340- }341- C.noecho()342- C.raw() // stty dsusp undef343-344- _color = theme != nil345- if _color {346- C.start_color()347- var baseTheme *ColorTheme348- if C.tigetnum(C.CString("colors")) >= 256 {349- baseTheme = Dark256350- } else {351- baseTheme = Default16352- }353- initPairs(baseTheme, theme, black)354- C.bkgd(C.chtype(C.COLOR_PAIR(ColNormal)))355- _colorFn = attrColored356- } else {357- _colorFn = attrMono358- }359-}360-361-func override(baseTheme *ColorTheme, theme *ColorTheme) {362- o := func(a int16, b int16) int16 {363- if b == colUndefined {364- return a365- }366- return b367- }368- theme.Fg = o(baseTheme.Fg, theme.Fg)369- theme.Bg = o(baseTheme.Bg, theme.Bg)370- theme.DarkBg = o(baseTheme.DarkBg, theme.DarkBg)371- theme.Prompt = o(baseTheme.Prompt, theme.Prompt)372- theme.Match = o(baseTheme.Match, theme.Match)373- theme.Current = o(baseTheme.Current, theme.Current)374- theme.CurrentMatch = o(baseTheme.CurrentMatch, theme.CurrentMatch)375- theme.Spinner = o(baseTheme.Spinner, theme.Spinner)376- theme.Info = o(baseTheme.Info, theme.Info)377- theme.Cursor = o(baseTheme.Cursor, theme.Cursor)378- theme.Selected = o(baseTheme.Selected, theme.Selected)379- theme.Header = o(baseTheme.Header, theme.Header)380- theme.Border = o(baseTheme.Border, theme.Border)381-}382-383-func initPairs(baseTheme *ColorTheme, theme *ColorTheme, black bool) {384- if black {385- theme.Bg = C.COLOR_BLACK386- }387- // Updates theme388- override(baseTheme, theme)389-390- C.assume_default_colors(C.int(theme.Fg), C.int(theme.Bg))391- initPair := func(group C.short, fg int16, bg int16) {392- C.init_pair(group, C.short(fg), C.short(bg))393- }394- initPair(ColNormal, theme.Fg, theme.Bg)395- initPair(ColPrompt, theme.Prompt, theme.Bg)396- initPair(ColMatch, theme.Match, theme.Bg)397- initPair(ColCurrent, theme.Current, theme.DarkBg)398- initPair(ColCurrentMatch, theme.CurrentMatch, theme.DarkBg)399- initPair(ColSpinner, theme.Spinner, theme.Bg)400- initPair(ColInfo, theme.Info, theme.Bg)401- initPair(ColCursor, theme.Cursor, theme.DarkBg)402- initPair(ColSelected, theme.Selected, theme.DarkBg)403- initPair(ColHeader, theme.Header, theme.Bg)404- initPair(ColBorder, theme.Border, theme.Bg)405-}406-407-func Close() {408- C.endwin()409- C.delscreen(_screen)410-}411-412-func GetBytes() []byte {413- c := getch(false)414- _buf = append(_buf, byte(c))415-416- for {417- c = getch(true)418- if c == -1 {419- break420- }421- _buf = append(_buf, byte(c))422- }423-424- return _buf425-}426-427-// 27 (91 79) 77 type x y428-func mouseSequence(sz *int) Event {429- if len(_buf) < 6 {430- return Event{Invalid, 0, nil}431- }432- *sz = 6433- switch _buf[3] {434- case 32, 36, 40, 48, // mouse-down / shift / cmd / ctrl435- 35, 39, 43, 51: // mouse-up / shift / cmd / ctrl436- mod := _buf[3] >= 36437- down := _buf[3]%2 == 0438- x := int(_buf[4] - 33)439- y := int(_buf[5] - 33)440- double := false441- if down {442- now := time.Now()443- if now.Sub(_prevDownTime) < doubleClickDuration {444- _clickY = append(_clickY, y)445- } else {446- _clickY = []int{y}447- }448- _prevDownTime = now449- } else {450- if len(_clickY) > 1 && _clickY[0] == _clickY[1] &&451- time.Now().Sub(_prevDownTime) < doubleClickDuration {452- double = true453- }454- }455- return Event{Mouse, 0, &MouseEvent{y, x, 0, down, double, mod}}456- case 96, 100, 104, 112, // scroll-up / shift / cmd / ctrl457- 97, 101, 105, 113: // scroll-down / shift / cmd / ctrl458- mod := _buf[3] >= 100459- s := 1 - int(_buf[3]%2)*2460- x := int(_buf[4] - 33)461- y := int(_buf[5] - 33)462- return Event{Mouse, 0, &MouseEvent{y, x, s, false, false, mod}}463- }464- return Event{Invalid, 0, nil}465-}466-467-func escSequence(sz *int) Event {468- if len(_buf) < 2 {469- return Event{ESC, 0, nil}470- }471- *sz = 2472- switch _buf[1] {473- case 13:474- return Event{AltEnter, 0, nil}475- case 32:476- return Event{AltSpace, 0, nil}477- case 47:478- return Event{AltSlash, 0, nil}479- case 98:480- return Event{AltB, 0, nil}481- case 100:482- return Event{AltD, 0, nil}483- case 102:484- return Event{AltF, 0, nil}485- case 127:486- return Event{AltBS, 0, nil}487- case 91, 79:488- if len(_buf) < 3 {489- return Event{Invalid, 0, nil}490- }491- *sz = 3492- switch _buf[2] {493- case 68:494- return Event{Left, 0, nil}495- case 67:496- return Event{Right, 0, nil}497- case 66:498- return Event{Down, 0, nil}499- case 65:500- return Event{Up, 0, nil}501- case 90:502- return Event{BTab, 0, nil}503- case 72:504- return Event{Home, 0, nil}505- case 70:506- return Event{End, 0, nil}507- case 77:508- return mouseSequence(sz)509- case 80:510- return Event{F1, 0, nil}511- case 81:512- return Event{F2, 0, nil}513- case 82:514- return Event{F3, 0, nil}515- case 83:516- return Event{F4, 0, nil}517- case 49, 50, 51, 52, 53, 54:518- if len(_buf) < 4 {519- return Event{Invalid, 0, nil}520- }521- *sz = 4522- switch _buf[2] {523- case 50:524- if len(_buf) == 5 && _buf[4] == 126 {525- *sz = 5526- switch _buf[3] {527- case 48:528- return Event{F9, 0, nil}529- case 49:530- return Event{F10, 0, nil}531- }532- }533- // Bracketed paste mode \e[200~ / \e[201534- if _buf[3] == 48 && (_buf[4] == 48 || _buf[4] == 49) && _buf[5] == 126 {535- *sz = 6536- return Event{Invalid, 0, nil}537- }538- return Event{Invalid, 0, nil} // INS539- case 51:540- return Event{Del, 0, nil}541- case 52:542- return Event{End, 0, nil}543- case 53:544- return Event{PgUp, 0, nil}545- case 54:546- return Event{PgDn, 0, nil}547- case 49:548- switch _buf[3] {549- case 126:550- return Event{Home, 0, nil}551- case 53, 55, 56, 57:552- if len(_buf) == 5 && _buf[4] == 126 {553- *sz = 5554- switch _buf[3] {555- case 53:556- return Event{F5, 0, nil}557- case 55:558- return Event{F6, 0, nil}559- case 56:560- return Event{F7, 0, nil}561- case 57:562- return Event{F8, 0, nil}563- }564- }565- return Event{Invalid, 0, nil}566- case 59:567- if len(_buf) != 6 {568- return Event{Invalid, 0, nil}569- }570- *sz = 6571- switch _buf[4] {572- case 50:573- switch _buf[5] {574- case 68:575- return Event{Home, 0, nil}576- case 67:577- return Event{End, 0, nil}578- }579- case 53:580- switch _buf[5] {581- case 68:582- return Event{SLeft, 0, nil}583- case 67:584- return Event{SRight, 0, nil}585- }586- } // _buf[4]587- } // _buf[3]588- } // _buf[2]589- } // _buf[2]590- } // _buf[1]591- if _buf[1] >= 'a' && _buf[1] <= 'z' {592- return Event{AltA + int(_buf[1]) - 'a', 0, nil}593- }594- return Event{Invalid, 0, nil}595-}596-597-func GetChar() Event {598- if len(_buf) == 0 {599- _buf = GetBytes()600- }601- if len(_buf) == 0 {602- panic("Empty _buffer")603- }604-605- sz := 1606- defer func() {607- _buf = _buf[sz:]608- }()609-610- switch _buf[0] {611- case CtrlC:612- return Event{CtrlC, 0, nil}613- case CtrlG:614- return Event{CtrlG, 0, nil}615- case CtrlQ:616- return Event{CtrlQ, 0, nil}617- case 127:618- return Event{BSpace, 0, nil}619- case ESC:620- return escSequence(&sz)621- }622-623- // CTRL-A ~ CTRL-Z624- if _buf[0] <= CtrlZ {625- return Event{int(_buf[0]), 0, nil}626- }627- r, rsz := utf8.DecodeRune(_buf)628- if r == utf8.RuneError {629- return Event{ESC, 0, nil}630- }631- sz = rsz632- return Event{Rune, r, nil}633-}634-635-func (w *Window) Close() {636- C.delwin(w.win)637-}638-639-func (w *Window) Enclose(y int, x int) bool {640- return bool(C.wenclose(w.win, C.int(y), C.int(x)))641-}642-643-func (w *Window) Move(y int, x int) {644- C.wmove(w.win, C.int(y), C.int(x))645-}646-647-func (w *Window) MoveAndClear(y int, x int) {648- w.Move(y, x)649- C.wclrtoeol(w.win)650-}651-652-func (w *Window) Print(text string) {653- C.waddstr(w.win, C.CString(strings.Map(func(r rune) rune {654- if r < 32 {655- return -1656- }657- return r658- }, text)))659-}660-661-func (w *Window) CPrint(pair int, a Attr, text string) {662- attr := _colorFn(pair, a)663- C.wattron(w.win, attr)664- w.Print(text)665- C.wattroff(w.win, attr)666-}667-668-func Clear() {669- C.clear()670-}671-672-func Endwin() {673- C.endwin()674-}675-676-func Refresh() {677- C.refresh()678-}679-680-func (w *Window) Erase() {681- C.werase(w.win)682-}683-684-func (w *Window) Fill(str string) bool {685- return C.waddstr(w.win, C.CString(str)) == C.OK686-}687-688-func (w *Window) CFill(str string, fg int, bg int, a Attr) bool {689- attr := _colorFn(PairFor(fg, bg), a)690- C.wattron(w.win, attr)691- ret := w.Fill(str)692- C.wattroff(w.win, attr)693- return ret694-}695-696-func (w *Window) Refresh() {697- C.wnoutrefresh(w.win)698-}699-700-func DoUpdate() {701- C.doupdate()702-}703-704-func PairFor(fg int, bg int) int {705- key := (fg << 8) + bg706- if found, prs := _colorMap[key]; prs {707- return found708- }709-710- id := len(_colorMap) + ColUser711- C.init_pair(C.short(id), C.short(fg), C.short(bg))712- _colorMap[key] = id713- return id714-}added:src/tui/ncurses.go+513 added
1+// +build !windows2+// +build !termbox3+4+package tui5+6+/*7+#include <ncurses.h>8+#include <locale.h>9+#cgo !static LDFLAGS: -lncurses10+#cgo static LDFLAGS: -l:libncursesw.a -l:libtinfo.a -l:libgpm.a -ldl11+#cgo android static LDFLAGS: -l:libncurses.a -fPIE -march=armv7-a -mfpu=neon -mhard-float -Wl,--no-warn-mismatch12+13+SCREEN *c_newterm () {14+ return newterm(NULL, stderr, stdin);15+}16+*/17+import "C"18+19+import (20+ "fmt"21+ "os"22+ "strings"23+ "syscall"24+ "time"25+ "unicode/utf8"26+)27+28+type ColorPair int1629+type Attr C.int30+type WindowImpl C.WINDOW31+32+const (33+ Bold = C.A_BOLD34+ Dim = C.A_DIM35+ Blink = C.A_BLINK36+ Reverse = C.A_REVERSE37+ Underline = C.A_UNDERLINE38+)39+40+const (41+ AttrRegular Attr = 042+)43+44+// Pallete45+const (46+ ColDefault ColorPair = iota47+ ColNormal48+ ColPrompt49+ ColMatch50+ ColCurrent51+ ColCurrentMatch52+ ColSpinner53+ ColInfo54+ ColCursor55+ ColSelected56+ ColHeader57+ ColBorder58+ ColUser // Should be the last entry59+)60+61+var (62+ _in *os.File63+ _screen *C.SCREEN64+ _colorMap map[int]ColorPair65+ _colorFn func(ColorPair, Attr) C.int66+)67+68+func init() {69+ _colorMap = make(map[int]ColorPair)70+}71+72+func (a Attr) Merge(b Attr) Attr {73+ return a | b74+}75+76+func DefaultTheme() *ColorTheme {77+ if C.tigetnum(C.CString("colors")) >= 256 {78+ return Dark25679+ }80+ return Default1681+}82+83+func Init(theme *ColorTheme, black bool, mouse bool) {84+ {85+ in, err := os.OpenFile("/dev/tty", syscall.O_RDONLY, 0)86+ if err != nil {87+ panic("Failed to open /dev/tty")88+ }89+ _in = in90+ // Break STDIN91+ // syscall.Dup2(int(in.Fd()), int(os.Stdin.Fd()))92+ }93+94+ C.setlocale(C.LC_ALL, C.CString(""))95+ _screen = C.c_newterm()96+ if _screen == nil {97+ fmt.Println("Invalid $TERM: " + os.Getenv("TERM"))98+ os.Exit(2)99+ }100+ C.set_term(_screen)101+ if mouse {102+ C.mousemask(C.ALL_MOUSE_EVENTS, nil)103+ }104+ C.noecho()105+ C.raw() // stty dsusp undef106+107+ _color = theme != nil108+ if _color {109+ C.start_color()110+ InitTheme(theme, black)111+ initPairs(theme)112+ C.bkgd(C.chtype(C.COLOR_PAIR(C.int(ColNormal))))113+ _colorFn = attrColored114+ } else {115+ _colorFn = attrMono116+ }117+}118+119+func initPairs(theme *ColorTheme) {120+ C.assume_default_colors(C.int(theme.Fg), C.int(theme.Bg))121+ initPair := func(group ColorPair, fg Color, bg Color) {122+ C.init_pair(C.short(group), C.short(fg), C.short(bg))123+ }124+ initPair(ColNormal, theme.Fg, theme.Bg)125+ initPair(ColPrompt, theme.Prompt, theme.Bg)126+ initPair(ColMatch, theme.Match, theme.Bg)127+ initPair(ColCurrent, theme.Current, theme.DarkBg)128+ initPair(ColCurrentMatch, theme.CurrentMatch, theme.DarkBg)129+ initPair(ColSpinner, theme.Spinner, theme.Bg)130+ initPair(ColInfo, theme.Info, theme.Bg)131+ initPair(ColCursor, theme.Cursor, theme.DarkBg)132+ initPair(ColSelected, theme.Selected, theme.DarkBg)133+ initPair(ColHeader, theme.Header, theme.Bg)134+ initPair(ColBorder, theme.Border, theme.Bg)135+}136+137+func Pause() {138+ C.endwin()139+}140+141+func Close() {142+ C.endwin()143+ C.delscreen(_screen)144+}145+146+func NewWindow(top int, left int, width int, height int, border bool) *Window {147+ win := C.newwin(C.int(height), C.int(width), C.int(top), C.int(left))148+ if _color {149+ C.wbkgd(win, C.chtype(C.COLOR_PAIR(C.int(ColNormal))))150+ }151+ if border {152+ attr := _colorFn(ColBorder, 0)153+ C.wattron(win, attr)154+ C.box(win, 0, 0)155+ C.wattroff(win, attr)156+ }157+158+ return &Window{159+ impl: (*WindowImpl)(win),160+ Top: top,161+ Left: left,162+ Width: width,163+ Height: height,164+ }165+}166+167+func attrColored(pair ColorPair, a Attr) C.int {168+ var attr C.int169+ if pair > 0 {170+ attr = C.COLOR_PAIR(C.int(pair))171+ }172+ return attr | C.int(a)173+}174+175+func attrMono(pair ColorPair, a Attr) C.int {176+ var attr C.int177+ switch pair {178+ case ColCurrent:179+ if C.int(a)&C.A_BOLD == C.A_BOLD {180+ attr = C.A_REVERSE181+ }182+ case ColMatch:183+ attr = C.A_UNDERLINE184+ case ColCurrentMatch:185+ attr = C.A_UNDERLINE | C.A_REVERSE186+ }187+ if C.int(a)&C.A_BOLD == C.A_BOLD {188+ attr = attr | C.A_BOLD189+ }190+ return attr191+}192+193+func MaxX() int {194+ return int(C.COLS)195+}196+197+func MaxY() int {198+ return int(C.LINES)199+}200+201+func (w *Window) win() *C.WINDOW {202+ return (*C.WINDOW)(w.impl)203+}204+205+func (w *Window) Close() {206+ C.delwin(w.win())207+}208+209+func (w *Window) Enclose(y int, x int) bool {210+ return bool(C.wenclose(w.win(), C.int(y), C.int(x)))211+}212+213+func (w *Window) Move(y int, x int) {214+ C.wmove(w.win(), C.int(y), C.int(x))215+}216+217+func (w *Window) MoveAndClear(y int, x int) {218+ w.Move(y, x)219+ C.wclrtoeol(w.win())220+}221+222+func (w *Window) Print(text string) {223+ C.waddstr(w.win(), C.CString(strings.Map(func(r rune) rune {224+ if r < 32 {225+ return -1226+ }227+ return r228+ }, text)))229+}230+231+func (w *Window) CPrint(pair ColorPair, a Attr, text string) {232+ attr := _colorFn(pair, a)233+ C.wattron(w.win(), attr)234+ w.Print(text)235+ C.wattroff(w.win(), attr)236+}237+238+func Clear() {239+ C.clear()240+ C.endwin()241+}242+243+func Refresh() {244+ C.refresh()245+}246+247+func (w *Window) Erase() {248+ C.werase(w.win())249+}250+251+func (w *Window) Fill(str string) bool {252+ return C.waddstr(w.win(), C.CString(str)) == C.OK253+}254+255+func (w *Window) CFill(str string, fg Color, bg Color, a Attr) bool {256+ attr := _colorFn(PairFor(fg, bg), a)257+ C.wattron(w.win(), attr)258+ ret := w.Fill(str)259+ C.wattroff(w.win(), attr)260+ return ret261+}262+263+func RefreshWindows(windows []*Window) {264+ for _, w := range windows {265+ C.wnoutrefresh(w.win())266+ }267+ C.doupdate()268+}269+270+func PairFor(fg Color, bg Color) ColorPair {271+ key := (int(fg) << 8) + int(bg)272+ if found, prs := _colorMap[key]; prs {273+ return found274+ }275+276+ id := ColorPair(len(_colorMap) + int(ColUser))277+ C.init_pair(C.short(id), C.short(fg), C.short(bg))278+ _colorMap[key] = id279+ return id280+}281+282+func getch(nonblock bool) int {283+ b := make([]byte, 1)284+ syscall.SetNonblock(int(_in.Fd()), nonblock)285+ _, err := _in.Read(b)286+ if err != nil {287+ return -1288+ }289+ return int(b[0])290+}291+292+func GetBytes() []byte {293+ c := getch(false)294+ _buf = append(_buf, byte(c))295+296+ for {297+ c = getch(true)298+ if c == -1 {299+ break300+ }301+ _buf = append(_buf, byte(c))302+ }303+304+ return _buf305+}306+307+// 27 (91 79) 77 type x y308+func mouseSequence(sz *int) Event {309+ if len(_buf) < 6 {310+ return Event{Invalid, 0, nil}311+ }312+ *sz = 6313+ switch _buf[3] {314+ case 32, 36, 40, 48, // mouse-down / shift / cmd / ctrl315+ 35, 39, 43, 51: // mouse-up / shift / cmd / ctrl316+ mod := _buf[3] >= 36317+ down := _buf[3]%2 == 0318+ x := int(_buf[4] - 33)319+ y := int(_buf[5] - 33)320+ double := false321+ if down {322+ now := time.Now()323+ if now.Sub(_prevDownTime) < doubleClickDuration {324+ _clickY = append(_clickY, y)325+ } else {326+ _clickY = []int{y}327+ }328+ _prevDownTime = now329+ } else {330+ if len(_clickY) > 1 && _clickY[0] == _clickY[1] &&331+ time.Now().Sub(_prevDownTime) < doubleClickDuration {332+ double = true333+ }334+ }335+ return Event{Mouse, 0, &MouseEvent{y, x, 0, down, double, mod}}336+ case 96, 100, 104, 112, // scroll-up / shift / cmd / ctrl337+ 97, 101, 105, 113: // scroll-down / shift / cmd / ctrl338+ mod := _buf[3] >= 100339+ s := 1 - int(_buf[3]%2)*2340+ x := int(_buf[4] - 33)341+ y := int(_buf[5] - 33)342+ return Event{Mouse, 0, &MouseEvent{y, x, s, false, false, mod}}343+ }344+ return Event{Invalid, 0, nil}345+}346+347+func escSequence(sz *int) Event {348+ if len(_buf) < 2 {349+ return Event{ESC, 0, nil}350+ }351+ *sz = 2352+ switch _buf[1] {353+ case 13:354+ return Event{AltEnter, 0, nil}355+ case 32:356+ return Event{AltSpace, 0, nil}357+ case 47:358+ return Event{AltSlash, 0, nil}359+ case 98:360+ return Event{AltB, 0, nil}361+ case 100:362+ return Event{AltD, 0, nil}363+ case 102:364+ return Event{AltF, 0, nil}365+ case 127:366+ return Event{AltBS, 0, nil}367+ case 91, 79:368+ if len(_buf) < 3 {369+ return Event{Invalid, 0, nil}370+ }371+ *sz = 3372+ switch _buf[2] {373+ case 68:374+ return Event{Left, 0, nil}375+ case 67:376+ return Event{Right, 0, nil}377+ case 66:378+ return Event{Down, 0, nil}379+ case 65:380+ return Event{Up, 0, nil}381+ case 90:382+ return Event{BTab, 0, nil}383+ case 72:384+ return Event{Home, 0, nil}385+ case 70:386+ return Event{End, 0, nil}387+ case 77:388+ return mouseSequence(sz)389+ case 80:390+ return Event{F1, 0, nil}391+ case 81:392+ return Event{F2, 0, nil}393+ case 82:394+ return Event{F3, 0, nil}395+ case 83:396+ return Event{F4, 0, nil}397+ case 49, 50, 51, 52, 53, 54:398+ if len(_buf) < 4 {399+ return Event{Invalid, 0, nil}400+ }401+ *sz = 4402+ switch _buf[2] {403+ case 50:404+ if len(_buf) == 5 && _buf[4] == 126 {405+ *sz = 5406+ switch _buf[3] {407+ case 48:408+ return Event{F9, 0, nil}409+ case 49:410+ return Event{F10, 0, nil}411+ }412+ }413+ // Bracketed paste mode \e[200~ / \e[201414+ if _buf[3] == 48 && (_buf[4] == 48 || _buf[4] == 49) && _buf[5] == 126 {415+ *sz = 6416+ return Event{Invalid, 0, nil}417+ }418+ return Event{Invalid, 0, nil} // INS419+ case 51:420+ return Event{Del, 0, nil}421+ case 52:422+ return Event{End, 0, nil}423+ case 53:424+ return Event{PgUp, 0, nil}425+ case 54:426+ return Event{PgDn, 0, nil}427+ case 49:428+ switch _buf[3] {429+ case 126:430+ return Event{Home, 0, nil}431+ case 53, 55, 56, 57:432+ if len(_buf) == 5 && _buf[4] == 126 {433+ *sz = 5434+ switch _buf[3] {435+ case 53:436+ return Event{F5, 0, nil}437+ case 55:438+ return Event{F6, 0, nil}439+ case 56:440+ return Event{F7, 0, nil}441+ case 57:442+ return Event{F8, 0, nil}443+ }444+ }445+ return Event{Invalid, 0, nil}446+ case 59:447+ if len(_buf) != 6 {448+ return Event{Invalid, 0, nil}449+ }450+ *sz = 6451+ switch _buf[4] {452+ case 50:453+ switch _buf[5] {454+ case 68:455+ return Event{Home, 0, nil}456+ case 67:457+ return Event{End, 0, nil}458+ }459+ case 53:460+ switch _buf[5] {461+ case 68:462+ return Event{SLeft, 0, nil}463+ case 67:464+ return Event{SRight, 0, nil}465+ }466+ } // _buf[4]467+ } // _buf[3]468+ } // _buf[2]469+ } // _buf[2]470+ } // _buf[1]471+ if _buf[1] >= 'a' && _buf[1] <= 'z' {472+ return Event{AltA + int(_buf[1]) - 'a', 0, nil}473+ }474+ return Event{Invalid, 0, nil}475+}476+477+func GetChar() Event {478+ if len(_buf) == 0 {479+ _buf = GetBytes()480+ }481+ if len(_buf) == 0 {482+ panic("Empty _buffer")483+ }484+485+ sz := 1486+ defer func() {487+ _buf = _buf[sz:]488+ }()489+490+ switch _buf[0] {491+ case CtrlC:492+ return Event{CtrlC, 0, nil}493+ case CtrlG:494+ return Event{CtrlG, 0, nil}495+ case CtrlQ:496+ return Event{CtrlQ, 0, nil}497+ case 127:498+ return Event{BSpace, 0, nil}499+ case ESC:500+ return escSequence(&sz)501+ }502+503+ // CTRL-A ~ CTRL-Z504+ if _buf[0] <= CtrlZ {505+ return Event{int(_buf[0]), 0, nil}506+ }507+ r, rsz := utf8.DecodeRune(_buf)508+ if r == utf8.RuneError {509+ return Event{ESC, 0, nil}510+ }511+ sz = rsz512+ return Event{Rune, r, nil}513+}added:src/tui/termbox.go+151 added
1+// +build termbox windows2+3+package tui4+5+import (6+ "github.com/nsf/termbox-go"7+)8+9+type ColorPair [2]Color10+type Attr uint1611+type WindowImpl int // FIXME12+13+const (14+ // TODO15+ _ = iota16+ Bold17+ Dim18+ Blink19+ Reverse20+ Underline21+)22+23+const (24+ AttrRegular Attr = 025+)26+27+var (28+ ColDefault = ColorPair{colDefault, colDefault}29+ ColNormal ColorPair30+ ColPrompt ColorPair31+ ColMatch ColorPair32+ ColCurrent ColorPair33+ ColCurrentMatch ColorPair34+ ColSpinner ColorPair35+ ColInfo ColorPair36+ ColCursor ColorPair37+ ColSelected ColorPair38+ ColHeader ColorPair39+ ColBorder ColorPair40+ ColUser ColorPair41+)42+43+func DefaultTheme() *ColorTheme {44+ if termbox.SetOutputMode(termbox.OutputCurrent) == termbox.Output256 {45+ return Dark25646+ }47+ return Default1648+}49+50+func PairFor(fg Color, bg Color) ColorPair {51+ return [2]Color{fg, bg}52+}53+54+func (a Attr) Merge(b Attr) Attr {55+ return a | b56+}57+58+func Init(theme *ColorTheme, black bool, mouse bool) {59+ ColNormal = ColorPair{theme.Fg, theme.Bg}60+ ColPrompt = ColorPair{theme.Prompt, theme.Bg}61+ ColMatch = ColorPair{theme.Match, theme.Bg}62+ ColCurrent = ColorPair{theme.Current, theme.DarkBg}63+ ColCurrentMatch = ColorPair{theme.CurrentMatch, theme.DarkBg}64+ ColSpinner = ColorPair{theme.Spinner, theme.Bg}65+ ColInfo = ColorPair{theme.Info, theme.Bg}66+ ColCursor = ColorPair{theme.Cursor, theme.DarkBg}67+ ColSelected = ColorPair{theme.Selected, theme.DarkBg}68+ ColHeader = ColorPair{theme.Header, theme.Bg}69+ ColBorder = ColorPair{theme.Border, theme.Bg}70+71+ // TODO72+}73+74+func MaxX() int {75+ // TODO76+ return 8077+}78+79+func MaxY() int {80+ // TODO81+ return 2482+}83+84+func Clear() {85+ // TODO86+}87+88+func Refresh() {89+ // TODO90+}91+92+func GetChar() Event {93+ // TODO94+ return Event{}95+}96+97+func Pause() {98+ // TODO99+}100+101+func Close() {102+ // TODO103+}104+105+func RefreshWindows(windows []*Window) {106+ // TODO107+}108+109+func NewWindow(top int, left int, width int, height int, border bool) *Window {110+ // TODO111+ return &Window{}112+}113+114+func (w *Window) Close() {115+ // TODO116+}117+118+func (w *Window) Erase() {119+ // TODO120+}121+122+func (w *Window) Enclose(y int, x int) bool {123+ // TODO124+ return false125+}126+127+func (w *Window) Move(y int, x int) {128+ // TODO129+}130+131+func (w *Window) MoveAndClear(y int, x int) {132+ // TODO133+}134+135+func (w *Window) Print(text string) {136+ // TODO137+}138+139+func (w *Window) CPrint(pair ColorPair, a Attr, text string) {140+ // TODO141+}142+143+func (w *Window) Fill(str string) bool {144+ // TODO145+ return false146+}147+148+func (w *Window) CFill(str string, fg Color, bg Color, a Attr) bool {149+ // TODO150+ return false151+}added:src/tui/tui_test.go+14 added
1+package tui2+3+import (4+ "testing"5+)6+7+func TestPairFor(t *testing.T) {8+ if PairFor(30, 50) != PairFor(30, 50) {9+ t.Fail()10+ }11+ if PairFor(-1, 10) != PairFor(-1, 10) {12+ t.Fail()13+ }14+}added:src/tui/tui.go+250 added
1+package tui2+3+import (4+ "time"5+)6+7+// Types of user action8+const (9+ Rune = iota10+11+ CtrlA12+ CtrlB13+ CtrlC14+ CtrlD15+ CtrlE16+ CtrlF17+ CtrlG18+ CtrlH19+ Tab20+ CtrlJ21+ CtrlK22+ CtrlL23+ CtrlM24+ CtrlN25+ CtrlO26+ CtrlP27+ CtrlQ28+ CtrlR29+ CtrlS30+ CtrlT31+ CtrlU32+ CtrlV33+ CtrlW34+ CtrlX35+ CtrlY36+ CtrlZ37+ ESC38+39+ Invalid40+ Mouse41+ DoubleClick42+43+ BTab44+ BSpace45+46+ Del47+ PgUp48+ PgDn49+50+ Up51+ Down52+ Left53+ Right54+ Home55+ End56+57+ SLeft58+ SRight59+60+ F161+ F262+ F363+ F464+ F565+ F666+ F767+ F868+ F969+ F1070+71+ AltEnter72+ AltSpace73+ AltSlash74+ AltBS75+ AltA76+ AltB77+ AltC78+ AltD79+ AltE80+ AltF81+82+ AltZ = AltA + 'z' - 'a'83+)84+85+const (86+ doubleClickDuration = 500 * time.Millisecond87+)88+89+type Color int1690+91+const (92+ colUndefined Color = -293+ colDefault = -194+)95+96+const (97+ colBlack Color = iota98+ colRed99+ colGreen100+ colYellow101+ colBlue102+ colMagenta103+ colCyan104+ colWhite105+)106+107+type ColorTheme struct {108+ Fg Color109+ Bg Color110+ DarkBg Color111+ Prompt Color112+ Match Color113+ Current Color114+ CurrentMatch Color115+ Spinner Color116+ Info Color117+ Cursor Color118+ Selected Color119+ Header Color120+ Border Color121+}122+123+type Event struct {124+ Type int125+ Char rune126+ MouseEvent *MouseEvent127+}128+129+type MouseEvent struct {130+ Y int131+ X int132+ S int133+ Down bool134+ Double bool135+ Mod bool136+}137+138+var (139+ _buf []byte140+ _color bool141+ _prevDownTime time.Time142+ _clickY []int143+ Default16 *ColorTheme144+ Dark256 *ColorTheme145+ Light256 *ColorTheme146+)147+148+type Window struct {149+ impl *WindowImpl150+ Top int151+ Left int152+ Width int153+ Height int154+}155+156+func EmptyTheme() *ColorTheme {157+ return &ColorTheme{158+ Fg: colUndefined,159+ Bg: colUndefined,160+ DarkBg: colUndefined,161+ Prompt: colUndefined,162+ Match: colUndefined,163+ Current: colUndefined,164+ CurrentMatch: colUndefined,165+ Spinner: colUndefined,166+ Info: colUndefined,167+ Cursor: colUndefined,168+ Selected: colUndefined,169+ Header: colUndefined,170+ Border: colUndefined}171+}172+173+func init() {174+ _prevDownTime = time.Unix(0, 0)175+ _clickY = []int{}176+ Default16 = &ColorTheme{177+ Fg: colDefault,178+ Bg: colDefault,179+ DarkBg: colBlack,180+ Prompt: colBlue,181+ Match: colGreen,182+ Current: colYellow,183+ CurrentMatch: colGreen,184+ Spinner: colGreen,185+ Info: colWhite,186+ Cursor: colRed,187+ Selected: colMagenta,188+ Header: colCyan,189+ Border: colBlack}190+ Dark256 = &ColorTheme{191+ Fg: colDefault,192+ Bg: colDefault,193+ DarkBg: 236,194+ Prompt: 110,195+ Match: 108,196+ Current: 254,197+ CurrentMatch: 151,198+ Spinner: 148,199+ Info: 144,200+ Cursor: 161,201+ Selected: 168,202+ Header: 109,203+ Border: 59}204+ Light256 = &ColorTheme{205+ Fg: colDefault,206+ Bg: colDefault,207+ DarkBg: 251,208+ Prompt: 25,209+ Match: 66,210+ Current: 237,211+ CurrentMatch: 23,212+ Spinner: 65,213+ Info: 101,214+ Cursor: 161,215+ Selected: 168,216+ Header: 31,217+ Border: 145}218+}219+220+func InitTheme(theme *ColorTheme, black bool) {221+ _color = theme != nil222+ if !_color {223+ return224+ }225+226+ baseTheme := DefaultTheme()227+ if black {228+ theme.Bg = colBlack229+ }230+231+ o := func(a Color, b Color) Color {232+ if b == colUndefined {233+ return a234+ }235+ return b236+ }237+ theme.Fg = o(baseTheme.Fg, theme.Fg)238+ theme.Bg = o(baseTheme.Bg, theme.Bg)239+ theme.DarkBg = o(baseTheme.DarkBg, theme.DarkBg)240+ theme.Prompt = o(baseTheme.Prompt, theme.Prompt)241+ theme.Match = o(baseTheme.Match, theme.Match)242+ theme.Current = o(baseTheme.Current, theme.Current)243+ theme.CurrentMatch = o(baseTheme.CurrentMatch, theme.CurrentMatch)244+ theme.Spinner = o(baseTheme.Spinner, theme.Spinner)245+ theme.Info = o(baseTheme.Info, theme.Info)246+ theme.Cursor = o(baseTheme.Cursor, theme.Cursor)247+ theme.Selected = o(baseTheme.Selected, theme.Selected)248+ theme.Header = o(baseTheme.Header, theme.Header)249+ theme.Border = o(baseTheme.Border, theme.Border)250+}added:src/util/util_unix.go+17 added
1+// +build !windows2+3+package util4+5+import (6+ "os"7+ "os/exec"8+)9+10+// ExecCommand executes the given command with $SHELL11+func ExecCommand(command string) *exec.Cmd {12+ shell := os.Getenv("SHELL")13+ if len(shell) == 0 {14+ shell = "sh"15+ }16+ return exec.Command(shell, "-c", command)17+}added:src/util/util_windows.go+17 added
1+// +build windows2+3+package util4+5+import (6+ "os"7+ "os/exec"8+)9+10+// ExecCommand executes the given command with $SHELL11+func ExecCommand(command string) *exec.Cmd {12+ shell := os.Getenv("SHELL")13+ if len(shell) == 0 {14+ shell = "cmd"15+ }16+ return exec.Command(shell, "/c", command)17+}modified:src/util/util.go+3 added-14 removed
1 package util2 3-// #include <unistd.h>4-import "C"5-3 import (4 "math"5 "os"9- "os/exec"6 "time"7+8+ "github.com/junegunn/go-isatty"9 )10 11 // Max returns the largest integer12 func Max(first int, second int) int {79 unmodified lines92 }93 94 // IsTty returns true is stdin is a terminal95 func IsTty() bool {98- return int(C.isatty(C.int(os.Stdin.Fd()))) != 096+ return isatty.IsTerminal(os.Stdin.Fd())97 }100-101-// ExecCommand executes the given command with $SHELL102-func ExecCommand(command string) *exec.Cmd {103- shell := os.Getenv("SHELL")104- if len(shell) == 0 {105- shell = "sh"106- }107- return exec.Command(shell, "-c", command)108-}modified:src/ansi_test.go+4 added-4 removed
2 unmodified lines3 import (4 "fmt"5 "testing"6 7- "github.com/junegunn/fzf/src/curses"7+ "github.com/junegunn/fzf/src/tui"8 )9 10 func TestExtractColor(t *testing.T) {11- assert := func(offset ansiOffset, b int32, e int32, fg int, bg int, bold bool) {12- var attr curses.Attr11+ assert := func(offset ansiOffset, b int32, e int32, fg tui.Color, bg tui.Color, bold bool) {12+ var attr tui.Attr13 if bold {14- attr = curses.Bold14+ attr = tui.Bold15 }16 if offset.offset[0] != b || offset.offset[1] != e ||17 offset.color.fg != fg || offset.color.bg != bg || offset.color.attr != attr {18 t.Error(offset, b, e, fg, bg, attr)140 unmodified linesmodified:src/ansi.go+14 added-14 removed
5 unmodified lines6 "strconv"7 "strings"8 "unicode/utf8"9 10- "github.com/junegunn/fzf/src/curses"10+ "github.com/junegunn/fzf/src/tui"11 )12 13 type ansiOffset struct {14 offset [2]int3215 color ansiState16 }17 18 type ansiState struct {19- fg int20- bg int21- attr curses.Attr19+ fg tui.Color20+ bg tui.Color21+ attr tui.Attr22 }23 24 func (s *ansiState) colored() bool {25 return s.fg != -1 || s.bg != -1 || s.attr > 0107 unmodified lines133 state.fg = -1134 case 49:135 state.bg = -1136 case 1:137- state.attr = curses.Bold137+ state.attr = tui.Bold138 case 2:139- state.attr = curses.Dim139+ state.attr = tui.Dim140 case 4:141- state.attr = curses.Underline141+ state.attr = tui.Underline142 case 5:143- state.attr = curses.Blink143+ state.attr = tui.Blink144 case 7:145- state.attr = curses.Reverse145+ state.attr = tui.Reverse146 case 0:147 init()148 default:149 if num >= 30 && num <= 37 {150- state.fg = num - 30150+ state.fg = tui.Color(num - 30)151 } else if num >= 40 && num <= 47 {152- state.bg = num - 40152+ state.bg = tui.Color(num - 40)153 } else if num >= 90 && num <= 97 {154- state.fg = num - 90 + 8154+ state.fg = tui.Color(num - 90 + 8)155 } else if num >= 100 && num <= 107 {156- state.bg = num - 100 + 8156+ state.bg = tui.Color(num - 100 + 8)157 }158 }159 case 1:160 switch num {2 unmodified lines163 default:164 state256 = 0165 }166 case 2:167- *ptr = num167+ *ptr = tui.Color(num)168 state256 = 0169 }170 }171 }2 unmodified linesadded:src/constants_unix.go+8 added
1+// +build !windows2+3+package fzf4+5+const (6+ // Reader7+ defaultCommand = `find . -path '*/\.*' -prune -o -type f -print -o -type l -print 2> /dev/null | sed s/^..//`8+)added:src/constants_windows.go+8 added
1+// +build windows2+3+package fzf4+5+const (6+ // Reader7+ defaultCommand = `dir /s/b`8+)modified:src/constants.go-1 removed
13 unmodified lines14 coordinatorDelayMax time.Duration = 100 * time.Millisecond15 coordinatorDelayStep time.Duration = 10 * time.Millisecond16 17 // Reader18- defaultCommand = `find . -path '*/\.*' -prune -o -type f -print -o -type l -print 2> /dev/null | sed s/^..//`18 readerBufferSize = 64 * 102419 20 // Terminal21 initialDelay = 20 * time.Millisecond46 unmodified linesmodified:src/Makefile+1 added-1 removed
41 unmodified lines42 mkdir -p $(shell dirname $(SRCDIR))43 ln -s $(ROOTDIR) $(SRCDIR)44 45 deps: $(SRCDIR) $(SOURCES)46- cd $(SRCDIR) && go get46+ cd $(SRCDIR) && go get -tags "$(TAGS)"47 48 android-build: $(SRCDIR)49 cd $(SRCDIR) && GOARCH=arm GOARM=7 CGO_ENABLED=1 go get50 cd $(SRCDIR)/fzf && GOARCH=arm GOARM=7 CGO_ENABLED=1 go build -a -ldflags="-w -extldflags=-pie" -o $(BINARYARM7)66 unmodified linesmodified:src/options_test.go+86 added-86 removed
2 unmodified lines3 import (4 "fmt"5 "testing"6 7- "github.com/junegunn/fzf/src/curses"7+ "github.com/junegunn/fzf/src/tui"8 "github.com/junegunn/fzf/src/util"9 )10 11 func TestDelimiterRegex(t *testing.T) {120 unmodified lines132 }133 if len(pairs) != 11 {134 t.Error(11)135 }136- check(curses.CtrlZ, "ctrl-z")137- check(curses.AltZ, "alt-z")138- check(curses.F2, "f2")139- check(curses.AltZ+'@', "@")140- check(curses.AltA, "Alt-a")141- check(curses.AltZ+'!', "!")142- check(curses.CtrlA+'g'-'a', "ctrl-G")143- check(curses.AltZ+'J', "J")144- check(curses.AltZ+'g', "g")145- check(curses.AltEnter, "ALT-enter")146- check(curses.AltSpace, "alt-SPACE")136+ check(tui.CtrlZ, "ctrl-z")137+ check(tui.AltZ, "alt-z")138+ check(tui.F2, "f2")139+ check(tui.AltZ+'@', "@")140+ check(tui.AltA, "Alt-a")141+ check(tui.AltZ+'!', "!")142+ check(tui.CtrlA+'g'-'a', "ctrl-G")143+ check(tui.AltZ+'J', "J")144+ check(tui.AltZ+'g', "g")145+ check(tui.AltEnter, "ALT-enter")146+ check(tui.AltSpace, "alt-SPACE")147 148 // Synonyms149 pairs = parseKeyChords("enter,Return,space,tab,btab,esc,up,down,left,right", "")150 if len(pairs) != 9 {151 t.Error(9)152 }153- check(curses.CtrlM, "Return")154- check(curses.AltZ+' ', "space")155- check(curses.Tab, "tab")156- check(curses.BTab, "btab")157- check(curses.ESC, "esc")158- check(curses.Up, "up")159- check(curses.Down, "down")160- check(curses.Left, "left")161- check(curses.Right, "right")153+ check(tui.CtrlM, "Return")154+ check(tui.AltZ+' ', "space")155+ check(tui.Tab, "tab")156+ check(tui.BTab, "btab")157+ check(tui.ESC, "esc")158+ check(tui.Up, "up")159+ check(tui.Down, "down")160+ check(tui.Left, "left")161+ check(tui.Right, "right")162 163 pairs = parseKeyChords("Tab,Ctrl-I,PgUp,page-up,pgdn,Page-Down,Home,End,Alt-BS,Alt-BSpace,shift-left,shift-right,btab,shift-tab,return,Enter,bspace", "")164 if len(pairs) != 11 {165 t.Error(11)166 }167- check(curses.Tab, "Ctrl-I")168- check(curses.PgUp, "page-up")169- check(curses.PgDn, "Page-Down")170- check(curses.Home, "Home")171- check(curses.End, "End")172- check(curses.AltBS, "Alt-BSpace")173- check(curses.SLeft, "shift-left")174- check(curses.SRight, "shift-right")175- check(curses.BTab, "shift-tab")176- check(curses.CtrlM, "Enter")177- check(curses.BSpace, "bspace")167+ check(tui.Tab, "Ctrl-I")168+ check(tui.PgUp, "page-up")169+ check(tui.PgDn, "Page-Down")170+ check(tui.Home, "Home")171+ check(tui.End, "End")172+ check(tui.AltBS, "Alt-BSpace")173+ check(tui.SLeft, "shift-left")174+ check(tui.SRight, "shift-right")175+ check(tui.BTab, "shift-tab")176+ check(tui.CtrlM, "Enter")177+ check(tui.BSpace, "bspace")178 }179 180 func TestParseKeysWithComma(t *testing.T) {181 checkN := func(a int, b int) {8 unmodified lines190 }191 192 pairs := parseKeyChords(",", "")193 checkN(len(pairs), 1)194- check(pairs, curses.AltZ+',', ",")194+ check(pairs, tui.AltZ+',', ",")195 196 pairs = parseKeyChords(",,a,b", "")197 checkN(len(pairs), 3)198- check(pairs, curses.AltZ+'a', "a")199- check(pairs, curses.AltZ+'b', "b")200- check(pairs, curses.AltZ+',', ",")198+ check(pairs, tui.AltZ+'a', "a")199+ check(pairs, tui.AltZ+'b', "b")200+ check(pairs, tui.AltZ+',', ",")201 202 pairs = parseKeyChords("a,b,,", "")203 checkN(len(pairs), 3)204- check(pairs, curses.AltZ+'a', "a")205- check(pairs, curses.AltZ+'b', "b")206- check(pairs, curses.AltZ+',', ",")204+ check(pairs, tui.AltZ+'a', "a")205+ check(pairs, tui.AltZ+'b', "b")206+ check(pairs, tui.AltZ+',', ",")207 208 pairs = parseKeyChords("a,,,b", "")209 checkN(len(pairs), 3)210- check(pairs, curses.AltZ+'a', "a")211- check(pairs, curses.AltZ+'b', "b")212- check(pairs, curses.AltZ+',', ",")210+ check(pairs, tui.AltZ+'a', "a")211+ check(pairs, tui.AltZ+'b', "b")212+ check(pairs, tui.AltZ+',', ",")213 214 pairs = parseKeyChords("a,,,b,c", "")215 checkN(len(pairs), 4)216- check(pairs, curses.AltZ+'a', "a")217- check(pairs, curses.AltZ+'b', "b")218- check(pairs, curses.AltZ+'c', "c")219- check(pairs, curses.AltZ+',', ",")216+ check(pairs, tui.AltZ+'a', "a")217+ check(pairs, tui.AltZ+'b', "b")218+ check(pairs, tui.AltZ+'c', "c")219+ check(pairs, tui.AltZ+',', ",")220 221 pairs = parseKeyChords(",,,", "")222 checkN(len(pairs), 1)223- check(pairs, curses.AltZ+',', ",")223+ check(pairs, tui.AltZ+',', ",")224 }225 226 func TestBind(t *testing.T) {227 check := func(action actionType, expected actionType) {7 unmodified lines235 }236 }237 keymap := defaultKeymap()238 execmap := make(map[int]string)239- check(actBeginningOfLine, keymap[curses.CtrlA])239+ check(actBeginningOfLine, keymap[tui.CtrlA])240 parseKeymap(keymap, execmap,241 "ctrl-a:kill-line,ctrl-b:toggle-sort,c:page-up,alt-z:page-down,"+242 "f1:execute(ls {}),f2:execute/echo {}, {}, {}/,f3:execute[echo '({})'],f4:execute;less {};,"+243 "alt-a:execute@echo (,),[,],/,:,;,%,{}@,alt-b:execute;echo (,),[,],/,:,@,%,{};"+244 ",,:abort,::accept,X:execute:\nfoobar,Y:execute(baz)")245- check(actKillLine, keymap[curses.CtrlA])246- check(actToggleSort, keymap[curses.CtrlB])247- check(actPageUp, keymap[curses.AltZ+'c'])248- check(actAbort, keymap[curses.AltZ+','])249- check(actAccept, keymap[curses.AltZ+':'])250- check(actPageDown, keymap[curses.AltZ])251- check(actExecute, keymap[curses.F1])252- check(actExecute, keymap[curses.F2])253- check(actExecute, keymap[curses.F3])254- check(actExecute, keymap[curses.F4])255- checkString("ls {}", execmap[curses.F1])256- checkString("echo {}, {}, {}", execmap[curses.F2])257- checkString("echo '({})'", execmap[curses.F3])258- checkString("less {}", execmap[curses.F4])259- checkString("echo (,),[,],/,:,;,%,{}", execmap[curses.AltA])260- checkString("echo (,),[,],/,:,@,%,{}", execmap[curses.AltB])261- checkString("\nfoobar,Y:execute(baz)", execmap[curses.AltZ+'X'])245+ check(actKillLine, keymap[tui.CtrlA])246+ check(actToggleSort, keymap[tui.CtrlB])247+ check(actPageUp, keymap[tui.AltZ+'c'])248+ check(actAbort, keymap[tui.AltZ+','])249+ check(actAccept, keymap[tui.AltZ+':'])250+ check(actPageDown, keymap[tui.AltZ])251+ check(actExecute, keymap[tui.F1])252+ check(actExecute, keymap[tui.F2])253+ check(actExecute, keymap[tui.F3])254+ check(actExecute, keymap[tui.F4])255+ checkString("ls {}", execmap[tui.F1])256+ checkString("echo {}, {}, {}", execmap[tui.F2])257+ checkString("echo '({})'", execmap[tui.F3])258+ checkString("less {}", execmap[tui.F4])259+ checkString("echo (,),[,],/,:,;,%,{}", execmap[tui.AltA])260+ checkString("echo (,),[,],/,:,@,%,{}", execmap[tui.AltB])261+ checkString("\nfoobar,Y:execute(baz)", execmap[tui.AltZ+'X'])262 263 for idx, char := range []rune{'~', '!', '@', '#', '$', '%', '^', '&', '*', '|', ';', '/'} {264 parseKeymap(keymap, execmap, fmt.Sprintf("%d:execute%cfoobar%c", idx%10, char, char))265- checkString("foobar", execmap[curses.AltZ+int([]rune(fmt.Sprintf("%d", idx%10))[0])])265+ checkString("foobar", execmap[tui.AltZ+int([]rune(fmt.Sprintf("%d", idx%10))[0])])266 }267 268 parseKeymap(keymap, execmap, "f1:abort")269- check(actAbort, keymap[curses.F1])269+ check(actAbort, keymap[tui.F1])270 }271 272 func TestColorSpec(t *testing.T) {273- theme := curses.Dark256273+ theme := tui.Dark256274 dark := parseTheme(theme, "dark")275 if *dark != *theme {276 t.Errorf("colors should be equivalent")277 }4 unmodified lines282 light := parseTheme(theme, "dark,light")283 if *light == *theme {284 t.Errorf("should not be equivalent")285 }286- if *light != *curses.Light256 {286+ if *light != *tui.Light256 {287 t.Errorf("colors should be equivalent")288 }289 if light == theme {290 t.Errorf("point should not be equivalent")2 unmodified lines293 customized := parseTheme(theme, "fg:231,bg:232")294 if customized.Fg != 231 || customized.Bg != 232 {295 t.Errorf("color not customized")296 }297- if *curses.Dark256 == *customized {297+ if *tui.Dark256 == *customized {298 t.Errorf("colors should not be equivalent")299 }300- customized.Fg = curses.Dark256.Fg301- customized.Bg = curses.Dark256.Bg302- if *curses.Dark256 != *customized {303- t.Errorf("colors should now be equivalent: %v, %v", curses.Dark256, customized)300+ customized.Fg = tui.Dark256.Fg301+ customized.Bg = tui.Dark256.Bg302+ if *tui.Dark256 != *customized {303+ t.Errorf("colors should now be equivalent: %v, %v", tui.Dark256, customized)304 }305 306 customized = parseTheme(theme, "fg:231,dark,bg:232")307- if customized.Fg != curses.Dark256.Fg || customized.Bg == curses.Dark256.Bg {307+ if customized.Fg != tui.Dark256.Fg || customized.Bg == tui.Dark256.Bg {308 t.Errorf("color not customized")309 }310 }311 312 func TestParseNilTheme(t *testing.T) {313- var theme *curses.ColorTheme313+ var theme *tui.ColorTheme314 newTheme := parseTheme(theme, "prompt:12")315 if newTheme != nil {316 t.Errorf("color is disabled. keep it that way.")317 }11 unmodified lines329 if opts.Keymap[key] != expected {330 t.Error()331 }332 }333- check([]string{}, curses.CtrlN, actDown)334- check([]string{}, curses.CtrlP, actUp)333+ check([]string{}, tui.CtrlN, actDown)334+ check([]string{}, tui.CtrlP, actUp)335 336- check([]string{"--bind=ctrl-n:accept"}, curses.CtrlN, actAccept)337- check([]string{"--bind=ctrl-p:accept"}, curses.CtrlP, actAccept)336+ check([]string{"--bind=ctrl-n:accept"}, tui.CtrlN, actAccept)337+ check([]string{"--bind=ctrl-p:accept"}, tui.CtrlP, actAccept)338 339 hist := "--history=/tmp/fzf-history"340- check([]string{hist}, curses.CtrlN, actNextHistory)341- check([]string{hist}, curses.CtrlP, actPreviousHistory)340+ check([]string{hist}, tui.CtrlN, actNextHistory)341+ check([]string{hist}, tui.CtrlP, actPreviousHistory)342 343- check([]string{hist, "--bind=ctrl-n:accept"}, curses.CtrlN, actAccept)344- check([]string{hist, "--bind=ctrl-n:accept"}, curses.CtrlP, actPreviousHistory)343+ check([]string{hist, "--bind=ctrl-n:accept"}, tui.CtrlN, actAccept)344+ check([]string{hist, "--bind=ctrl-n:accept"}, tui.CtrlP, actPreviousHistory)345 346- check([]string{hist, "--bind=ctrl-p:accept"}, curses.CtrlN, actNextHistory)347- check([]string{hist, "--bind=ctrl-p:accept"}, curses.CtrlP, actAccept)346+ check([]string{hist, "--bind=ctrl-p:accept"}, tui.CtrlN, actNextHistory)347+ check([]string{hist, "--bind=ctrl-p:accept"}, tui.CtrlP, actAccept)348 }349 350 func optsFor(words ...string) *Options {351 opts := defaultOptions()49 unmodified linesmodified:src/options.go+44 added-44 removed
7 unmodified lines8 "strings"9 "unicode/utf8"10 11 "github.com/junegunn/fzf/src/algo"12- "github.com/junegunn/fzf/src/curses"12+ "github.com/junegunn/fzf/src/tui"13 14 "github.com/junegunn/go-shellwords"15 )16 124 unmodified lines141 Criteria []criterion142 Multi bool143 Ansi bool144 Mouse bool145- Theme *curses.ColorTheme145+ Theme *tui.ColorTheme146 Black bool147 Reverse bool148 Cycle bool149 Hscroll bool36 unmodified lines186 Criteria: []criterion{byScore, byLength},187 Multi: false,188 Ansi: false,189 Mouse: true,190- Theme: curses.EmptyTheme(),190+ Theme: tui.EmptyTheme(),191 Black: false,192 Reverse: false,193 Cycle: false,194 Hscroll: true,162 unmodified lines357 lkey := strings.ToLower(key)358 chord := 0359 switch lkey {360 case "up":361- chord = curses.Up361+ chord = tui.Up362 case "down":363- chord = curses.Down363+ chord = tui.Down364 case "left":365- chord = curses.Left365+ chord = tui.Left366 case "right":367- chord = curses.Right367+ chord = tui.Right368 case "enter", "return":369- chord = curses.CtrlM369+ chord = tui.CtrlM370 case "space":371- chord = curses.AltZ + int(' ')371+ chord = tui.AltZ + int(' ')372 case "bspace", "bs":373- chord = curses.BSpace373+ chord = tui.BSpace374 case "alt-enter", "alt-return":375- chord = curses.AltEnter375+ chord = tui.AltEnter376 case "alt-space":377- chord = curses.AltSpace377+ chord = tui.AltSpace378 case "alt-/":379- chord = curses.AltSlash379+ chord = tui.AltSlash380 case "alt-bs", "alt-bspace":381- chord = curses.AltBS381+ chord = tui.AltBS382 case "tab":383- chord = curses.Tab383+ chord = tui.Tab384 case "btab", "shift-tab":385- chord = curses.BTab385+ chord = tui.BTab386 case "esc":387- chord = curses.ESC387+ chord = tui.ESC388 case "del":389- chord = curses.Del389+ chord = tui.Del390 case "home":391- chord = curses.Home391+ chord = tui.Home392 case "end":393- chord = curses.End393+ chord = tui.End394 case "pgup", "page-up":395- chord = curses.PgUp395+ chord = tui.PgUp396 case "pgdn", "page-down":397- chord = curses.PgDn397+ chord = tui.PgDn398 case "shift-left":399- chord = curses.SLeft399+ chord = tui.SLeft400 case "shift-right":401- chord = curses.SRight401+ chord = tui.SRight402 case "double-click":403- chord = curses.DoubleClick403+ chord = tui.DoubleClick404 case "f10":405- chord = curses.F10405+ chord = tui.F10406 default:407 if len(key) == 6 && strings.HasPrefix(lkey, "ctrl-") && isAlphabet(lkey[5]) {408- chord = curses.CtrlA + int(lkey[5]) - 'a'408+ chord = tui.CtrlA + int(lkey[5]) - 'a'409 } else if len(key) == 5 && strings.HasPrefix(lkey, "alt-") && isAlphabet(lkey[4]) {410- chord = curses.AltA + int(lkey[4]) - 'a'410+ chord = tui.AltA + int(lkey[4]) - 'a'411 } else if len(key) == 2 && strings.HasPrefix(lkey, "f") && key[1] >= '1' && key[1] <= '9' {412- chord = curses.F1 + int(key[1]) - '1'412+ chord = tui.F1 + int(key[1]) - '1'413 } else if utf8.RuneCountInString(key) == 1 {414- chord = curses.AltZ + int([]rune(key)[0])414+ chord = tui.AltZ + int([]rune(key)[0])415 } else {416 errorExit("unsupported key: " + key)417 }418 }38 unmodified lines457 }458 return criteria459 }460 461-func dupeTheme(theme *curses.ColorTheme) *curses.ColorTheme {461+func dupeTheme(theme *tui.ColorTheme) *tui.ColorTheme {462 if theme != nil {463 dupe := *theme464 return &dupe465 }466 return nil467 }468 469-func parseTheme(defaultTheme *curses.ColorTheme, str string) *curses.ColorTheme {469+func parseTheme(defaultTheme *tui.ColorTheme, str string) *tui.ColorTheme {470 theme := dupeTheme(defaultTheme)471 for _, str := range strings.Split(strings.ToLower(str), ",") {472 switch str {473 case "dark":474- theme = dupeTheme(curses.Dark256)474+ theme = dupeTheme(tui.Dark256)475 case "light":476- theme = dupeTheme(curses.Light256)476+ theme = dupeTheme(tui.Light256)477 case "16":478- theme = dupeTheme(curses.Default16)478+ theme = dupeTheme(tui.Default16)479 case "bw", "no":480 theme = nil481 default:482 fail := func() {11 unmodified lines494 ansi32, err := strconv.Atoi(pair[1])495 if err != nil || ansi32 < -1 || ansi32 > 255 {496 fail()497 }498- ansi := int16(ansi32)498+ ansi := tui.Color(ansi32)499 switch pair[0] {500 case "fg":501 theme.Fg = ansi502 case "bg":68 unmodified lines571 errorExit("bind action not specified: " + origPairStr)572 }573 var key int574 if len(pair[0]) == 1 && pair[0][0] == escapedColon {575- key = ':' + curses.AltZ575+ key = ':' + tui.AltZ576 } else if len(pair[0]) == 1 && pair[0][0] == escapedComma {577- key = ',' + curses.AltZ577+ key = ',' + tui.AltZ578 } else {579 keys := parseKeyChords(pair[0], "key name required")580 key = firstKey(keys)581 }285 unmodified lines867 parseKeymap(opts.Keymap, opts.Execmap, nextString(allArgs, &i, "bind expression required"))868 case "--color":869 spec := optionalNextString(allArgs, &i)870 if len(spec) == 0 {871- opts.Theme = curses.EmptyTheme()871+ opts.Theme = tui.EmptyTheme()872 } else {873 opts.Theme = parseTheme(opts.Theme, spec)874 }875 case "--toggle-sort":28 unmodified lines904 opts.Mouse = false905 case "+c", "--no-color":906 opts.Theme = nil907 case "+2", "--no-256":908- opts.Theme = curses.Default16908+ opts.Theme = tui.Default16909 case "--black":910 opts.Black = true911 case "--no-black":912 opts.Black = false157 unmodified lines1070 1071 func postProcessOptions(opts *Options) {1072 // Default actions for CTRL-N / CTRL-P when --history is set1073 if opts.History != nil {1074- if _, prs := opts.Keymap[curses.CtrlP]; !prs {1075- opts.Keymap[curses.CtrlP] = actPreviousHistory1074+ if _, prs := opts.Keymap[tui.CtrlP]; !prs {1075+ opts.Keymap[tui.CtrlP] = actPreviousHistory1076 }1077- if _, prs := opts.Keymap[curses.CtrlN]; !prs {1078- opts.Keymap[curses.CtrlN] = actNextHistory1077+ if _, prs := opts.Keymap[tui.CtrlN]; !prs {1078+ opts.Keymap[tui.CtrlN] = actNextHistory1079 }1080 }1081 1082 // Extend the default key map37 unmodified linesmodified:src/README.md+4 added-2 removed
81 unmodified lines82 --------------------------83 84 - [ncurses][ncurses]85 - [mattn/go-runewidth](https://github.com/mattn/go-runewidth)86- - Licensed under [MIT](http://mattn.mit-license.org/2013)86+ - Licensed under [MIT](http://mattn.mit-license.org)87 - [mattn/go-shellwords](https://github.com/mattn/go-shellwords)88- - Licensed under [MIT](http://mattn.mit-license.org/2014)88+ - Licensed under [MIT](http://mattn.mit-license.org)89+- [mattn/go-isatty](https://github.com/mattn/go-isatty)90+ - Licensed under [MIT](http://mattn.mit-license.org)91 92 License93 -------94 8 unmodified linesmodified:src/result_test.go+13 added-11 removed
1+// +build !termbox2+3 package fzf4 5 import (6 "math"7 "sort"8 "testing"9 8- "github.com/junegunn/fzf/src/curses"10+ "github.com/junegunn/fzf/src/tui"11 "github.com/junegunn/fzf/src/util"12 )13 14 func TestOffsetSort(t *testing.T) {84 unmodified lines99 item := Result{100 item: &Item{101 colors: &[]ansiOffset{102 ansiOffset{[2]int32{0, 20}, ansiState{1, 5, 0}},101- ansiOffset{[2]int32{22, 27}, ansiState{2, 6, curses.Bold}},103+ ansiOffset{[2]int32{22, 27}, ansiState{2, 6, tui.Bold}},104 ansiOffset{[2]int32{30, 32}, ansiState{3, 7, 0}},103- ansiOffset{[2]int32{33, 40}, ansiState{4, 8, curses.Bold}}}}}105+ ansiOffset{[2]int32{33, 40}, ansiState{4, 8, tui.Bold}}}}}106 // [{[0 5] 9 false} {[5 15] 99 false} {[15 20] 9 false} {[22 25] 10 true} {[25 35] 99 false} {[35 40] 11 true}]107 106- colors := item.colorOffsets(offsets, curses.Dark256, 99, 0, true)107- assert := func(idx int, b int32, e int32, c int, bold bool) {108- var attr curses.Attr108+ colors := item.colorOffsets(offsets, tui.Dark256, 99, 0, true)109+ assert := func(idx int, b int32, e int32, c tui.ColorPair, bold bool) {110+ var attr tui.Attr111 if bold {110- attr = curses.Bold112+ attr = tui.Bold113 }114 o := colors[idx]115 if o.offset[0] != b || o.offset[1] != e || o.color != c || o.attr != attr {116 t.Error(o)117 }118 }117- assert(0, 0, 5, curses.ColUser, false)119+ assert(0, 0, 5, tui.ColUser, false)120 assert(1, 5, 15, 99, false)119- assert(2, 15, 20, curses.ColUser, false)120- assert(3, 22, 25, curses.ColUser+1, true)121+ assert(2, 15, 20, tui.ColUser, false)122+ assert(3, 22, 25, tui.ColUser+1, true)123 assert(4, 25, 35, 99, false)122- assert(5, 35, 40, curses.ColUser+2, true)124+ assert(5, 35, 40, tui.ColUser+2, true)125 }modified:src/result.go+10 added-10 removed
3 unmodified lines4 "math"5 "sort"6 "unicode"7 8- "github.com/junegunn/fzf/src/curses"8+ "github.com/junegunn/fzf/src/tui"9 "github.com/junegunn/fzf/src/util"10 )11 12 // Offset holds two 32-bit integers denoting the offsets of a matched substring13 type Offset [2]int3214 15 type colorOffset struct {16 offset [2]int3217- color int18- attr curses.Attr17+ color tui.ColorPair18+ attr tui.Attr19 index int3220 }21 22 type rank struct {68 unmodified lines91 func minRank() rank {92 return rank{index: 0, points: [4]uint16{math.MaxUint16, 0, 0, 0}}93 }94 95-func (result *Result) colorOffsets(matchOffsets []Offset, theme *curses.ColorTheme, color int, attr curses.Attr, current bool) []colorOffset {95+func (result *Result) colorOffsets(matchOffsets []Offset, theme *tui.ColorTheme, color tui.ColorPair, attr tui.Attr, current bool) []colorOffset {96 itemColors := result.item.Colors()97 98 // No ANSI code, or --color=no99 if len(itemColors) == 0 {48 unmodified lines148 ansi := itemColors[curr-1]149 fg := ansi.color.fg150 if fg == -1 {151 if current {152- fg = int(theme.Current)152+ fg = theme.Current153 } else {154- fg = int(theme.Fg)154+ fg = theme.Fg155 }156 }157 bg := ansi.color.bg158 if bg == -1 {159 if current {160- bg = int(theme.DarkBg)160+ bg = theme.DarkBg161 } else {162- bg = int(theme.Bg)162+ bg = theme.Bg163 }164 }165 colors = append(colors, colorOffset{166 offset: [2]int32{int32(start), int32(idx)},167- color: curses.PairFor(fg, bg),168- attr: ansi.color.attr | attr})167+ color: tui.PairFor(fg, bg),168+ attr: ansi.color.attr.Merge(attr)})169 }170 }171 }172 for idx, col := range cols {69 unmodified linesadded:src/terminal_unix.go+13 added
1+// +build !windows2+3+package fzf4+5+import (6+ "os"7+ "os/signal"8+ "syscall"9+)10+11+func notifyOnResize(resizeChan chan<- os.Signal) {12+ signal.Notify(resizeChan, syscall.SIGWINCH)13+}added:src/terminal_windows.go+11 added
1+// +build windows2+3+package fzf4+5+import (6+ "os"7+)8+9+func notifyOnResize(resizeChan chan<- os.Signal) {10+ // TODO11+}modified:src/terminal.go+90 added-91 removed
10 unmodified lines11 "sync"12 "syscall"13 "time"14 15- C "github.com/junegunn/fzf/src/curses"15+ "github.com/junegunn/fzf/src/tui"16 "github.com/junegunn/fzf/src/util"17 18 "github.com/junegunn/go-runewidth"19 )48 unmodified lines68 header []string69 header0 []string70 ansi bool71 margin [4]sizeSpec72- window *C.Window73- bwindow *C.Window74- pwindow *C.Window72+ window *tui.Window73+ bwindow *tui.Window74+ pwindow *tui.Window75 count int76 progress int77 reading bool78 jumping jumpMode10 unmodified lines89 initFunc func()90 suppress bool91 startChan chan bool92 slab *util.Slab93- theme *C.ColorTheme93+ theme *tui.ColorTheme94 }95 96 type selectedItem struct {97 at time.Time88 unmodified lines186 )187 188 func defaultKeymap() map[int]actionType {189 keymap := make(map[int]actionType)190- keymap[C.Invalid] = actInvalid191- keymap[C.CtrlA] = actBeginningOfLine192- keymap[C.CtrlB] = actBackwardChar193- keymap[C.CtrlC] = actAbort194- keymap[C.CtrlG] = actAbort195- keymap[C.CtrlQ] = actAbort196- keymap[C.ESC] = actAbort197- keymap[C.CtrlD] = actDeleteCharEOF198- keymap[C.CtrlE] = actEndOfLine199- keymap[C.CtrlF] = actForwardChar200- keymap[C.CtrlH] = actBackwardDeleteChar201- keymap[C.BSpace] = actBackwardDeleteChar202- keymap[C.Tab] = actToggleDown203- keymap[C.BTab] = actToggleUp204- keymap[C.CtrlJ] = actDown205- keymap[C.CtrlK] = actUp206- keymap[C.CtrlL] = actClearScreen207- keymap[C.CtrlM] = actAccept208- keymap[C.CtrlN] = actDown209- keymap[C.CtrlP] = actUp210- keymap[C.CtrlU] = actUnixLineDiscard211- keymap[C.CtrlW] = actUnixWordRubout212- keymap[C.CtrlY] = actYank190+ keymap[tui.Invalid] = actInvalid191+ keymap[tui.CtrlA] = actBeginningOfLine192+ keymap[tui.CtrlB] = actBackwardChar193+ keymap[tui.CtrlC] = actAbort194+ keymap[tui.CtrlG] = actAbort195+ keymap[tui.CtrlQ] = actAbort196+ keymap[tui.ESC] = actAbort197+ keymap[tui.CtrlD] = actDeleteCharEOF198+ keymap[tui.CtrlE] = actEndOfLine199+ keymap[tui.CtrlF] = actForwardChar200+ keymap[tui.CtrlH] = actBackwardDeleteChar201+ keymap[tui.BSpace] = actBackwardDeleteChar202+ keymap[tui.Tab] = actToggleDown203+ keymap[tui.BTab] = actToggleUp204+ keymap[tui.CtrlJ] = actDown205+ keymap[tui.CtrlK] = actUp206+ keymap[tui.CtrlL] = actClearScreen207+ keymap[tui.CtrlM] = actAccept208+ keymap[tui.CtrlN] = actDown209+ keymap[tui.CtrlP] = actUp210+ keymap[tui.CtrlU] = actUnixLineDiscard211+ keymap[tui.CtrlW] = actUnixWordRubout212+ keymap[tui.CtrlY] = actYank213 214- keymap[C.AltB] = actBackwardWord215- keymap[C.SLeft] = actBackwardWord216- keymap[C.AltF] = actForwardWord217- keymap[C.SRight] = actForwardWord218- keymap[C.AltD] = actKillWord219- keymap[C.AltBS] = actBackwardKillWord214+ keymap[tui.AltB] = actBackwardWord215+ keymap[tui.SLeft] = actBackwardWord216+ keymap[tui.AltF] = actForwardWord217+ keymap[tui.SRight] = actForwardWord218+ keymap[tui.AltD] = actKillWord219+ keymap[tui.AltBS] = actBackwardKillWord220 221- keymap[C.Up] = actUp222- keymap[C.Down] = actDown223- keymap[C.Left] = actBackwardChar224- keymap[C.Right] = actForwardChar221+ keymap[tui.Up] = actUp222+ keymap[tui.Down] = actDown223+ keymap[tui.Left] = actBackwardChar224+ keymap[tui.Right] = actForwardChar225 226- keymap[C.Home] = actBeginningOfLine227- keymap[C.End] = actEndOfLine228- keymap[C.Del] = actDeleteChar229- keymap[C.PgUp] = actPageUp230- keymap[C.PgDn] = actPageDown226+ keymap[tui.Home] = actBeginningOfLine227+ keymap[tui.End] = actEndOfLine228+ keymap[tui.Del] = actDeleteChar229+ keymap[tui.PgUp] = actPageUp230+ keymap[tui.PgDn] = actPageDown231 232- keymap[C.Rune] = actRune233- keymap[C.Mouse] = actMouse234- keymap[C.DoubleClick] = actAccept232+ keymap[tui.Rune] = actRune233+ keymap[tui.Mouse] = actMouse234+ keymap[tui.DoubleClick] = actAccept235 return keymap236 }237 238 // NewTerminal returns new Terminal object59 unmodified lines298 slab: util.MakeSlab(slab16Size, slab32Size),299 theme: opts.Theme,300 startChan: make(chan bool, 1),301 initFunc: func() {302- C.Init(opts.Theme, opts.Black, opts.Mouse)302+ tui.Init(opts.Theme, opts.Black, opts.Mouse)303 }}304 }305 306 // Input returns current query string121 unmodified lines428 return util.Constrain(int(size.size), minSize, max)429 }430 431 func (t *Terminal) resizeWindows() {432- screenWidth := C.MaxX()433- screenHeight := C.MaxY()432+ screenWidth := tui.MaxX()433+ screenHeight := tui.MaxY()434 marginInt := [4]int{}435 for idx, sizeSpec := range t.margin {436 if sizeSpec.percent {437 var max float6440 unmodified lines478 width := screenWidth - marginInt[1] - marginInt[3]479 height := screenHeight - marginInt[0] - marginInt[2]480 if t.isPreviewEnabled() {481 createPreviewWindow := func(y int, x int, w int, h int) {482- t.bwindow = C.NewWindow(y, x, w, h, true)483- t.pwindow = C.NewWindow(y+1, x+2, w-4, h-2, false)482+ t.bwindow = tui.NewWindow(y, x, w, h, true)483+ t.pwindow = tui.NewWindow(y+1, x+2, w-4, h-2, false)484 }485 switch t.preview.position {486 case posUp:487 pheight := calculateSize(height, t.preview.size, minHeight, 3)488- t.window = C.NewWindow(488+ t.window = tui.NewWindow(489 marginInt[0]+pheight, marginInt[3], width, height-pheight, false)490 createPreviewWindow(marginInt[0], marginInt[3], width, pheight)491 case posDown:492 pheight := calculateSize(height, t.preview.size, minHeight, 3)493- t.window = C.NewWindow(493+ t.window = tui.NewWindow(494 marginInt[0], marginInt[3], width, height-pheight, false)495 createPreviewWindow(marginInt[0]+height-pheight, marginInt[3], width, pheight)496 case posLeft:497 pwidth := calculateSize(width, t.preview.size, minWidth, 5)498- t.window = C.NewWindow(498+ t.window = tui.NewWindow(499 marginInt[0], marginInt[3]+pwidth, width-pwidth, height, false)500 createPreviewWindow(marginInt[0], marginInt[3], pwidth, height)501 case posRight:502 pwidth := calculateSize(width, t.preview.size, minWidth, 5)503- t.window = C.NewWindow(503+ t.window = tui.NewWindow(504 marginInt[0], marginInt[3], width-pwidth, height, false)505 createPreviewWindow(marginInt[0], marginInt[3]+width-pwidth, pwidth, height)506 }507 } else {508- t.window = C.NewWindow(508+ t.window = tui.NewWindow(509 marginInt[0],510 marginInt[3],511 width,512 height, false)17 unmodified lines530 }531 532 func (t *Terminal) printPrompt() {533 t.move(0, 0, true)534- t.window.CPrint(C.ColPrompt, C.Bold, t.prompt)535- t.window.CPrint(C.ColNormal, C.Bold, string(t.input))534+ t.window.CPrint(tui.ColPrompt, tui.Bold, t.prompt)535+ t.window.CPrint(tui.ColNormal, tui.Bold, string(t.input))536 }537 538 func (t *Terminal) printInfo() {539 if t.inlineInfo {540 t.move(0, displayWidth([]rune(t.prompt))+displayWidth(t.input)+1, true)541 if t.reading {542- t.window.CPrint(C.ColSpinner, C.Bold, " < ")542+ t.window.CPrint(tui.ColSpinner, tui.Bold, " < ")543 } else {544- t.window.CPrint(C.ColPrompt, C.Bold, " < ")544+ t.window.CPrint(tui.ColPrompt, tui.Bold, " < ")545 }546 } else {547 t.move(1, 0, true)548 if t.reading {549 duration := int64(spinnerDuration)550 idx := (time.Now().UnixNano() % (duration * int64(len(_spinner)))) / duration551- t.window.CPrint(C.ColSpinner, C.Bold, _spinner[idx])551+ t.window.CPrint(tui.ColSpinner, tui.Bold, _spinner[idx])552 }553 t.move(1, 2, false)554 }555 10 unmodified lines566 }567 if t.progress > 0 && t.progress < 100 {568 output += fmt.Sprintf(" (%d%%)", t.progress)569 }570- t.window.CPrint(C.ColInfo, 0, output)570+ t.window.CPrint(tui.ColInfo, 0, output)571 }572 573 func (t *Terminal) printHeader() {574 if len(t.header) == 0 {15 unmodified lines590 text: util.RunesToChars([]rune(trimmed)),591 colors: colors}592 593 t.move(line, 2, true)594- t.printHighlighted(&Result{item: item}, 0, C.ColHeader, 0, false)594+ t.printHighlighted(&Result{item: item},595+ tui.AttrRegular, tui.ColHeader, tui.ColDefault, false)596 }597 }598 599 func (t *Terminal) printList() {25 unmodified lines625 }626 } else if current {627 label = ">"628 }628- t.window.CPrint(C.ColCursor, C.Bold, label)629+ t.window.CPrint(tui.ColCursor, tui.Bold, label)630 if current {631 if selected {631- t.window.CPrint(C.ColSelected, C.Bold, ">")632+ t.window.CPrint(tui.ColSelected, tui.Bold, ">")633 } else {633- t.window.CPrint(C.ColCurrent, C.Bold, " ")634+ t.window.CPrint(tui.ColCurrent, tui.Bold, " ")635 }635- t.printHighlighted(result, C.Bold, C.ColCurrent, C.ColCurrentMatch, true)636+ t.printHighlighted(result, tui.Bold, tui.ColCurrent, tui.ColCurrentMatch, true)637 } else {638 if selected {638- t.window.CPrint(C.ColSelected, C.Bold, ">")639+ t.window.CPrint(tui.ColSelected, tui.Bold, ">")640 } else {641 t.window.Print(" ")642 }642- t.printHighlighted(result, 0, C.ColNormal, C.ColMatch, false)643+ t.printHighlighted(result, 0, tui.ColNormal, tui.ColMatch, false)644 }645 }646 647 func trimRight(runes []rune, width int) ([]rune, int) {47 unmodified lines695 }696 return false697 }698 698-func (t *Terminal) printHighlighted(result *Result, attr C.Attr, col1 int, col2 int, current bool) {699+func (t *Terminal) printHighlighted(result *Result, attr tui.Attr, col1 tui.ColorPair, col2 tui.ColorPair, current bool) {700 item := result.item701 702 // Overflow703 text := make([]rune, item.text.Length())123 unmodified lines827 })828 if t.previewer.offset > 0 {829 offset := fmt.Sprintf("%d/%d", t.previewer.offset+1, t.previewer.lines)830 t.pwindow.Move(0, t.pwindow.Width-len(offset))830- t.pwindow.CPrint(C.ColInfo, C.Reverse, offset)831+ t.pwindow.CPrint(tui.ColInfo, tui.Reverse, offset)832 }833 }834 835 func processTabs(runes []rune, prefixWidth int) (string, int) {22 unmodified lines858 859 func (t *Terminal) refresh() {860 if !t.suppress {861 if t.isPreviewEnabled() {861- t.bwindow.Refresh()862- t.pwindow.Refresh()862+ tui.RefreshWindows([]*tui.Window{t.bwindow, t.pwindow, t.window})863+ } else {864+ tui.RefreshWindows([]*tui.Window{t.window})865 }864- t.window.Refresh()865- C.DoUpdate()866 }867 }868 869 func (t *Terminal) delChar() bool {41 unmodified lines911 t.yanked = copySlice(t.input[t.cx:pcx])912 t.input = append(t.input[:t.cx], after...)913 }914 915-func keyMatch(key int, event C.Event) bool {915+func keyMatch(key int, event tui.Event) bool {916 return event.Type == key ||917- event.Type == C.Rune && int(event.Char) == key-C.AltZ ||918- event.Type == C.Mouse && key == C.DoubleClick && event.MouseEvent.Double917+ event.Type == tui.Rune && int(event.Char) == key-tui.AltZ ||918+ event.Type == tui.Mouse && key == tui.DoubleClick && event.MouseEvent.Double919 }920 921 func quoteEntry(entry string) string {922 return "'" + strings.Replace(entry, "'", "'\\''", -1) + "'"56 unmodified lines979 cmd := util.ExecCommand(command)980 cmd.Stdin = os.Stdin981 cmd.Stdout = os.Stdout982 cmd.Stderr = os.Stderr983- C.Endwin()983+ tui.Pause()984 cmd.Run()985 t.refresh()986 }987 25 unmodified lines1013 t.reqBox.Set(reqQuit, nil)1014 }()1015 1016 resizeChan := make(chan os.Signal, 1)1017- signal.Notify(resizeChan, syscall.SIGWINCH)1017+ notifyOnResize(resizeChan) // Non-portable1018 go func() {1019 for {1020 <-resizeChan1021 t.reqBox.Set(reqRedraw, nil)103 unmodified lines1125 t.printHeader()1126 case reqRefresh:1127 t.suppress = false1128 case reqRedraw:1129- C.Clear()1130- C.Endwin()1131- C.Refresh()1129+ tui.Clear()1130+ tui.Refresh()1131 t.printAll()1132 case reqClose:1134- C.Close()1133+ tui.Close()1134 if t.output() {1135 exit(exitOk)1136 }1137 exit(exitNoMatch)4 unmodified lines1142 t.printPreview()1143 case reqPreviewRefresh:1144 t.printPreview()1145 case reqPrintQuery:1147- C.Close()1146+ tui.Close()1147 t.printer(string(t.input))1148 exit(exitOk)1149 case reqQuit:1151- C.Close()1150+ tui.Close()1151 exit(exitInterrupt)1152 }1153 }1154 t.placeCursor()4 unmodified lines1159 }()1160 1161 looping := true1162 for looping {1164- event := C.GetChar()1163+ event := tui.GetChar()1164 1165 t.mutex.Lock()1166 previousInput := t.input1167 events := []util.EventType{reqPrompt}275 unmodified lines1443 if me.Double {1444 // Double-click1445 if my >= min {1446 if t.vset(t.offset+my-min) && t.cy < t.merger.Length() {1448- return doAction(t.keymap[C.DoubleClick], C.DoubleClick)1447+ return doAction(t.keymap[tui.DoubleClick], tui.DoubleClick)1448 }1449 }1450 } else if me.Down {1451 if my == 0 && mx >= 0 {14 unmodified lines1466 changed := false1467 mapkey := event.Type1468 if t.jumping == jumpDisabled {1469 action := t.keymap[mapkey]1471- if mapkey == C.Rune {1472- mapkey = int(event.Char) + int(C.AltZ)1470+ if mapkey == tui.Rune {1471+ mapkey = int(event.Char) + int(tui.AltZ)1472 if act, prs := t.keymap[mapkey]; prs {1473 action = act1474 }1475 }6 unmodified lines1482 t.cx = util.Constrain(t.cx, 0, maxPatternLength)1483 }1484 changed = string(previousInput) != string(t.input)1485 } else {1487- if mapkey == C.Rune {1486+ if mapkey == tui.Rune {1487 if idx := strings.IndexRune(t.jumpLabels, event.Char); idx >= 0 && idx < t.maxItems() && idx < t.merger.Length() {1488 t.cy = idx + t.offset1489 if t.jumping == jumpAcceptEnabled {1490 req(reqClose)64 unmodified lines