From a3aeca5cf32a9627a5d317d4e13ba718a8cfc8b1 Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Sun, 30 Aug 2015 22:19:30 +0000 Subject: [PATCH] cleaning up and minor features --- telecommander.js | 83 ++++++++++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 34 deletions(-) diff --git a/telecommander.js b/telecommander.js index f1ea64c..2f71271 100755 --- a/telecommander.js +++ b/telecommander.js @@ -48,21 +48,6 @@ var defaultStyle = { } } -// Function to create a log box -function mkBox(){ - var box = blessed.log({ - right: 0, - width: '80%', - height: screen.height-3, - border: { type: 'line' }, - scrollable: true, - draggable: true, - style: defaultStyle - }) - box.hide() - return box -} - // Contact list window var chats = blessed.list({ left: 0, @@ -71,10 +56,26 @@ var chats = blessed.list({ width: '20%', border: { type: 'line' }, mouse: true, + invertSelected: false, style: defaultStyle, }) chats.style.selected = { bg: 'blue' } +// Function to create a log box +function mkBox(){ + var box = blessed.log({ + right: 0, + width: '80%', + height: screen.height - cmdline.height, + border: { type: 'line' }, + scrollable: true, + //draggable: true, + style: defaultStyle + }) + box.hide() + return box +} + // Command line prompt var cmdline = blessed.textbox({ inputOnFocus: true, @@ -96,6 +97,17 @@ msgBox[statusWindow] = mkBox() screen.append(chats); screen.append(cmdline); screen.append(msgBox[statusWindow]); +chats.addItem(msgBox[statusWindow]) +switchToBox(statusWindow) +screen.on('resize',function(){ + for(i in msgBox){ + item = msgBox[i] + item.height = screen.height - cmdline.height + } + chats.height = screen.height - cmdline.height + screen.render() +}) +screen.render() // Contacts holds all the contacts data var contacts = { } @@ -113,6 +125,14 @@ var authKey // our authorization key to access telegram var connected = false // keep track of wether we are good to go and logged in var selectedWindow = statusWindow // the currently selected window +// Write something in the Status box +function log(){ + args = Array.prototype.slice.call(arguments) + var msg = args.join(' ') + msgBox[statusWindow].add(msg) + logger.info(msg) +} + function command(cmd){ cmdl = cmd.split(' ') cmdname = cmdl[0] @@ -182,14 +202,10 @@ function command(cmd){ // Log in finally if(user.registered) client.auth.signIn(user.phone,user.phoneCodeHash,code,cb) else client.auth.signUp(user.phone,user.phoneCodeHash,code,name,lastname,cb) - - } else if(cmdname === 'msg'){ // Send a message - sendMsg(cmdl[1],cmdl[2]) + } else { + log('Command not found.') } } -chats.addItem(msgBox[statusWindow]) -switchToBox(statusWindow) -screen.render(); // What happens when a different window is selected chats.on('select',function(selected){ @@ -198,7 +214,8 @@ chats.on('select',function(selected){ }) function switchToBox(boxname){ - msgBox[selectedWindow].hide() + if(selectedWindow && msgBox[selectedWindow]) + msgBox[selectedWindow].hide() selectedWindow = boxname; var newb = getMsgBox(selectedWindow) newb.show() @@ -228,9 +245,11 @@ cmdline.on('submit',function(value){ if(selectedWindow === statusWindow || nameToObj(selectedWindow) === undefined){ //log('Window:',selectedWindow,'Eval cmd:',value) command(value) + } else if(value.indexOf('//') === 0){ + sendMsg(selectedWindow,value.substring(1)) + } else if(value.indexOf('/') === 0){ + command(value.substring(1)) } else { - // Send Message - //log('sending message') sendMsg(selectedWindow,value) } cmdline.clearValue() @@ -239,14 +258,18 @@ cmdline.on('submit',function(value){ cmdline.focus() // make sure prompt is focused -// Catch ctrl-c or escape event and close program -cmdline.key(['escape','C-c'], function(ch,key){ +function quit(){ if(connected || client != undefined){ log('Closing communications and shutting down...') client.end(function(){ process.exit(0) }) } else process.exit(0); +} + +// Catch ctrl-c or escape event and close program +cmdline.key(['escape','C-c'], function(ch,key){ + quit() }); function nameToObj(name){ @@ -279,14 +302,6 @@ function sendMsg(name,str){ }) } -// Write something in the Status box -function log(){ - args = Array.prototype.slice.call(arguments) - var msg = args.join(' ') - msgBox[statusWindow].add(msg) - logger.info(msg) -} - // Connects to telegram function connect(){ client = telegramLink.createClient(app, telegramLink.PROD_PRIMARY_DC, function(){