mirror of
https://codeberg.org/TicklishHoneyBee/CLIte.git
synced 2026-03-11 09:04:37 +00:00
178 lines
3.1 KiB
JavaScript
178 lines
3.1 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,
|
|
},
|
|
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: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:
|
|
return 'bold';
|
|
break;
|
|
}
|
|
return null;
|
|
},
|
|
|
|
initscr:function() {
|
|
// curses only works on a tty
|
|
if (!clite.io.isatty(io.pid,io.stdin) || !clite.io.isatty(io.pid,io.stdout))
|
|
return false;
|
|
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);
|
|
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);
|
|
},
|
|
|
|
addstr:function(str) {
|
|
clite.io.write(io.pid,io.stdout,str);
|
|
},
|
|
|
|
addch:function(ch,opts) {
|
|
if (!this.isinit)
|
|
return;
|
|
this.addstr(ch);
|
|
},
|
|
|
|
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;
|
|
},
|
|
getyx:function() {
|
|
if (!this.isinit)
|
|
return false;
|
|
var c = clite.tty.getCursor(this.ctty);
|
|
if (!Array.isArray(c))
|
|
return [0,0];
|
|
return [c[1],c[0]];
|
|
},
|
|
|
|
move:function(y,x) {
|
|
if (!this.isinit)
|
|
return false;
|
|
clite.tty.setCursor(this.ctty,x,y);
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|