Skip to content

Commit

Permalink
Merge pull request #4 from WhirIO/fix/quality
Browse files Browse the repository at this point in the history
Fix/quality
  • Loading branch information
aichholzer authored Jun 6, 2017
2 parents cbb5ead + 9ea1a30 commit 4315979
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 90 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
<p>&nbsp;</p>

[![Alpha](https://img.shields.io/badge/Status-ALPHA-8456AC.svg)](https://github.com/WhirIO/Client)
[![ES7](https://img.shields.io/badge/Uses-ES7-00008B.svg)](https://github.com/WhirIO/Client)
[![ES7](https://img.shields.io/badge/JavaScript-ES7-00008B.svg)](https://github.com/WhirIO/Client)
[![codebeat badge](https://codebeat.co/badges/f82e9789-770f-4a7a-a8e5-c9e061f71f77)](https://codebeat.co/projects/github-com-whirio-client-master)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/a360fc01aeab4d7084254dc6c09de3d9)](https://www.codacy.com/app/aichholzer/Client?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=WhirIO/Client&amp;utm_campaign=Badge_Grade)
[![Downloads](https://img.shields.io/npm/dt/whir.io.svg)](https://www.npmjs.com/package/whir.io)
[![Dependency status](https://gemnasium.com/badges/github.com/WhirIO/Client.svg)](https://gemnasium.com/github.com/WhirIO/Client)

Expand Down
89 changes: 42 additions & 47 deletions app/core/components.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,47 @@
const blessed = require('blessed');
const Emitter = require('events').EventEmitter;

/**
* Enable scrolling through the conversation with the arrow keys.
* @see screen.input.key('up', scroll);
* @see screen.input.key('down', scroll);
*/
const inputHandler = (screen) => {
const scroll = (char, key) => {
const condition = () => (key.name === 'up' ? screen.scrollIndex < screen.scroll.length : screen.scrollIndex > 1);
if (screen.scroll.length) {
let found = false;
while (!found) {
if (condition()) {
found = true;
screen.scrollIndex += key.name === 'up' ? 1 : -1;
const data = screen.scroll[screen.scroll.length - screen.scrollIndex];
screen.input.setValue(data.message);
return screen.render();
}
found = true;
}
}
return true;
};

screen.input.key(['up', 'down'], scroll);
screen.input.key(['C-c'], () => {
screen.input.clearValue();
return screen.render();
});

screen.input.on('submit', (value) => {
value = value.trim();
if (!value) {
return screen.render();
}

screen.input.clearValue();
return screen.emit('message', value.trim());
});
};

class Components extends Emitter {

constructor(options) {
Expand Down Expand Up @@ -128,53 +169,7 @@ class Components extends Emitter {
inputOnFocus: true
});

/**
* Enable scrolling through the conversation with the arrow keys.
* @see this.input.key('up', scroll);
* @see this.input.key('down', scroll);
*/
const scroll = (char, key) => {
const condition = () => {
if (key.name === 'up') {
return this.scrollIndex < this.scroll.length;
}

return this.scrollIndex > 1;
};

if (this.scroll.length) {
let found = false;
while (!found) {
if (condition()) {
found = true;
this.scrollIndex += key.name === 'up' ? 1 : -1;
const data = this.scroll[this.scroll.length - this.scrollIndex];
this.input.setValue(data.message);
return this.render();
}
found = true;
}
}

return true;
};

this.input.key(['up', 'down'], scroll);
this.input.key(['C-c'], () => {
this.input.clearValue();
return this.render();
});

this.input.on('submit', (value) => {
value = value.trim();
if (!value) {
return this.render();
}

this.input.clearValue();
return this.emit('message', value.trim());
});

inputHandler(this);
return this.input;
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/core/screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class Screen {
passedItem = item.value;
}

const line = `\u258B ${string.pad(key, 'right', padding)}${chalk.white(passedItem)}`;
const line = `\u258B ${string.pad({ key, side: 'right', padding })}${chalk.white(passedItem)}`;
this.components.timeline.pushLine(line);
});
}
Expand Down
73 changes: 37 additions & 36 deletions app/core/whir.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,7 @@ class Whir extends Emitter {
.on('open', async () => {
this.socket.whirAlive = true;
})
.on('message', async (data) => {
try {
if (!this.isLoaded) {
this.spinner.stop(true);
this.screen = new Screen(this, { user: this.user, scrollSize: this.scroll, mute: this.muteChannel });
this.screen.muteChannel = true;

await this.loadHistory();
this.screen.muteChannel = this.muteChannel;
this.isLoaded = true;
}

data = JSON.parse(data.toString('utf8'));
this.channel = data.channel || this.channel;
data.timestamp = (new Date()).getTime();

await this.writeHistory(data);
return this.emit('received', data);
} catch (error) {
return this.emit('alert', error);
}
})
.on('message', this.messageHandler.bind(this))
.on('error', (error) => {
this.spinner.stop(true);
this.emit('error', error);
Expand All @@ -113,6 +92,29 @@ class Whir extends Emitter {
});
}

async messageHandler(data) {
try {
if (!this.isLoaded) {
this.spinner.stop(true);
this.screen = new Screen(this, { user: this.user, scrollSize: this.scroll, mute: this.muteChannel });
this.screen.muteChannel = true;

await this.loadHistory();
this.screen.muteChannel = this.muteChannel;
this.isLoaded = true;
}

data = JSON.parse(data.toString('utf8'));
this.channel = data.channel || this.channel;
data.timestamp = (new Date()).getTime();

await this.writeHistory(data);
return this.emit('received', data);
} catch (error) {
return this.emit('error', error);
}
}

send(message) {
const data = {
user: this.user,
Expand All @@ -126,18 +128,15 @@ class Whir extends Emitter {
data.command = data.message.replace(/^\//g, '');
switch (data.command) {
case 'exit': return this.screen.destroy(true);
case 'clear':
localCommand = true;
case 'clear': localCommand = true;
this.screen.components.timeline.getLines().forEach((lines, index) => {
this.screen.components.timeline.deleteLine(index);
});
break;
case 'mute':
localCommand = true;
case 'mute': localCommand = true;
this.screen.muteChannel = true;
break;
case 'unmute':
localCommand = true;
case 'unmute': localCommand = true;
this.screen.muteChannel = false;
break;
default:
Expand All @@ -150,7 +149,6 @@ class Whir extends Emitter {

this.writeHistory(data);
this.screen.scroll(data);

return this.emit('sent', data);
}

Expand All @@ -173,12 +171,10 @@ class Whir extends Emitter {

loadHistory() {
const history = `${this.store}/${this.user}.${this.channel}.whir`;
return new Promise((yes, no) => lineReader.createInterface({
input: fs.createReadStream(history)
.on('error', yes.bind(null, 'no_history'))
.on('end', yes)
})
.on('line', (line) => {
const fileStream = yes => fs.createReadStream(history)
.on('error', yes.bind(null, 'no_history'))
.on('end', yes);
const readLine = (no, line) => {
try {
const data = JSON.parse(line);
data.fromHistory = true;
Expand All @@ -190,7 +186,12 @@ class Whir extends Emitter {
} catch (error) {
return no(error);
}
}));
};

return new Promise((yes, no) => {
lineReader.createInterface({ input: fileStream(yes) })
.on('line', readLine.bind(null, no));
});
}

error(data) {
Expand Down
10 changes: 5 additions & 5 deletions app/library/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ module.exports = {

emojinize: input => input.replace(/:([\w]+):/g, (match, icon) => emoji[icon] || match),

pad: (string, side = 'right', padding = null, char = ' ') => {
if (!string || !padding || string.length >= padding) {
return string;
pad: ({ key, side = 'right', padding = null, char = ' ' }) => {
if (!key || !padding || key.length >= padding) {
return key;
}

const pad = char.repeat(padding - string.length);
return side === 'right' ? string + pad : pad + string;
const pad = char.repeat(padding - key.length);
return side === 'right' ? key + pad : pad + key;
}
};

0 comments on commit 4315979

Please sign in to comment.