mirror of
https://codeberg.org/TicklishHoneyBee/CLIte.git
synced 2026-03-11 09:04:37 +00:00
more curses, and get vi working again
This commit is contained in:
parent
140aff20a5
commit
b460a00c6d
4 changed files with 85 additions and 47 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
|
|
|||
77
clite/vi.js
77
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<end; i++) {
|
||||
if (i == cursor_line) {
|
||||
var cursor = '█';
|
||||
var cursor = '_';
|
||||
if (cursor_col == 0) {
|
||||
curses.printw('% 4d%c%s\n',i+1,cursor,lines[i][0]);
|
||||
}else{
|
||||
|
|
@ -100,7 +99,7 @@ Options:
|
|||
}else{
|
||||
for (var i=start; i<end; i++) {
|
||||
if (i == cursor_line) {
|
||||
var cursor = '█';
|
||||
var cursor = '_';
|
||||
if (cursor_col == 0) {
|
||||
curses.printw('%c%s\n',cursor,lines[i][0]);
|
||||
}else{
|
||||
|
|
@ -116,7 +115,16 @@ Options:
|
|||
}
|
||||
}
|
||||
}
|
||||
curses.printw('%s\t\t\t\t\t\t%d,%d\n',message,cursor_line,cursor_col);
|
||||
curses.move(rows-2,0);
|
||||
curses.printw('%s',message);
|
||||
var ct = cursor_line+','+cursor_col;
|
||||
curses.move(rows-2,cols-(ct.length+5));
|
||||
curses.printw('%s',ct);
|
||||
if (cmd_buff != '') {
|
||||
curses.move(rows-1,0);
|
||||
curses.printw('%s',cmd_buff);
|
||||
}
|
||||
//curses.move(cursor_line,cursor_col);
|
||||
}
|
||||
|
||||
function prepFile(fd) {
|
||||
|
|
@ -125,7 +133,7 @@ Options:
|
|||
io.exit(1);
|
||||
return;
|
||||
}
|
||||
var st = stdio.fstat(fd);
|
||||
var st = stdio.fstatat(fd,0);
|
||||
if ( !st || (st.type != stdio.types.FT_TEXT && st.type != stdio.types.FT_SCRIPT)) {
|
||||
stdio.write(io.stderr,'can not read files of this type');
|
||||
io.exit(1);
|
||||
|
|
@ -192,6 +200,7 @@ Options:
|
|||
default:;
|
||||
}
|
||||
}
|
||||
cmd_buff = '';
|
||||
if (write) {
|
||||
writeFile(force);
|
||||
changed = false;
|
||||
|
|
@ -267,7 +276,7 @@ Options:
|
|||
|
||||
curses.initscr();
|
||||
cols = curses.getmaxx();
|
||||
rows = curses.getmaxy()-2;
|
||||
rows = curses.getmaxy();
|
||||
curses.cbreak();
|
||||
curses.noecho();
|
||||
curses.printw('Loading...');
|
||||
|
|
@ -276,20 +285,16 @@ Options:
|
|||
if (mode == 0) { // view mode
|
||||
//message = '';
|
||||
switch (key) {
|
||||
case 'Up':
|
||||
case 'ArrowUp':
|
||||
case curses.key.KEY_UP:
|
||||
cursorUp();
|
||||
break;
|
||||
case 'Down':
|
||||
case 'ArrowDown':
|
||||
case curses.key.KEY_DOWN:
|
||||
cursorDn();
|
||||
break;
|
||||
case 'Left':
|
||||
case 'ArrowLeft':
|
||||
case curses.key.KEY_LEFT:
|
||||
cursorLt();
|
||||
break;
|
||||
case 'Right':
|
||||
case 'ArrowRight':
|
||||
case curses.key.KEY_RIGHT:
|
||||
cursorRt();
|
||||
break;
|
||||
case 'i':
|
||||
|
|
@ -307,7 +312,8 @@ Options:
|
|||
break;
|
||||
case ':':
|
||||
curses.echo();
|
||||
term.ttyctrl('iset',':');
|
||||
curses.move(rows-1,0);
|
||||
curses.printw(':');
|
||||
cmd_buff = ':';
|
||||
mode = 1;
|
||||
showAt(topLine);
|
||||
|
|
@ -316,18 +322,17 @@ Options:
|
|||
}else if (mode == 1) { // command mode
|
||||
//message = '';
|
||||
switch (key) {
|
||||
case 'Escape':
|
||||
case curses.key.KEY_ESCAPE:
|
||||
mode = 0;
|
||||
cmd_buff = '';
|
||||
curses.noecho();
|
||||
showAt(topLine);
|
||||
break;
|
||||
case 'Enter':
|
||||
case curses.key.KEY_ENTER:
|
||||
if (!doCommand(cmd_buff))
|
||||
return;
|
||||
showAt(topLine);
|
||||
cmd_buff = '';
|
||||
term.ttyctrl('iset','');
|
||||
break;
|
||||
default:
|
||||
if (key.length == 1)
|
||||
|
|
@ -337,29 +342,25 @@ Options:
|
|||
}else{ // 2 edit mode
|
||||
message = 'INSERT';
|
||||
switch (key) {
|
||||
case 'Escape':
|
||||
case curses.key.KEY_ESCAPE:
|
||||
mode = 0;
|
||||
curses.noecho();
|
||||
message = '';
|
||||
showAt(topLine);
|
||||
break;
|
||||
case 'Up':
|
||||
case 'ArrowUp':
|
||||
case curses.key.KEY_UP:
|
||||
cursorUp();
|
||||
break;
|
||||
case 'Down':
|
||||
case 'ArrowDown':
|
||||
case curses.key.KEY_DOWN:
|
||||
cursorDn();
|
||||
break;
|
||||
case 'Left':
|
||||
case 'ArrowLeft':
|
||||
case curses.key.KEY_LEFT:
|
||||
cursorLt();
|
||||
break;
|
||||
case 'Right':
|
||||
case 'ArrowRight':
|
||||
case curses.key.KEY_RIGHT:
|
||||
cursorRt();
|
||||
break;
|
||||
case 'Backspace':
|
||||
case curses.key.KEY_BACKSPACE:
|
||||
if (cursor_col > 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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue