diff --git a/clite/commands.js b/clite/commands.js index b335f5d..db3c5c4 100644 --- a/clite/commands.js +++ b/clite/commands.js @@ -462,25 +462,25 @@ Options: function input(key) { switch (key) { - case 27: + case curses.key.KEY_ESCAPE: case 'q': curses.endwin(); io.exit(0); return; break; - case -4: + case curses.key.KEY_UP: if (topLine > 0) { topLine--; showAt(topLine); } break; - case -3: + case curses.key.KEY_DOWN: if (topLine < bottomLine) { topLine++; showAt(topLine); } break; - case -7: + case curses.key.KEY_PPAGE: if (topLine > 0) { topLine -= rows-1; if (topLine < 0) @@ -488,7 +488,7 @@ Options: showAt(topLine); } break; - case -8: + case curses.key.KEY_NPAGE: if (topLine < bottomLine) { topLine += rows-1; if (topLine >= bottomLine) @@ -496,11 +496,11 @@ Options: showAt(topLine); } break; - case -9: + case curses.key.KEY_HOME: topLine = 0; showAt(topLine); break; - case -10: + case curses.key.KEY_END: topLine = bottomLine; showAt(topLine); break; diff --git a/clite/core.js b/clite/core.js index bebeadd..cde59ae 100644 --- a/clite/core.js +++ b/clite/core.js @@ -2483,6 +2483,21 @@ clite.tty = { default: return false; } + }, + getCursor:function(id) { + if (id<0 || id>=clite.tty.data.ttys.length) + return; + var t = clite.tty.data.ttys[id]; + return [t.x,t.y]; + }, + setCursor:function(id,x,y) { + if (id<0 || id>=clite.tty.data.ttys.length) + return; + var t = clite.tty.data.ttys[id]; + if (x > -1 && x < 80) + t.x = x; + if (y > -1 && y < 25) + t.y = y; } }; diff --git a/clite/libcurses.js b/clite/libcurses.js index f4f1488..fd3c03d 100644 --- a/clite/libcurses.js +++ b/clite/libcurses.js @@ -14,6 +14,23 @@ return Object.create({ A_NULL:0, A_BOLD:1, }, + key:{ + KEY_DOWN:-3, + KEY_UP:-4, + KEY_LEFT:-5, + KEY_RIGHT:-6, + KEY_ENTER:String.fromCharCode(10), + KEY_ESCAPE:27, + KEY_SHIFT:-2, + KEY_TAB:'\t', + KEY_DL:String.fromCharCode(127), + KEY_BACKSPACE:String.fromCharCode(8), + KEY_CONTROL:-1, + KEY_PPAGE:-7, + KEY_NPAGE:-8, + KEY_HOME:-9, + KEY_END:-10, + }, attmap:function(t) { switch (t) { case this.types.A_BOLD: @@ -27,7 +44,7 @@ return Object.create({ // curses only works on a tty if (!clite.io.isatty(io.pid,io.stdin)) return false; - // TODO: get the controlling tty id + this.ctty = clite.proc.getTTY(io.pid); this.size.rows = clite.tty.ttyctrl(this.ctty,'rows'); this.size.cols = clite.tty.ttyctrl(this.ctty,'cols'); clite.tty.clear(this.ctty); @@ -128,8 +145,13 @@ return Object.create({ ptrx.value = this.size.cols; ptry.value = this.size.rows; return true; - } + }, + move:function(y,x) { + if (!this.isinit) + return false; + clite.tty.setCursor(this.ctty,x,y); + } }); diff --git a/clite/vi.js b/clite/vi.js index 2d06c2b..9ff2e34 100644 --- a/clite/vi.js +++ b/clite/vi.js @@ -14,6 +14,11 @@ clite.commands.load('vi',function(args,env,io) { var changed = false; var num = false; var message = ''; + var cols; + var rows; + var lines = []; + var topLine = 0; + var bottomLine = 0; function help() { stdio.printf(` @@ -47,12 +52,6 @@ Options: if (!stdio.isatty(io.stdin)) return 1; - var cols; - var rows; - var lines = []; - var topLine = 0; - var bottomLine = 0; - function calculateBottomLine() { bottomLine = lines.length; var r = rows; @@ -75,14 +74,14 @@ Options: if (line < 0) line = 0; var start = line; - var end = line+rows; + var end = line+rows-2; if (end > lines.length) end = lines.length; curses.clear(); if (num) { for (var i=start; i 0) { var l = lines[cursor_line][0]; var b = l.substring(0,cursor_col-1); @@ -369,16 +370,16 @@ Options: showAt(topLine); } break; - case 'Enter': + case curses.key.KEY_ENTER: var l = lines[cursor_line][0]; var b = l.substring(0,cursor_col); var e = l.substring(cursor_col); setLine(b); insertLine(e); - cursor_col = e.length; + cursor_col = 0; showAt(topLine); break; - case 'Delete': + case curses.key.KEY_DL: if (cursor_col < lines[cursor_line][1]) { var l = lines[cursor_line][0]; var b = l.substring(0,cursor_col); @@ -388,7 +389,7 @@ Options: } break; default: - if (key.length == 1) { + if (typeof key === 'string' && key.length == 1) { var l = lines[cursor_line][0]; var b = l.substring(0,cursor_col); var e = l.substring(cursor_col);