Skip to content

Commit

Permalink
fix clock centering.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Oct 16, 2013
1 parent 3e12897 commit c8116f6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
39 changes: 35 additions & 4 deletions example/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
* https://github.com/chjj/blessed
*/

process.title = 'time.js';

var argv = process.argv;

if (~argv.indexOf('-h') || ~argv.indexOf('--help')) {
console.log('Options:');
console.log('-s - Show seconds.');
console.log('-n - No leading zero on hours.');
console.log('-d - Show date box.');
return process.exit(0);
}

var blessed = require('blessed');

var screen = blessed.screen({
Expand All @@ -27,10 +39,20 @@ var container = blessed.box({
//}
});

// Workaround for centering shrunken box.
container.on('prerender', function() {
var lpos = container._getCoords(true);
if (lpos) {
container.rleft = (screen.width - (lpos.xl - lpos.xi)) / 2 | 0;
}
});

var date = blessed.box({
parent: screen,
top: 1,
left: 1,
//top: '80%',
//left: 'center',
width: 'shrink',
height: 'shrink',
border: {
Expand All @@ -39,6 +61,8 @@ var date = blessed.box({
}
});

date.hide();

for (var i = 0; i < 10; i++) {
var symbols = positions[i] = {};

Expand Down Expand Up @@ -853,12 +877,16 @@ function updateTime() {
s = '0' + s;
}

time = process.argv[2] === '-s'
time = ~argv.indexOf('-s')
? h + ':' + m + ':' + s + im
: h + ':' + m + im;

time = time.split('');

if (~argv.indexOf('-n')) {
if (time[0] === '0') time[0] = ' ';
}

Object.keys(positions).forEach(function(key) {
var symbols = positions[key];
Object.keys(symbols).forEach(function(key) {
Expand All @@ -870,19 +898,22 @@ function updateTime() {
var symbols = positions[i]
, symbol = symbols[ch];

if (!symbol) return;

symbol.rleft = pos;
pos += symbol.width + 2;

symbol.show();
});

date.setContent(d.toISOString());
if (~argv.indexOf('-d')) {
date.show();
date.setContent(d.toISOString());
}

screen.render();
}

screen.render();

setInterval(updateTime, 1000);

updateTime();
Expand Down
4 changes: 4 additions & 0 deletions lib/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,8 @@ Screen.prototype.cleanSides = function(el) {
};

Screen.prototype.draw = function(start, end) {
// this.emit('predraw');

var x
, y
, line
Expand Down Expand Up @@ -1109,6 +1111,8 @@ Screen.prototype.draw = function(start, end) {
// this.program.output.write(pre + main + post);
this.program._write(pre + main + post);
}

// this.emit('draw');
};

Screen.prototype._reduceColor = function(col) {
Expand Down

0 comments on commit c8116f6

Please sign in to comment.