From 140aff20a5ebf558811b51e530336a1230339185 Mon Sep 17 00:00:00 2001 From: Lisa Milne Date: Wed, 13 Dec 2023 16:22:07 +1000 Subject: [PATCH] fix libcurses, get less working again --- clite/bios.js | 20 ++++++++++++++++++++ clite/commands.js | 22 ++++++++++------------ clite/core.js | 6 ++++++ clite/libcurses.js | 45 ++++++++++++++++++++++++++------------------- 4 files changed, 62 insertions(+), 31 deletions(-) diff --git a/clite/bios.js b/clite/bios.js index c93552c..8e2d4b6 100644 --- a/clite/bios.js +++ b/clite/bios.js @@ -511,6 +511,26 @@ bios.input.keyboard = { k.lchar = k.char; k.code = -1; break; + case 'PageUp': + k.char = -7; + k.lchr = k.char; + k.code = -7; + break; + case 'PageDown': + k.char = -8; + k.lchr = k.char; + k.code = -8; + break; + case 'Home': + k.char = -9; + k.lchr = k.char; + k.code = -9; + break; + case 'End': + k.char = -10; + k.lchr = k.char; + k.code = -10; + break; default: k.code = key; } diff --git a/clite/commands.js b/clite/commands.js index aee52b7..b335f5d 100644 --- a/clite/commands.js +++ b/clite/commands.js @@ -403,7 +403,7 @@ Options: end = lines.length; curses.clear(); for (var i=start; i cols) { - var sects = line.match(new RegExp('(.{1,'+cols+'})','g')); + if (line.length >= cols) { + var sects = line.match(new RegExp('(.{1,'+(cols-1)+'})','g')); lines.push.apply(lines,sects); return; } @@ -462,27 +462,25 @@ Options: function input(key) { switch (key) { - case 'Escape': + case 27: case 'q': curses.endwin(); io.exit(0); return; break; - case 'Up': - case 'ArrowUp': + case -4: if (topLine > 0) { topLine--; showAt(topLine); } break; - case 'Down': - case 'ArrowDown': + case -3: if (topLine < bottomLine) { topLine++; showAt(topLine); } break; - case 'PageUp': + case -7: if (topLine > 0) { topLine -= rows-1; if (topLine < 0) @@ -490,7 +488,7 @@ Options: showAt(topLine); } break; - case 'PageDown': + case -8: if (topLine < bottomLine) { topLine += rows-1; if (topLine >= bottomLine) @@ -498,11 +496,11 @@ Options: showAt(topLine); } break; - case 'Home': + case -9: topLine = 0; showAt(topLine); break; - case 'End': + case -10: topLine = bottomLine; showAt(topLine); break; diff --git a/clite/core.js b/clite/core.js index 79fa874..bebeadd 100644 --- a/clite/core.js +++ b/clite/core.js @@ -2474,6 +2474,12 @@ clite.tty = { t.echo = v; return t.echo; break; + case 'cols': + return t.data[0].length; + break; + case 'rows': + return t.data.length; + break; default: return false; } diff --git a/clite/libcurses.js b/clite/libcurses.js index d043ccd..f4f1488 100644 --- a/clite/libcurses.js +++ b/clite/libcurses.js @@ -4,6 +4,11 @@ clite.libs.load('libcurses','curses',function(io,env) { return Object.create({ + size:{ + rows:0, + cols:0 + }, + ctty:0, isinit:false, types:{ A_NULL:0, @@ -22,56 +27,58 @@ return Object.create({ // curses only works on a tty if (!clite.io.isatty(io.pid,io.stdin)) return false; - this.isinit = clite.term.ttyctrl('alt',true); + // TODO: get the controlling tty id + this.size.rows = clite.tty.ttyctrl(this.ctty,'rows'); + this.size.cols = clite.tty.ttyctrl(this.ctty,'cols'); + clite.tty.clear(this.ctty); + this.isinit = true; }, endwin:function() { if (!this.isinit) return; - clite.term.ttyctrl('rawin',false); - clite.term.ttyctrl('rawout',false); - clite.term.ttyctrl('echo',true); - clite.term.ttyctrl('iset',''); - this.isinit = clite.term.ttyctrl('alt',false); + clite.tty.ttyctrl(this.ctty,'raw',false); + clite.tty.ttyctrl(this.ctty,'echo',true); + this.isinit = false; }, cbreak:function() { if (!this.isinit) return; - clite.term.ttyctrl('rawin',true); + clite.tty.ttyctrl(this.ctty,'raw',true); }, nocbreak:function() { if (!this.isinit) return; - clite.term.ttyctrl('rawin',false); + clite.tty.ttyctrl(this.ctty,'raw',false); }, raw:function() { if (!this.isinit) return; - clite.term.ttyctrl('rawin',true); + clite.tty.ttyctrl(this.ctty,'raw',true); }, noraw:function() { if (!this.isinit) return; - clite.term.ttyctrl('rawin',false); + clite.tty.ttyctrl(this.ctty,'raw',false); }, clear:function() { if (!this.isinit) return; - clite.term.clear(); + clite.tty.clear(this.ctty); }, echo:function() { if (!this.isinit) return; - clite.term.ttyctrl('echo',true); + clite.tty.ttyctrl(this.ctty,'echo',true); }, noecho:function() { if (!this.isinit) return; - clite.term.ttyctrl('echo',false); + clite.tty.ttyctrl(this.ctty,'echo',false); }, printw:function(fmt) { @@ -96,30 +103,30 @@ return Object.create({ if (!this.isinit) return; a = this.attmap(a); - clite.term.ttyctrl('atton',a); + clite.tty.ttyctrl(this.ctty,'atton',a); }, attroff:function(a) { if (!this.isinit) return; a = this.attmap(a); - clite.term.ttyctrl('attoff',a); + clite.tty.ttyctrl(this.ctty,'attoff',a); }, getmaxx:function() { if (!this.isinit) return 0; - return clite.term.data.alt.cols; + return this.size.cols; }, getmaxy:function() { if (!this.isinit) return 0; - return clite.term.data.alt.lines; + return this.size.rows; }, getmaxyx:function(ptry,ptrx) { if (!this.isinit) return false; - ptrx.value = clite.term.data.alt.cols; - ptry.value = clite.term.data.alt.lines; + ptrx.value = this.size.cols; + ptry.value = this.size.rows; return true; }