Window.prototype.append_line = function(message, options){ var curx = this.window.curx; var cury = this.window.cury; options = _.defaults(options, { color: 0 }); this.window.cursor(this.window.height-3, 0); this.window.attron(nc.colorPair(options.color)); if(_.has(options, "row") && _.has(options, "column")) this.window.print(options.row, options.column, message); else this.window.print(message); this.window.attroff(nc.colorPair(options.color)); this.window.cursor(cury, curx); this.window.refresh(); var self = this; if(_.has(options, "duration")){ clearTimeout(self.window.message); self.window.message = setTimeout(function(){ var curx = self.window.curx; var cury = self.window.cury; self.window.cursor(self.window.height-3, 0); self.window.clrtoeol(); self.window.refresh(); self.window.cursor(cury, curx); }, options.duration); } }
function drawFooter(mywin, state){ mywin.cursor(mywin.height-2,0); mywin.clrtoeol(); win.addstr("HabitRPG"); mywin.addstr(mywin.height-2, mywin.width-(Math.min(state.length, mywin.width)), state, mywin.width); mywin.chgat(mywin.height-2, 0, mywin.width, nc.attrs.STANDOUT, nc.colorPair(5)); }
function drawBar(mywin, onColorPair, offColorPair, val, valMax, label, rowStart){ var totalwidth = mywin.width - 6, onWidth = Math.floor(totalwidth * (val / valMax)), offWidth = totalwidth - onWidth, poststring = '' + val + '/' + valMax, paddedstring = label + Array(totalwidth - label.length + 1 - poststring.length).join(' ') + poststring; mywin.cursor(rowStart,3); mywin.attron(nc.colorPair(1)); for (var i = 0; i < paddedstring.length; i++){ if(i > onWidth){ mywin.attroff(nc.colorPair(1)); mywin.attron(nc.colorPair(2)); } mywin.addstr(paddedstring.charAt(i)); } mywin.attroff(nc.colorPair(2)); mywin.refresh(); }
var VerboseWindowUI = function () { SimpleWindowUI.call(this); this.nowPlayingWindow.resize(parseInt(nc.lines / 2), nc.cols); this.logWindow = new nc.Window(nc.lines - this.nowPlayingWindow.height, nc.cols, this.nowPlayingWindow.height, 0); this.logWindow.attron(nc.colorPair(2, nc.colors['CYAN'] + 8, nc.attrs['NORMAL'])); this.logBuffer = []; };
return function(){ var state = (HabitAPI.connected?(HabitAPI.unsaved?'unsaved':''):'disconnected'); statusBarWin.cursor(0,0); statusBarWin.clrtoeol(); statusBarWin.addstr("HabitRPG (? for help)"); statusBarWin.addstr(0, nc.cols-(Math.min(state.length, nc.cols)), state, nc.cols); statusBarWin.chgat(0, 0, nc.cols, nc.attrs.STANDOUT, nc.colorPair(5)); statusBarWin.refresh(); }
function addLine (head, message) { var curx = scrollback.curx, cury = scrollback.cury; scrollback.scroll(); scrollback.cursor(scrollback.height - 3, 0); scrollback.attron(curses.colorPair(3, 2, 0)); scrollback.print(head); scrollback.attrset(0); scrollback.print(' ' + message); scrollback.cursor(cury, curx); scrollback.refresh(); }
var SimpleWindowUI = function () { SimpleUI.call(this); var self = this; this.nowPlayingWindow = new nc.Window(); this.nowPlayingWindow.attron(nc.colorPair(1, nc.colors['GREEN'], nc.attrs['NORMAL'])); this.currentTrackMetadata = null; nc.showCursor = false; process.on('SIGWINCH', function () { self.resize(); }); };
inputWindow.on('inputChar', function (c, i) { if(mode == 'normal'){ inputWindow.clear(); //console.log(i); //process.exit(0); if(i === 106 || i === 259){ // J, up arrow: move up win.chgat(items[currentIndex].cury, 2, win.width-5, nc.attrs.NORMAL, nc.colorPair(0)); currentIndex++; if(currentIndex > items.length -1){ currentIndex = 0; } win.chgat(items[currentIndex].cury, 2, win.width-5, nc.attrs.STANDOUT, nc.colorPair(5)); win.cursor(items[currentIndex].cury,2); win.refresh(); } else if(i === 107 || i === 258){ // K, down arrow : move down win.chgat(items[currentIndex].cury, 2, win.width-5, nc.attrs.NORMAL, nc.colorPair(0)); currentIndex--; if(currentIndex < 0){ currentIndex = items.length-1; } win.chgat(items[currentIndex].cury, 2, win.width-5, nc.attrs.STANDOUT, nc.colorPair(5)); win.cursor(items[currentIndex].cury,2); win.refresh(); } else if(i === 120 || i === 32 || i === 10){ // X and Space and Enter if(items[currentIndex].type == 'daily' || items[currentIndex].type == 'todos'){ unsaved = true; items[currentIndex].done = (items[currentIndex].done * -1) + 1; setTaskStatus(items[currentIndex].id,items[currentIndex].done); } else if(items[currentIndex].type == 'habits'){ doHabit(items[currentIndex].id,'up'); } if(items[currentIndex].type == 'todos'){ currentIndex--; } } else if(i === 100 || i === 45){ // d, minus to decrement whatever you are on unsaved = true; if(items[currentIndex].type == 'daily' || items[currentIndex].type == 'todos'){ setTaskStatus(items[currentIndex].id,items[currentIndex].done); } else if(items[currentIndex].type == 'habits'){ //items[currentIndex].down += 1; doHabit(items[currentIndex].id,'down'); } } else if(i === 58){ mode = 'command'; inputWindow.inbuffer = ''; nc.showCursor = true; } } if(mode == 'command'){ if(i === 9){ mode = 'normal'; inputWindow.inbuffer = ''; nc.showCursor = false; inputWindow.refresh(); }else if(i === 330){ var prev_x = inputWindow.curx; inputWindow.delch(inputWindow.height-1, inputWindow.curx); inputWindow.inbuffer = inputWindow.inbuffer.substring(0, inputWindow.curx-1) + inputWindow.inbuffer.substring(inputWindow.curx); inputWindow.cursor(inputWindow.height-1, prev_x); if(inputWindow.inbuffer.length == 0){ mode = 'normal'; nc.showCursor = false; } inputWindow.refresh(); } else if (i === 127 && inputWindow.curx > 0) { var prev_x = inputWindow.curx-1; inputWindow.delch(inputWindow.height-1, prev_x); inputWindow.inbuffer = inputWindow.inbuffer.substring(0, prev_x) + inputWindow.inbuffer.substring(prev_x+1); inputWindow.cursor(inputWindow.height-1, prev_x); if(inputWindow.inbuffer.length == 0){ mode = 'normal'; nc.showCursor = false; } inputWindow.refresh(); } else if (i === nc.keys.NEWLINE) { if (inputWindow.inbuffer.length) { if (inputWindow.inbuffer[0] === ':') { var cmd = inputWindow.inbuffer.substring(1).split(' ', 1).join('').trim(), args = inputWindow.inbuffer.substring(inputWindow.inbuffer.indexOf(cmd)+cmd.length+1).trim(); switch (cmd.toLowerCase()) { case 't': if (args.length) { inputWindow.clear(); inputWindow.inbuffer = ''; nc.showCursor = false; mode = 'normal'; unsaved = true; createTask("todo",args); } break; case 'h': if (args.length) { inputWindow.clear(); inputWindow.inbuffer = ''; nc.showCursor = false; mode = 'normal'; unsaved = true; createTask("habit",args); } break; case 'd': if (args.length) { inputWindow.clear(); inputWindow.inbuffer = ''; nc.showCursor = false; mode = 'normal'; unsaved = true; createTask("daily",args); } break; case 'r': if (args.length) { inputWindow.clear(); inputWindow.inbuffer = ''; nc.showCursor = false; mode = 'normal'; unsaved = true; renameTask(items[currentIndex].id,args); } break; case 'delete': deleteTask(items[currentIndex].id); inputWindow.clear(); inputWindow.inbuffer = ''; nc.showCursor = false; mode = 'normal'; unsaved = true; break; case 'q': nc.cleanup(); process.exit(0); break; default: inputWindow.clear(); inputWindow.addstr('Unknown command: ' + cmd); mode = 'normal'; nc.showCursor = false; } } } } else if (i >= 32 && i <= 126 && inputWindow.curx < inputWindow.width-4) { inputWindow.echochar(i); inputWindow.inbuffer += c; } } inputWindow.refresh(); });
var drawFn = function(){ items = []; win.clear(); /* draw the header */ drawFooter(win,(unsaved?'unsaved':'')); /* draw the status box */ statusWindow.move(1,2); statusWindow.box(); statusWindow.cursor(0,2); statusWindow.addstr(data.username); statusWindow.addstr(' [lvl ' + data.level + ']'); statusWindow.refresh(); drawBar(statusWindow,1,2,data.health,data.healthMax,'Health',2); drawBar(statusWindow,1,2,data.exp,data.expMax,'Exp',4); win.cursor(9,0); var habitsDone = 0; win.cursor(win.cury+1,0); for(var i = 0; i<data.habits.length;i++){ win.cursor(win.cury+1,2); data.habits[i].cury = win.cury; data.habits[i].type = 'habits'; items.push(data.habits[i]); win.addstr('[' + (data.habits[i].up - data.habits[i].down == 0?' ':'' + Math.abs(data.habits[i].up - data.habits[i].down)) + '] ' + data.habits[i].name.substr(0,win.width-5)); if (data.habits[i].up - data.habits[i].down < 0){ win.cursor(win.cury,3); win.addstr('-'); //win.chgat(win.cury, 3, 1, nc.attrs.NORMAL, nc.colorPair(3)); } habitsDone += data.habits[i].up; habitsDone -= data.habits[i].down; } win.cursor(win.cury-data.habits.length-1,0); drawHeader(win,"Habits [" + habitsDone + " today]"); win.cursor(win.cury+data.habits.length+4,0); var dailyDone = 0; for(var i = 0; i<data.daily.length;i++){ win.cursor(win.cury+1,2); data.daily[i].cury = win.cury; data.daily[i].type = 'daily'; items.push(data.daily[i]); win.addstr('[' + (data.daily[i].done?'X':' ') + '] ' + data.daily[i].name.substr(0,win.width-5)); dailyDone += data.daily[i].done; } win.cursor(win.cury-data.daily.length-1,0); drawHeader(win,"Daily [" + dailyDone + " complete]"); win.cursor(win.cury+data.daily.length+4,0); var todosDone = 0; for(var i = 0; i<data.todos.length;i++){ win.cursor(win.cury+1,2); data.todos[i].cury = win.cury; data.todos[i].type = 'todos'; items.push(data.todos[i]); win.addstr('[' + (data.todos[i].done?'X':' ') + '] ' + data.todos[i].name.substr(0,win.width-5)); todosDone += data.todos[i].done; } win.cursor(win.cury-data.todos.length-1,0); drawHeader(win,"Todos [" + todosDone + " completed today]"); win.cursor(win.cury+data.todos.length+4,0); win.chgat(items[currentIndex].cury, 2, win.width-5, nc.attrs.STANDOUT, nc.colorPair(5)); win.refresh(); }
}); } var createTask = function(tasktype, text){ request.post(fullapiurl + "/user/task").set('Accept', 'application/json').set('X-API-User', config.apiuser).set('X-API-Key', config.apitoken).send({type:tasktype, text:text}).end(function(res){ refresh(); }); } refresh(); setInterval(refresh,10000); var items = []; nc.colorPair(1,nc.colors.BLACK,nc.colors.WHITE); nc.colorPair(2,nc.colors.WHITE,nc.colors.BLACK); nc.colorPair(3,nc.colors.WHITE,nc.colors.CYAN); var statusWindow = new nc.Window(7,nc.cols-4); var currentIndex = 0; var unsaved = false; var drawFn = function(){ items = []; win.clear(); /* draw the header */ drawFooter(win,(unsaved?'unsaved':'')); /* draw the status box */
moveCursor: function(inc){ LogWindow.log("Moving cursor " + (inc > 0 ? "down" : "up")); if(items[currentIndex].border == inc){ LogWindow.log("Hit fold going " + (inc > 0 ? "down" : "up")); if(items[currentIndex].type == "habit"){ habitOffset += inc; } else if(items[currentIndex].type == "daily"){ dailyOffset += inc; } else if(items[currentIndex].type == "todo"){ todoOffset += inc; } TaskList.refresh(); } else if(currentIndex + inc < items.length && currentIndex + inc >= 0){ if(items[currentIndex + inc].outby != 0){ if(items[currentIndex + inc].type == "habit"){ habitOffset += items[currentIndex + inc].outby; } else if(items[currentIndex + inc].type =="daily"){ dailyOffset += items[currentIndex + inc].outby; } else if(items[currentIndex + inc].type =="todo"){ todoOffset += items[currentIndex + inc].outby; } currentIndex += inc; TaskList.refresh(); } else { taskWin.chgat(items[currentIndex].cury, 2, taskWin.width-4, nc.attrs.NORMAL, nc.colorPair(0)); currentIndex += inc; taskWin.chgat(items[currentIndex].cury, 2, taskWin.width-4, nc.attrs.NORMAL, nc.colorPair(5)); taskWin.refresh(); } } },
refresh: function(){ var t; // for convenience setSectionSizes(); taskWin.erase(); taskWin.refresh(); items = []; taskWin.cursor(0,0); drawHeader("Habits [" + HabitAPI.data.stats.habitToday + " today]"); if(habitOffset > 0){ taskWin.centertext(taskWin.cury+1," - - - - - - ".substring(0,taskWin.width-3)); } else { taskWin.cursor(taskWin.cury+1,2); } taskWin.cursor(taskWin.cury+1,2); for(var i = 0; i<HabitAPI.data.habitIds.length;i++){ t = HabitAPI.data.tasks[HabitAPI.data.habitIds[i]]; if(habitOffset > i){ // above the fold t.cury = taskWin.cury; t.outby = i - habitOffset; t.border=0; } else if (i >= maxHabitLines + habitOffset) { // below the fold t.cury = taskWin.cury-1; t.outby = i-maxHabitLines + habitOffset+1; t.border=0; } else { // right on target t.cury = taskWin.cury; if(i===habitOffset && habitOffset > 0){ t.border=-1; } else if(i=== maxHabitLines + habitOffset -1 && HabitAPI.data.habitIds.length > maxHabitLines + habitOffset){ t.border=1; } else { t.border = 0; } t.outby = 0; taskWin.addstr('[' + (t.up - t.down == 0?' ':'' + Math.abs(t.up - t.down)) + '] ' + t.text.substr(0,taskWin.width-5)); if (t.up - t.down < 0){ taskWin.cursor(taskWin.cury,3); taskWin.addstr('-'); } taskWin.cursor(taskWin.cury+1,2); } items.push(t); } if(HabitAPI.data.habitIds.length > maxHabitLines + habitOffset){ taskWin.centertext(taskWin.cury," - - - - - - ".substring(0,taskWin.width-3)); } taskWin.cursor(taskWin.cury+1,0); drawHeader("Daily [" + HabitAPI.data.stats.dailyToday + " complete]"); if(dailyOffset > 0){ taskWin.centertext(taskWin.cury+1," - - - - - - ".substring(0,taskWin.width-3)); } else { taskWin.cursor(taskWin.cury+1,2); } taskWin.cursor(taskWin.cury+1,2); for(var i = 0; i<HabitAPI.data.dailyIds.length;i++){ t = HabitAPI.data.tasks[HabitAPI.data.dailyIds[i]]; if(dailyOffset > i){ // above the fold t.cury = taskWin.cury; t.outby = i - dailyOffset; t.border=0; } else if (i >= maxDailyLines + dailyOffset) { // below the fold t.cury = taskWin.cury-1; t.outby = i-maxDailyLines + dailyOffset+1; t.border=0; } else { // right on target t.cury = taskWin.cury; if(i===dailyOffset && dailyOffset > 0){ t.border=-1; } else if(i=== maxDailyLines + dailyOffset -1 && HabitAPI.data.dailyIds.length > maxDailyLines + dailyOffset){ t.border=1; } else { t.border = 0; } t.outby = 0; taskWin.addstr('[' + (t.completed?'X':' ') + '] ' + t.text.substr(0,taskWin.width-5)); if (t.up - t.down < 0){ taskWin.cursor(taskWin.cury,3); taskWin.addstr('-'); } taskWin.cursor(taskWin.cury+1,2); } items.push(t); } if(HabitAPI.data.dailyIds.length > maxDailyLines + dailyOffset){ taskWin.centertext(taskWin.cury," - - - - - - ".substring(0,taskWin.width-3)); } taskWin.cursor(taskWin.cury+1,0); drawHeader("Todos [" + HabitAPI.data.stats.todoToday + " completed today]"); if(todoOffset > 0){ taskWin.centertext(taskWin.cury+1," - - - - - - ".substring(0,taskWin.width-3)); } else { taskWin.cursor(taskWin.cury+1,2); } taskWin.cursor(taskWin.cury+1,2); var filteredTodos = _.filter(HabitAPI.data.todoIds,function(id){return !HabitAPI.data.tasks[id].completed}); for(var i = 0; i<filteredTodos.length;i++){ t = HabitAPI.data.tasks[filteredTodos[i]]; if(todoOffset > i){ // above the fold t.cury = taskWin.cury; t.outby = i - todoOffset; t.border=0; } else if (i >= maxTodoLines + todoOffset) { // below the fold t.cury = taskWin.cury-1; t.outby = i-maxTodoLines + todoOffset+1; t.border=0; } else { // right on target t.cury = taskWin.cury; if(i===todoOffset && todoOffset > 0){ t.border=-1; } else if(i=== maxTodoLines + todoOffset -1 && filteredTodos.length > maxTodoLines + todoOffset){ t.border=1; } else { t.border = 0; } t.outby = 0; taskWin.addstr('[' + (t.completed?'X':' ') + '] ' + t.text.substr(0,taskWin.width-5)); if (t.up - t.down < 0){ taskWin.cursor(taskWin.cury,3); taskWin.addstr('-'); } taskWin.cursor(taskWin.cury+1,2); } items.push(t); } if(filteredTodos.length > maxTodoLines + todoOffset){ taskWin.centertext(taskWin.cury," - - - - - - ".substring(0,taskWin.width-3)); } // draw where the cursor is now if(items.length > 0 && items[currentIndex] && items[currentIndex].cury ){ try{ taskWin.chgat(items[currentIndex].cury, 2, taskWin.width-4, nc.attrs.STANDOUT, nc.colorPair(5)); } catch(e){ LogWindow.log(e,1); } } else { currentIndex = 0; } taskWin.refresh(); },
var amqp = require('amqp'), curses = require('ncurses'), util = require('util'), meid = Math.round(Math.random()*1000000000), me = 'app-console-' + String(meid), conn = amqp.createConnection(); var root = new curses.Window(); curses.colorPair(1, 2, 0); var rightbar = new curses.Window(root.height, 21, 0, root.width - 21); rightbar.hline(rightbar.width); rightbar.attron(curses.attrs.BOLD); rightbar.print(' '); rightbar.attron(curses.colorPair(1)); rightbar.print('Installed Apps'); rightbar.attrset(0); rightbar.print(' +'); rightbar.refresh(); var sep = new curses.Window(root.height, 1, 0, root.width - 22); sep.attrset(0); sep.vline(sep.height); sep.cursor(0, 0); sep.print('+'); sep.refresh(); var scrollback = new curses.Window(root.height, root.width - 22, 0, 0); scrollback.attrset(0);