Skip to content

Commit

Permalink
feat: add eslint (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
reconbot authored and lirantal committed Dec 25, 2018
1 parent 0373df4 commit ed68bca
Show file tree
Hide file tree
Showing 22 changed files with 2,402 additions and 923 deletions.
21 changes: 21 additions & 0 deletions .eslintrc.json
@@ -0,0 +1,21 @@
{
"env": {
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 2015
},
"rules": {
"brace-style": "error",
"no-irregular-whitespace":"error",
"no-octal-escape": "error",
"no-octal": "error",
"no-proto":"error",
"strict":["error", "global"],
"no-undef":"error",
"no-use-before-define": "off",
"indent": ["error", 2]
}
}
87 changes: 42 additions & 45 deletions lib/layout/carousel.js
@@ -1,46 +1,43 @@

var blessed = require('blessed')

"use strict";
function Carousel(pages, options) {

this.currPage = 0
this.pages = pages
this.options = options
this.screen = this.options.screen
this.currPage = 0
this.pages = pages
this.options = options
this.screen = this.options.screen
}

Carousel.prototype.move = function() {
var i = this.screen.children.length
while (i--) this.screen.children[i].detach()
Carousel.prototype.move = function() {
var i = this.screen.children.length
while (i--) this.screen.children[i].detach()

this.pages[this.currPage](this.screen, this.currPage);
this.screen.render()
this.pages[this.currPage](this.screen, this.currPage);
this.screen.render()
}

Carousel.prototype.next = function() {
this.currPage++
if (this.currPage==this.pages.length){
if (!this.options.rotate) {
this.currPage--;
return;
} else {
this.currPage=0
}
}
this.move()
Carousel.prototype.next = function() {
this.currPage++
if (this.currPage==this.pages.length){
if (!this.options.rotate) {
this.currPage--;
return;
} else {
this.currPage=0
}
}
this.move()
}

Carousel.prototype.prev = function() {
this.currPage--
if (this.currPage<0) {
if (!this.options.rotate) {
Carousel.prototype.prev = function() {
this.currPage--
if (this.currPage<0) {
if (!this.options.rotate) {
this.currPage++;
return;
} else {
} else {
this.currPage=this.pages.length-1
}
}
this.move()
}
}
this.move()
}

Carousel.prototype.home = function() {
Expand All @@ -54,22 +51,22 @@ Carousel.prototype.end = function() {
}

Carousel.prototype.start = function() {
var self = this
var self = this

this.move()
this.move()

if (this.options.interval) {
setInterval(this.next.bind(this), this.options.interval)
}
if (this.options.interval) {
setInterval(this.next.bind(this), this.options.interval)
}

if (this.options.controlKeys) {
this.screen.key(['right', 'left', 'home', 'end'], function(ch, key) {
if (key.name=='right') self.next()
if (key.name=='left') self.prev()
if (key.name=='home') self.home()
if (key.name=='end') self.end()
});
}
if (this.options.controlKeys) {
this.screen.key(['right', 'left', 'home', 'end'], function(ch, key) {
if (key.name=='right') self.next()
if (key.name=='left') self.prev()
if (key.name=='home') self.home()
if (key.name=='end') self.end()
});
}

}

Expand Down
53 changes: 27 additions & 26 deletions lib/layout/grid.js
@@ -1,39 +1,40 @@
"use strict";
var utils = require('../utils')

var widgetSpacing = 0

function Grid(options) {
if (!options.screen) throw "Error: A screen property must be specified in the grid options.\r\n" +
if (!options.screen) throw "Error: A screen property must be specified in the grid options.\r\n" +
"Note: Release 2.0.0 has breaking changes. Please refer to the README or to https://github.com/yaronn/blessed-contrib/issues/39"
this.options = options
this.options.dashboardMargin = this.options.dashboardMargin || 0
this.cellWidth = ((100 - this.options.dashboardMargin*2) / this.options.cols)
this.cellHeight = ((100 - this.options.dashboardMargin*2) / this.options.rows)
this.options = options
this.options.dashboardMargin = this.options.dashboardMargin || 0
this.cellWidth = ((100 - this.options.dashboardMargin*2) / this.options.cols)
this.cellHeight = ((100 - this.options.dashboardMargin*2) / this.options.rows)
}

Grid.prototype.set = function(row, col, rowSpan, colSpan, obj, opts) {

if (obj instanceof Grid) {
throw "Error: A Grid is not allowed to be nested inside another grid.\r\n" +
if (obj instanceof Grid) {
throw "Error: A Grid is not allowed to be nested inside another grid.\r\n" +
"Note: Release 2.0.0 has breaking changes. Please refer to the README or to https://github.com/yaronn/blessed-contrib/issues/39"
}

var top = row * this.cellHeight + this.options.dashboardMargin
var left = col * this.cellWidth + this.options.dashboardMargin

//var options = JSON.parse(JSON.stringify(opts));
var options = {}
options = utils.MergeRecursive(options, opts)
options.top = top + '%'
options.left = left + '%'
options.width = (this.cellWidth * colSpan - widgetSpacing) + "%"
options.height = (this.cellHeight * rowSpan - widgetSpacing) + "%"
if (!this.options.hideBorder)
options.border = {type: "line", fg: this.options.color || "cyan"}
var instance = obj(options)
this.options.screen.append(instance)
return instance
}

var top = row * this.cellHeight + this.options.dashboardMargin
var left = col * this.cellWidth + this.options.dashboardMargin

//var options = JSON.parse(JSON.stringify(opts));
var options = {}
options = utils.MergeRecursive(options, opts)
options.top = top + '%'
options.left = left + '%'
options.width = (this.cellWidth * colSpan - widgetSpacing) + "%"
options.height = (this.cellHeight * rowSpan - widgetSpacing) + "%"
if (!this.options.hideBorder)
options.border = {type: "line", fg: this.options.color || "cyan"}

var instance = obj(options)
this.options.screen.append(instance)
return instance
}

module.exports = Grid
module.exports = Grid
84 changes: 42 additions & 42 deletions lib/server-utils.js
@@ -1,68 +1,68 @@

"use strict";
var url = require('url')
, contrib = require('../index')
, blessed = require('blessed')

function OutputBuffer(options) {
this.isTTY = true
this.columns = options.cols
this.rows = options.rows
this.write = function(s) {
s = s.replace("\x1b8", "") //not clear from where in blessed this code comes from. It forces the terminal to clear and loose existing content.
options.res.write(s)
}
this.isTTY = true
this.columns = options.cols
this.rows = options.rows
this.write = function(s) {
s = s.replace("\x1b8", "") //not clear from where in blessed this code comes from. It forces the terminal to clear and loose existing content.
options.res.write(s)
}

this.on = function() {}
}

function InputBuffer(options) {
this.isTTY = true
this.isRaw = true
function InputBuffer() {
this.isTTY = true
this.isRaw = true

this.emit = function() {}

this.emit = function() {}

this.setRawMode = function() {}
this.resume = function() {}
this.pause = function() {}
this.setRawMode = function() {}
this.resume = function() {}
this.pause = function() {}

this.on = function() {}
this.on = function() {}
}

function serverError(req, res, err) {
setTimeout(function() {
if (!res.headersSent) res.writeHead(500, {'Content-Type': 'text/plain'});
res.write('\r\n\r\n'+err+'\r\n\r\n')
//restore cursor
res.end('\033[?25h')
}, 0)
return true
setTimeout(function() {
if (!res.headersSent) res.writeHead(500, {'Content-Type': 'text/plain'});
res.write('\r\n\r\n'+err+'\r\n\r\n')
//restore cursor
res.end('\u001b[?25h')
}, 0)

return true
}


function createScreen(req, res) {
var query = url.parse(req.url, true).query
var query = url.parse(req.url, true).query

var cols = query.cols || 250
var rows = query.rows || 50

var cols = query.cols || 250
var rows = query.rows || 50
if (cols<=35 || cols>=500 || rows<=5 || rows>=300) {
serverError(req, res, "cols must be bigger than 35 and rows must be bigger than 5")
return null
}

if (cols<=35 || cols>=500 || rows<=5 || rows>=300) {
serverError(req, res, "cols must be bigger than 35 and rows must be bigger than 5")
return null
}
res.writeHead(200, {'Content-Type': 'text/plain'});

res.writeHead(200, {'Content-Type': 'text/plain'});
var output = new contrib.OutputBuffer({res: res, cols: cols, rows: rows})
var input = new contrib.InputBuffer() //required to run under forever since it replaces stdin to non-tty
var program = blessed.program({output: output, input: input})

var output = new contrib.OutputBuffer({res: res, cols: cols, rows: rows})
var input = new contrib.InputBuffer() //required to run under forever since it replaces stdin to non-tty
var program = blessed.program({output: output, input: input})

if (query.terminal) program.terminal = query.terminal
if (query.isOSX) program.isOSXTerm = query.isOSX
if (query.isiTerm2) program.isiTerm2 = query.isiTerm2
if (query.terminal) program.terminal = query.terminal
if (query.isOSX) program.isOSXTerm = query.isOSX
if (query.isiTerm2) program.isiTerm2 = query.isiTerm2

var screen = blessed.screen({program: program});
return screen
var screen = blessed.screen({program: program});
return screen
}


Expand Down
56 changes: 29 additions & 27 deletions lib/utils.js
@@ -1,12 +1,16 @@
"use strict";
var x256 = require('x256')

/*
* Recursively merge properties of two objects
*/
function MergeRecursive(obj1, obj2) {

if (obj1==null) return obj2
if (obj2==null) return obj1
if (obj1==null) {
return obj2
}
if (obj2==null) {
return obj1
}

for (var p in obj2) {
try {
Expand All @@ -31,36 +35,34 @@ function MergeRecursive(obj1, obj2) {


function getTypeName(thing){
if(thing===null)return "[object Null]"; // special case
return Object.prototype.toString.call(thing);
if(thing===null)return "[object Null]"; // special case
return Object.prototype.toString.call(thing);
}

function abbreviateNumber(value) {
var newValue = value;
if (value >= 1000) {
var suffixes = ["", "k", "m", "b","t"];
var suffixNum = Math.floor( (""+value).length/3 );
var shortValue = '';
for (var precision = 2; precision >= 1; precision--) {
shortValue = parseFloat( (suffixNum != 0 ? (value / Math.pow(1000,suffixNum) ) : value).toPrecision(precision));
var dotLessShortValue = (shortValue + '').replace(/[^a-zA-Z 0-9]+/g,'');
if (dotLessShortValue.length <= 2) { break; }
}
if (shortValue % 1 != 0) shortNum = shortValue.toFixed(1);
newValue = shortValue+suffixes[suffixNum];
}
return newValue;
var newValue = value;
if (value >= 1000) {
var suffixes = ["", "k", "m", "b","t"];
var suffixNum = Math.floor( (""+value).length/3 );
var shortValue = '';
for (var precision = 2; precision >= 1; precision--) {
shortValue = parseFloat( (suffixNum != 0 ? (value / Math.pow(1000,suffixNum) ) : value).toPrecision(precision));
var dotLessShortValue = (shortValue + '').replace(/[^a-zA-Z 0-9]+/g,'');
if (dotLessShortValue.length <= 2) {
break;
}
}
newValue = shortValue+suffixes[suffixNum];
}
return newValue;
}

function getColorCode(color) {
if (Array.isArray(color) && color.length == 3)
{
return x256(color[0],color[1],color[2]);
}
else
{
return color;
}
if (Array.isArray(color) && color.length == 3) {
return x256(color[0],color[1],color[2]);
} else {
return color;
}
}

exports.MergeRecursive = MergeRecursive
Expand Down

0 comments on commit ed68bca

Please sign in to comment.