Created
August 13, 2013 15:00
-
-
Save johnspackman/6222068 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var blessed = require('blessed'); | |
function createScreen() { | |
var global = blessed.Screen.global; | |
var screen = blessed.Screen.global || blessed.screen({ tput: true }); | |
if (!global) { | |
screen.program.key('C-c', function() { | |
return process.exit(0); | |
}); | |
} | |
return screen; | |
} | |
function createQuestion() { | |
var screen = createScreen() | |
, question = createQuestion.question; | |
if (question) { | |
question.detach(); | |
screen.append(question); | |
return question; | |
} | |
question = createQuestion.question = blessed.box({ | |
parent: screen, | |
hidden: true, | |
content: '', | |
width: 'half', | |
height: 5, | |
left: 'center', | |
top: 'center', | |
border: { | |
type: 'ascii' | |
}, | |
tags: true | |
}); | |
question._.okay = blessed.button({ | |
parent: question, | |
top: 3, | |
height: 1, | |
left: 2, | |
width: 6, | |
content: 'Okay', | |
align: 'center', | |
bg: 'black', | |
hoverBg: 'blue', | |
autoFocus: false, | |
mouse: true | |
}); | |
question._.cancel = blessed.button({ | |
parent: question, | |
top: 3, | |
height: 1, | |
shrink: true, | |
left: 10, | |
width: 8, | |
content: 'Cancel', | |
align: 'center', | |
bg: 'black', | |
hoverBg: 'blue', | |
autoFocus: false, | |
mouse: true | |
}); | |
question._.ask = function(text, callback) { | |
var press, okay, cancel; | |
question.show(); | |
question.setContent(' ' + text); | |
screen.on('keypress', press = function(ch, key) { | |
if (key.name === 'mouse') return; | |
if (key.name !== 'enter' | |
&& key.name !== 'escape' | |
&& key.name !== 'q' | |
&& key.name !== 'y' | |
&& key.name !== 'n') { | |
return; | |
} | |
done(null, key.name === 'enter' || key.name === 'y'); | |
}); | |
question._.okay.on('press', okay = function() { | |
done(null, true); | |
}); | |
question._.cancel.on('press', cancel = function() { | |
done(null, false); | |
}); | |
screen.saveFocus(); | |
question.focus(); | |
function done(err, data) { | |
question.hide(); | |
screen.restoreFocus(); | |
screen.removeListener('keypress', press); | |
question._.okay.removeListener('press', okay); | |
question._.cancel.removeListener('press', cancel); | |
return callback(err, data); | |
} | |
screen.render(); | |
}; | |
return question; | |
} | |
var question = createQuestion(); | |
question._.ask("Test Question?", function(err, res) { | |
console.log("question answered: err=" + err + ", res=" + res); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment