add hardware cursor, and improve vi some more

This commit is contained in:
Lisa Milne 2023-12-14 12:53:46 +10:00
parent 32927521b0
commit 975406a803
4 changed files with 105 additions and 66 deletions

View file

@ -291,6 +291,23 @@ bios.video.text = {
c.data.bk = id;
c.className = bios.video.text.internal.dataToClass(c.data);
},
setCursor:function(x,y,enabled) {
if (typeof enabled !== 'boolean')
return;
var c = document.getElementById('vga-cursor');
if (!c)
return;
c.data.x = x;
c.data.y = y;
c.data.enabled = enabled;
if (!enabled) {
c.style.display = 'none';
return;
}
c.style.display = 'block';
c.style.top = (y*bios.video.text.data.screen.nh)+'px';
c.style.left = (x*bios.video.text.data.screen.nw)+'px';
},
writeSet:function(name,x,y,id) {
if (x < 0 || x > 79 || y < 0 || y > 24)
return;
@ -304,6 +321,9 @@ bios.video.text = {
case 2:
bios.video.text.io.setBlink(x,y,id);
break;
case 3:
bios.video.text.io.setCursor(x,y,id);
break;
default:;
}
}
@ -335,11 +355,29 @@ bios.video.text = {
c.style.left = (x*xpx)+'px';
c.style.width = xpx+'px';
c.style.height = ypx+'px';
c.style.zIndex = '5';
c.data = {bg:'0',fg:'F',bk:false};
c.id = 'vga-'+y+'-'+x;
bios.video.text.data.el.appendChild(c);
}
}
// the cursor
var c = document.createElement('div');
c.className = 'vga-cursor vga-blink vga-bt vga-fF';
c.style.position = 'absolute';
c.style.top = '0px';
c.style.left = '0px';
c.style.width = xpx+'px';
c.style.height = ypx+'px';
c.style.zIndex = '10';
c.id = 'vga-cursor';
c.textContent = '_';
c.data = {};
c.data.x = x;
c.data.y = y;
c.data.enabled = false;
bios.video.text.data.el.appendChild(c);
bios.video.text.resize();
bios.video.text.data.isinit = true;
return bios.video.text.data.isinit;
@ -378,6 +416,14 @@ bios.video.text = {
c.style.fontSize = bios.video.text.data.screen.f+'px';
}
}
var c = document.getElementById('vga-cursor');
if (c) {
c.style.top = (y*bios.video.text.data.screen.nh)+'px';
c.style.left = (x*bios.video.text.data.screen.nw)+'px';
c.style.width = bios.video.text.data.screen.nw+'px';
c.style.height = bios.video.text.data.screen.nh+'px';
c.style.fontSize = bios.video.text.data.screen.f+'px';
}
}
};

View file

