1
0
mirror of https://github.com/fazo96/telecommander.git synced 2025-01-10 11:34:20 +01:00

cleaning up and minor features

This commit is contained in:
Enrico Fasoli 2015-08-30 22:19:30 +00:00
parent 588b48a640
commit a3aeca5cf3

View File

@ -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 // Contact list window
var chats = blessed.list({ var chats = blessed.list({
left: 0, left: 0,
@ -71,10 +56,26 @@ var chats = blessed.list({
width: '20%', width: '20%',
border: { type: 'line' }, border: { type: 'line' },
mouse: true, mouse: true,
invertSelected: false,
style: defaultStyle, style: defaultStyle,
}) })
chats.style.selected = { bg: 'blue' } 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 // Command line prompt
var cmdline = blessed.textbox({ var cmdline = blessed.textbox({
inputOnFocus: true, inputOnFocus: true,
@ -96,6 +97,17 @@ msgBox[statusWindow] = mkBox()
screen.append(chats); screen.append(chats);
screen.append(cmdline); screen.append(cmdline);
screen.append(msgBox[statusWindow]); 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 // Contacts holds all the contacts data
var contacts = { } 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 connected = false // keep track of wether we are good to go and logged in
var selectedWindow = statusWindow // the currently selected window 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){ function command(cmd){
cmdl = cmd.split(' ') cmdl = cmd.split(' ')
cmdname = cmdl[0] cmdname = cmdl[0]
@ -182,14 +202,10 @@ function command(cmd){
// Log in finally // Log in finally
if(user.registered) client.auth.signIn(user.phone,user.phoneCodeHash,code,cb) 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 client.auth.signUp(user.phone,user.phoneCodeHash,code,name,lastname,cb)
} else {
} else if(cmdname === 'msg'){ // Send a message log('Command not found.')
sendMsg(cmdl[1],cmdl[2])
} }
} }
chats.addItem(msgBox[statusWindow])
switchToBox(statusWindow)
screen.render();
// What happens when a different window is selected // What happens when a different window is selected
chats.on('select',function(selected){ chats.on('select',function(selected){
@ -198,7 +214,8 @@ chats.on('select',function(selected){
}) })
function switchToBox(boxname){ function switchToBox(boxname){
msgBox[selectedWindow].hide() if(selectedWindow && msgBox[selectedWindow])
msgBox[selectedWindow].hide()
selectedWindow = boxname; selectedWindow = boxname;
var newb = getMsgBox(selectedWindow) var newb = getMsgBox(selectedWindow)
newb.show() newb.show()
@ -228,9 +245,11 @@ cmdline.on('submit',function(value){
if(selectedWindow === statusWindow || nameToObj(selectedWindow) === undefined){ if(selectedWindow === statusWindow || nameToObj(selectedWindow) === undefined){
//log('Window:',selectedWindow,'Eval cmd:',value) //log('Window:',selectedWindow,'Eval cmd:',value)
command(value) command(value)
} else if(value.indexOf('//') === 0){
sendMsg(selectedWindow,value.substring(1))
} else if(value.indexOf('/') === 0){
command(value.substring(1))
} else { } else {
// Send Message
//log('sending message')
sendMsg(selectedWindow,value) sendMsg(selectedWindow,value)
} }
cmdline.clearValue() cmdline.clearValue()
@ -239,14 +258,18 @@ cmdline.on('submit',function(value){
cmdline.focus() // make sure prompt is focused cmdline.focus() // make sure prompt is focused
// Catch ctrl-c or escape event and close program function quit(){
cmdline.key(['escape','C-c'], function(ch,key){
if(connected || client != undefined){ if(connected || client != undefined){
log('Closing communications and shutting down...') log('Closing communications and shutting down...')
client.end(function(){ client.end(function(){
process.exit(0) process.exit(0)
}) })
} else 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){ 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 // Connects to telegram
function connect(){ function connect(){
client = telegramLink.createClient(app, telegramLink.PROD_PRIMARY_DC, function(){ client = telegramLink.createClient(app, telegramLink.PROD_PRIMARY_DC, function(){