From 48700a82786b23a4b0b78629fdf21f600f9f5764 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 7 Nov 2018 14:34:06 -0500 Subject: [PATCH] detect typing --- src/index.js | 41 +++++++++++++++++++++++++++++++++++++++-- src/ui.js | 28 +++++++++++++++++++++------- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index cc6ec7c..ed9d103 100644 --- a/src/index.js +++ b/src/index.js @@ -8,6 +8,8 @@ var ui = new UI(); var channels = new ChannelManager(); +let usersTyping = {}; + channels.events.on('update', () => { ui.availableChannels(channels.getChannelList()); }); @@ -43,13 +45,43 @@ var handleProtocolMessages = function(channelName, data) { let user = channels.allUsers.addOrUpdateUserKey(fromUser, data.username); let channel = channels.getChannel(channelName); channel.users.addUserOrUpdate(user); - channels.events.emit("update"); + channels.events.emit("update"); } + + if (msg.type === 'typing') { + usersTyping[fromUser] = (new Date().getTime()); + } } channels.events.on('update', updateUsers); channels.events.on('channelSwitch', updateUsers); +setInterval(function() { + let typingUsers = []; + let currentTime = (new Date().getTime()); + for (let pubkey in usersTyping) { + let lastTyped = usersTyping[pubkey]; + if (currentTime - lastTyped > 5*1000 || currentTime < lastTyped) { + delete usersTyping[pubkey]; + } else { + if (channels.allUsers.users[pubkey]) { + typingUsers.push(channels.allUsers.users[pubkey].username); + } + } + } + + if (typingUsers.length === 0) { + ui.consoleState.setContent(""); + return; + } + if (typingUsers.length === 1) { + ui.consoleState.setContent(typingUsers[0] + " is typing"); + return; + } + + ui.consoleState.setContent(typingUsers.join(', ') + " are typing"); +}, 3*1000); + ui.logEntry(` Welcome to _________ __ __ ____ ___ @@ -119,5 +151,10 @@ ui.events.on('cmd', (cmd) => { } status.sendMessage(channels.getCurrentChannel().name, cmd); -}) +}); + +ui.events.on('typing', () => { + // TODO: use async.cargo instead and/or a to avoid unnecessary requests + status.sendJsonMessage(channels.getCurrentChannel().name, {type: "typing"}); +}); diff --git a/src/ui.js b/src/ui.js index 1df880b..f807fc2 100644 --- a/src/ui.js +++ b/src/ui.js @@ -242,32 +242,46 @@ class UI { self.input.focus(); }); + this.input.key('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''), function () { + self.events.emit("typing"); + }); + this.input.on('submit', this.submitCmd.bind(this)); this.screen.append(this.consoleBox); } layoutState() { - this.consoleState = blessed.box({ - label: '', - tags: true, - padding: 0, + this.consoleStateContainer = blessed.layout({ width: '73%', height: '5%', left: '7%', top: '90%', + layout: "grid" + }); + + this.consoleState = blessed.box({ + parent: this.consoleStateContainer, + label: "", + tags: true, + padding: { + left: 1 + }, + width: "100%", + height: "100%", + valign: "middle", border: { - type: 'line' + type: "line" }, style: { - fg: 'black', + fg: -1, border: { fg: this.color } } }); - this.screen.append(this.consoleState); + this.screen.append(this.consoleStateContainer); } submitCmd(cmd) {