@ -28,6 +28,7 @@ div span {margin:0; padding:0; border:0;}
.vga-fE {color:#FFFF55;}
.vga-fF {color:#FFFFFF;}
.vga-bt {background-color:transparent;}
.vga-b0 {background-color:#000000;}
.vga-b1 {background-color:#0000AA;}
.vga-b2 {background-color:#00AA00;}

View file

@ -2499,6 +2499,8 @@ clite.tty = {
t.x = x;
if (y > -1 && y < 25)
t.y = y;
if (id == clite.tty.data.activeID)
clite.console.setCursor(x,y);
}
};
@ -2507,7 +2509,7 @@ clite.console = {
vgaio:null,
state:0,
size:[80,25],
cursor:[0,0,0,0]
cursor:[0,0]
},
init:function() {
// set graphics to text mode, and get the VGA api
@ -2564,20 +2566,11 @@ clite.console = {
}
},
moveCursor:function() {
if (
clite.console.data.cursor[0] == clite.console.data.cursor[2]
&& clite.console.data.cursor[1] == clite.console.data.cursor[3]
)
return;
clite.console.data.vgaio.writeSet(2,clite.console.data.cursor[0],clite.console.data.cursor[1],false);
clite.console.data.cursor[0] = clite.console.data.cursor[2];
clite.console.data.cursor[1] = clite.console.data.cursor[3];
//clite.console.data.vgaio.write(clite.console.data.cursor[0],clite.console.data.cursor[1],'_');
clite.console.data.vgaio.writeSet(2,clite.console.data.cursor[0],clite.console.data.cursor[1],true);
clite.console.data.vgaio.writeSet(3,clite.console.data.cursor[0],clite.console.data.cursor[1],true);
},
setCursor:function(x,y) {
clite.console.data.cursor[2] = x;
clite.console.data.cursor[3] = y;
clite.console.data.cursor[0] = x;
clite.console.data.cursor[1] = y;
clite.console.moveCursor();
},
read:function() {

View file

@ -20,6 +20,38 @@ clite.commands.load('vi',function(args,env,io) {
var topLine = 0;
var bottomLine = 0;
const VIMODE_VIEW = 0;
const VIMODE_CMD = 1;
const VIMODE_EDIT = 2;
function switchMode(m) {
if (m == mode)
return;
switch (m) {
case VIMODE_CMD:
mode = 1;
curses.echo();
curses.move(rows-1,0);
curses.printw(':');
cmd_buff = ':';
showAt(topLine);
break;
case VIMODE_EDIT:
mode = 2;
curses.noecho();
message = 'INSERT';
showAt(topLine);
break;
case VIMODE_VIEW:
default:
mode = 0;
cmd_buff = '';
curses.noecho();
showAt(topLine);
break;
}
}
function help() {
stdio.printf(`
vi - Visual text file editor
@ -78,41 +110,20 @@ Options:
if (end > lines.length)
end = lines.length;
curses.clear();
var cursor_screen = [0,cursor_col+1];
var k = 0;
if (num) {
for (var i=start; i<end; i++) {
if (i == cursor_line) {
var cursor = '_';
if (cursor_col == 0) {
curses.printw('% 4d%c%s\n',i+1,cursor,lines[i][0]);
}else{
if (cursor_col > lines[i][0].length)
cursor_col = lines[i][0].length;
var l = lines[i][0];
var b = l.substring(0,cursor_col);
var e = l.substring(cursor_col);
curses.printw('% 4d %s%c%s\n',i+1,b,cursor,e);
}
}else{
curses.printw('% 4d %s\n',i+1,lines[i][0]);
}
cursor_screen[1] += 4;
for (var i=start; i<end; i++,k++) {
if (i == cursor_line)// {
cursor_screen[0] = k;
curses.printw('% 4d %s\n',i+1,lines[i][0]);
}
}else{
for (var i=start; i<end; i++) {
if (i == cursor_line) {
var cursor = '_';
if (cursor_col == 0) {
curses.printw('%c%s\n',cursor,lines[i][0]);
}else{
if (cursor_col > lines[i][0].length)
cursor_col = lines[i][0].length;
var l = lines[i][0];
var b = l.substring(0,cursor_col);
var e = l.substring(cursor_col);
curses.printw(' %s%c%s\n',b,cursor,e);
}
}else{
curses.printw(' %s\n',lines[i][0]);
}
for (var i=start; i<end; i++,k++) {
if (i == cursor_line)// {
cursor_screen[0] = k;
curses.printw(' %s\n',lines[i][0]);
}
}
curses.move(rows-2,0);
@ -123,6 +134,8 @@ Options:
curses.move(rows-1,0);
if (cmd_buff != '')
curses.printw('%s',cmd_buff);
if (mode != 1)
curses.move(cursor_screen[0],cursor_screen[1]);
}
function prepFile(fd) {
@ -296,35 +309,21 @@ Options:
cursorRt();
break;
case 'i':
curses.noecho();
mode = 2;
message = 'INSERT';
showAt(topLine);
switchMode(VIMODE_EDIT);
break;
case 'a':
cursor_col = lines[cursor_line][0].length;
curses.noecho();
mode = 2;
message = 'INSERT';
showAt(topLine);
switchMode(VIMODE_EDIT);
break;
case ':':
curses.echo();
curses.move(rows-1,0);
curses.printw(':');
cmd_buff = ':';
mode = 1;
showAt(topLine);
switchMode(VIMODE_CMD);
break;
}
}else if (mode == 1) { // command mode
//message = '';
switch (key) {
case curses.key.KEY_ESCAPE:
mode = 0;
cmd_buff = '';
curses.noecho();
showAt(topLine);
switchMode(VIMODE_VIEW);
break;
case curses.key.KEY_ENTER:
if (!doCommand(cmd_buff))
@ -336,6 +335,8 @@ Options:
if (cmd_buff.length > 0) {
cmd_buff = cmd_buff.substring(0,cmd_buff.length-1);
showAt(topLine);
if (!cmd_buff.length)
switchMode(VIMODE_VIEW);
}
break;
default:
@ -347,10 +348,8 @@ Options:
message = 'INSERT';
switch (key) {
case curses.key.KEY_ESCAPE:
mode = 0;
curses.noecho();
message = '';
showAt(topLine);
switchMode(VIMODE_VIEW);
break;
case curses.key.KEY_UP:
cursorUp();