lots of old code for proprietary codes and "artificial cursors".

This commit is contained in:
Christopher Jeffrey 2015-03-14 16:35:17 -07:00
parent 3dcc0b3d1b
commit d3e566b9c1
4 changed files with 197 additions and 2 deletions

View File

@ -945,6 +945,24 @@ terminals.
- **getPixelRatio(callback)** - get the pixel to cell ratio for the terminal. - **getPixelRatio(callback)** - get the pixel to cell ratio for the terminal.
### Artificial Cursors
Terminal cursors can be tricky. They all have different custom escape codes to
alter. As an _experimental_ alternative, blessed can draw a cursor for you,
allowing you to have a custom cursor that you control.
``` js
var screen = blessed.screen({
artificialCursor: true,
cursorShape: 'line',
cursorBlink: true,
cursorColor: null // null for default
});
```
That's it. It's controlled the same way as the regular cursor.
### Positioning ### Positioning
Offsets may be a number, a percentage (e.g. `50%`), or a keyword (e.g. Offsets may be a number, a percentage (e.g. `50%`), or a keyword (e.g.

View File

@ -1553,6 +1553,98 @@ Program.prototype.__defineSetter__('title', function(title) {
return this._title; return this._title;
}); });
// Specific to iTerm2, but I think it's really cool.
// Example:
// if (!screen.copyToClipboard(text)) {
// execClipboardProgram(text);
// }
Program.prototype.copyToClipboard = function(text) {
if (this.isiTerm2) {
this._twrite('\x1b]50;CopyToCliboard=' + text + '\x07');
return true;
}
return false;
};
// Only XTerm and iTerm2. If you know of any others, post them.
Program.prototype.cursorShape = function(shape, blink) {
if (this.isiTerm2) {
switch (shape) {
case 'block':
if (!blink) {
this._twrite('\x1b]50;CursorShape=0;BlinkingCursorEnabled=0\x07');
} else {
this._twrite('\x1b]50;CursorShape=0;BlinkingCursorEnabled=1\x07');
}
break;
case 'underline':
if (!blink) {
// this._twrite('\x1b]50;CursorShape=n;BlinkingCursorEnabled=0\x07');
} else {
// this._twrite('\x1b]50;CursorShape=n;BlinkingCursorEnabled=1\x07');
}
break;
case 'line':
if (!blink) {
this._twrite('\x1b]50;CursorShape=1;BlinkingCursorEnabled=0\x07');
} else {
this._twrite('\x1b]50;CursorShape=1;BlinkingCursorEnabled=1\x07');
}
break;
}
return true;
} else if (this.term('xterm')) {
switch (shape) {
case 'block':
if (!blink) {
this._twrite('\x1b[0 q');
} else {
this._twrite('\x1b[1 q');
}
break;
case 'underline':
if (!blink) {
this._twrite('\x1b[2 q');
} else {
this._twrite('\x1b[3 q');
}
break;
case 'line':
if (!blink) {
this._twrite('\x1b[4 q');
} else {
this._twrite('\x1b[5 q');
}
break;
}
return true;
}
return false;
};
Program.prototype.cursorColor = function(color) {
if (this.term('xterm') || this.term('rxvt')) {
this._twrite('\x1b]12;' + color + '\x07');
return true;
}
return false;
};
Program.prototype.resetCursor = function() {
if (this.term('rxvt')) {
// urxvt doesnt support OSC 112
this._twrite('\x1b]12;white\007');
this._twrite('\x1b[0 q');
return true;
} else if (this.term('xterm')) {
// XXX
// return this.resetColors();
this._twrite('\x1b]112\x07');
return true;
}
return false;
};
/** /**
* Normal * Normal
*/ */

View File

@ -321,6 +321,32 @@ function Screen(options) {
this._ci = -1; this._ci = -1;
if (options.artificialCursor) {
this.artificialCursor = true;
this.cursorShape = options.cursorShape || 'block';
this.cursorBlink = options.cursorBlink || false;
this.cursorColor = options.cursorColor || null;
this.cursorState = 1;
this._cursorHidden = true;
var hideCursor = this.program.hideCursor;
this.program.hideCursor = function() {
hideCursor.call(self.program);
self._cursorHidden = true;
if (self.renders) self.render();
};
var showCursor = this.program.showCursor;
this.program.showCursor = function() {
self._cursorHidden = false;
if (self.program._exiting) showCursor.call(self.program);
if (self.renders) self.render();
};
this._blink = setInterval(function() {
if (!self.cursorBlink) return;
self.cursorState ^= 1;
if (self.renders) self.render();
}, 500);
}
function resize() { function resize() {
self.alloc(); self.alloc();
self.render(); self.render();
@ -971,7 +997,10 @@ Screen.prototype.draw = function(start, end) {
line = this.lines[y]; line = this.lines[y];
o = this.olines[y]; o = this.olines[y];
if (!line.dirty) continue; // if (!line.dirty) continue;
if (!line.dirty && !(this.artificialCursor && y === this.program.y)) {
continue;
}
line.dirty = false; line.dirty = false;
out = ''; out = '';
@ -985,6 +1014,26 @@ Screen.prototype.draw = function(start, end) {
data = line[x][0]; data = line[x][0];
ch = line[x][1]; ch = line[x][1];
if (this.artificialCursor
&& !this._cursorHidden
&& this.cursorState
&& x === this.program.x
&& y === this.program.y) {
if (this.cursorShape === 'line') {
data |= 7 << 9;
ch = '\u2502';
} else if (this.cursorShape === 'underline') {
data |= 7 << 9;
data |= 2 << 18;
} else if (this.cursorShape === 'block') {
data |= 7 << 9;
data |= 8 << 18;
}
if (this.cursorColor != null) {
data |= this.cursorColor << 9;
}
}
// Take advantage of xterm's back_color_erase feature by using a // Take advantage of xterm's back_color_erase feature by using a
// lookahead. Stop spitting out so many damn spaces. NOTE: Is checking // lookahead. Stop spitting out so many damn spaces. NOTE: Is checking
// the bg for non BCE terminals worth the overhead? // the bg for non BCE terminals worth the overhead?
@ -1805,6 +1854,37 @@ Screen.prototype.sigtstp = function(callback) {
}); });
}; };
Screen.prototype.copyToClipboard = function(text) {
return this.program.copyToClipboard(text);
};
Screen.prototype.cursorShape = function(shape, blink) {
if (this.artificialCursor) {
this.cursorShape = shape;
this.cursorBlink = blink;
return true;
}
return this.program.cursorShape(shape, blink);
};
Screen.prototype.cursorColor = function(color) {
if (this.artificialCursor) {
this.cursorColor = colors.convert(color);
return true;
}
return this.program.cursorColor(color);
};
Screen.prototype.resetCursor = function() {
if (this.artificialCursor) {
this.cursorShape = 'block';
this.cursorBlink = false;
this.cursorColor = null;
return true;
}
return this.program.resetCursor();
};
/** /**
* Element * Element
*/ */

View File

@ -3,7 +3,12 @@ var blessed = require('../')
screen = blessed.screen({ screen = blessed.screen({
dump: __dirname + '/logs/widget.log', dump: __dirname + '/logs/widget.log',
title: 'widget test' title: 'widget test',
artificialCursor: true,
cursorShape: 'line',
cursorBlink: true,
cursorColor: null
}); });
screen.append(blessed.text({ screen.append(blessed.text({