CLIte/clite/libcurses.js
2023-12-13 16:22:07 +10:00

138 lines
2.3 KiB
JavaScript

clite.libs.data = function() {
clite.libs.load('libcurses','curses',function(io,env) {
return Object.create({
size:{
rows:0,
cols:0
},
ctty:0,
isinit:false,
types:{
A_NULL:0,
A_BOLD:1,
},
attmap:function(t) {
switch (t) {
case this.types.A_BOLD:
return 'bold';
break;
}
return null;
},
initscr:function() {
// curses only works on a tty
if (!clite.io.isatty(io.pid,io.stdin))
return false;
// 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.tty.ttyctrl(this.ctty,'raw',false);
clite.tty.ttyctrl(this.ctty,'echo',true);
this.isinit = false;
},
cbreak:function() {
if (!this.isinit)
return;
clite.tty.ttyctrl(this.ctty,'raw',true);
},
nocbreak:function() {
if (!this.isinit)
return;
clite.tty.ttyctrl(this.ctty,'raw',false);
},
raw:function() {
if (!this.isinit)
return;
clite.tty.ttyctrl(this.ctty,'raw',true);
},
noraw:function() {
if (!this.isinit)
return;
clite.tty.ttyctrl(this.ctty,'raw',false);
},
clear:function() {
if (!this.isinit)
return;
clite.tty.clear(this.ctty);
},
echo:function() {
if (!this.isinit)
return;
clite.tty.ttyctrl(this.ctty,'echo',true);
},
noecho:function() {
if (!this.isinit)
return;
clite.tty.ttyctrl(this.ctty,'echo',false);
},
printw:function(fmt) {
if (!this.isinit)
return;
var args = Array.from(arguments);
args.shift();
clite.io.vfprintf(io.pid,io.stdout,fmt,args);
},
getch:function(cb) {
if (!this.isinit)
return false;
return clite.io.read(io.pid,io.stdin,cb);
},
refresh:function() {
// no op, drawing isn't queued
},
attron:function(a) {
if (!this.isinit)
return;
a = this.attmap(a);
clite.tty.ttyctrl(this.ctty,'atton',a);
},
attroff:function(a) {
if (!this.isinit)
return;
a = this.attmap(a);
clite.tty.ttyctrl(this.ctty,'attoff',a);
},
getmaxx:function() {
if (!this.isinit)
return 0;
return this.size.cols;
},
getmaxy:function() {
if (!this.isinit)
return 0;
return this.size.rows;
},
getmaxyx:function(ptry,ptrx) {
if (!this.isinit)
return false;
ptrx.value = this.size.cols;
ptry.value = this.size.rows;
return true;
}
});
});
};