From df9fa0de499c644b9afcadfe633fcdc0a50702ed Mon Sep 17 00:00:00 2001 From: Equim Date: Thu, 12 Oct 2017 01:28:22 +0800 Subject: [PATCH 1/3] lint code --- lc.js | 321 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 158 insertions(+), 163 deletions(-) diff --git a/lc.js b/lc.js index 8c28909..8a20e58 100755 --- a/lc.js +++ b/lc.js @@ -1,181 +1,176 @@ #!/usr/bin/env node - 'use strict'; -const fs = require('fs'); -const path = require('path'); - const yaml = require('js-yaml'); - const colors = require('colors'); +const fs = require('fs'); +const path = require('path'); const util = require('util'); -var ColorLS = class ColorLS { - - constructor(input, report) { - this.input = input || process.cwd(); - this.contents = fs.readdirSync(this.input); - this.count = { - folders: 0, - recognized_files: 0, - unrecognized_files: 0 - }; - this.report = report; - this.screen_width = process.stdout.columns; - this.max_widths = this.contents.map(x => x.length); - - this.init_icons(); - } - - ls() { - this.contents = this.chunkify(); - this.contents.forEach((chunk) => this.ls_line(chunk)); - process.stdout.write(util.format('\n')); - if (this.report) { - this.display_report(); - } - } - - init_icons() { - this.files = this.load_from_yaml('./files.yaml'); - this.file_aliases = this.load_from_yaml('./file_aliases.yaml', true); - this.folders = this.load_from_yaml('./folders.yaml'); - this.folder_aliases = this.load_from_yaml('./folder_aliases.yaml'); - - this.file_keys = Object.keys(this.files); - this.file_alias_keys = Object.keys(this.file_aliases); - this.folder_keys = Object.keys(this.folders); - this.folder_alias_keys = Object.keys(this.folder_aliases); - - this.all_files = this.file_keys.concat(this.file_alias_keys); - this.all_folders = this.folder_keys.concat(this.folder_alias_keys); - } - - chunkify() { - var chunk; - var chunk_size = this.contents.length; - while (!(this.in_line(chunk_size) || chunk_size <= 1)) { - chunk_size -= 1; - chunk = this.get_chunk(chunk_size); - } - return chunk || [this.contents]; - } - - get_chunk(chunk_size) { - var chunk = []; - var i = 0; - var currentChunk = []; - while (i < this.contents.length) { - currentChunk.push(this.contents[i++]); - if (currentChunk.length === chunk_size) { - chunk.push(currentChunk); - currentChunk = []; - } - } - while (currentChunk.length < chunk_size) { - currentChunk.push(''); - } - chunk.push(currentChunk); - var transposed = chunk[0].map(function(col, i) { - return chunk.map(function(row) { - return row[i]; - }); - }); - this.max_widths = transposed.map(row => Math.max(... row.map(s => s.length))); - return chunk; - } - - in_line(chunk_size) { - return !(this.max_widths.reduce((a, b) => a + b, 0) + 6 * chunk_size > this.screen_width); - } - - display_report() { - const len = this.contents.reduce((a, b) => a.concat(b), []).length; - process.stdout.write(util.format(`\n Found ${len} contents in directory `.white)); - - process.stdout.write(util.format(this.input.blue)); - - process.stdout.write(util.format(`\n\n\tFolders\t\t\t: ${this.count.folders}`.white)); - process.stdout.write(util.format(`\n\tRecognized files\t: ${this.count.recognized_files}`.white)); - process.stdout.write(util.format(`\n\tUnrecognized files\t: ${this.count.unrecognized_files}`.white)); - process.stdout.write(util.format('\n\n')); - } - - fetch_string(content, key, color, increment) { - this.count[increment] += 1; - var value = increment === 'folders' ? this.folders[key] : this.files[key]; - // todo -- port this line - // how it works for my future self: - // - value.gsub(/\\u[\da-f]{4}/i) matches and replaces all strings that look like unicode characters - // - it does the replacement by running each match through the function m -> m[-4..-1.to_i(16)].pack('U') - // this last part needs to be disected still, but it seems to make enough sense. - // logo = value.gsub(/\\u[\da-f]{4}/i) { |m| [m[-4..-1].to_i(16)].pack('U') } - return `${value} ${content}`[color]; - } - - load_from_yaml(filename, aliases) { - const location = path.join(__dirname, filename); - var loaded = yaml.safeLoad(fs.readFileSync(location, 'utf8')); - return loaded; - } - - ls_line(chunk) { - process.stdout.write(util.format('\n')); - for (var i = 0; i < chunk.length; ++i) { - var content = chunk[i]; - if (content.length === 0) { - break; - } - var opt = this.options(content); - var fetch = this.fetch_string(content, opt[0], opt[1], opt[2]); - process.stdout.write(util.format(fetch)); - var isFile = fs.existsSync(`${this.input}/${content}`) && - fs.lstatSync(`${this.input}/${content}`).isDirectory(); - process.stdout.write(util.format(isFile ? '/ '.blue : ' ')); - var space = new Array(this.max_widths[i] - content.length + 2).join(' '); - process.stdout.write(util.format(space)); - } - } - - options(content) { - var key; - if (fs.existsSync(`${this.input}/${content}`) && - fs.lstatSync(`${this.input}/${content}`).isDirectory()) { - key = content; - if (this.all_folders.indexOf(key) === -1) { - return ['folder', 'blue', 'folders']; - } - if (this.folder_keys.indexOf(key) === -1) { - key = this.folder_aliases[key]; - } - return [key, 'blue', 'folders']; - } - var split = content.split('.'); - key = split[split.length - 1].toLowerCase(); - if (this.all_files.indexOf(key) === -1) { - return ['file', 'yellow', 'unrecognized_files']; - } - if (this.file_keys.indexOf(key) === -1) { - key = this.file_aliases[key]; - } - return [key, 'green', 'recognized_files']; - } - +class ColorLS { + constructor(input, report) { + this.input = input || process.cwd(); + this.contents = fs.readdirSync(this.input); + this.count = { + folders: 0, + recognized_files: 0, + unrecognized_files: 0, + }; + this.report = report; + this.screen_width = process.stdout.columns; + this.max_widths = this.contents.map(x => x.length); + + this.init_icons(); + } + + ls() { + this.contents = this.chunkify(); + this.contents.forEach((chunk) => this.ls_line(chunk)); + process.stdout.write(util.format('\n')); + if (this.report) { + this.display_report(); + } + } + + init_icons() { + this.files = this.load_from_yaml('./files.yaml'); + this.file_aliases = this.load_from_yaml('./file_aliases.yaml', true); + this.folders = this.load_from_yaml('./folders.yaml'); + this.folder_aliases = this.load_from_yaml('./folder_aliases.yaml'); + + this.file_keys = Object.keys(this.files); + this.file_alias_keys = Object.keys(this.file_aliases); + this.folder_keys = Object.keys(this.folders); + this.folder_alias_keys = Object.keys(this.folder_aliases); + + this.all_files = this.file_keys.concat(this.file_alias_keys); + this.all_folders = this.folder_keys.concat(this.folder_alias_keys); + } + + chunkify() { + let chunk; + let chunk_size = this.contents.length; + while (!(this.in_line(chunk_size) || chunk_size <= 1)) { + chunk_size -= 1; + chunk = this.get_chunk(chunk_size); + } + return chunk || [this.contents]; + } + + get_chunk(chunk_size) { + let chunk = []; + let i = 0; + let currentChunk = []; + while (i < this.contents.length) { + currentChunk.push(this.contents[i++]); + if (currentChunk.length === chunk_size) { + chunk.push(currentChunk); + currentChunk = []; + } + } + while (currentChunk.length < chunk_size) { + currentChunk.push(''); + } + chunk.push(currentChunk); + let transposed = chunk[0].map(function (col, i) { + return chunk.map(function (row) { + return row[i]; + }); + }); + this.max_widths = transposed.map(row => Math.max(...row.map(s => s.length))); + return chunk; + } + + in_line(chunk_size) { + return !(this.max_widths.reduce((a, b) => a + b, 0) + 6 * chunk_size > this.screen_width); + } + + display_report() { + const len = this.contents.reduce((a, b) => a.concat(b), []).length; + process.stdout.write(util.format(`\n Found ${len} contents in directory `.white)); + + process.stdout.write(util.format(this.input.blue)); + + process.stdout.write(util.format(`\n\n\tFolders\t\t\t: ${this.count.folders}`.white)); + process.stdout.write(util.format(`\n\tRecognized files\t: ${this.count.recognized_files}`.white)); + process.stdout.write(util.format(`\n\tUnrecognized files\t: ${this.count.unrecognized_files}`.white)); + process.stdout.write(util.format('\n\n')); + } + + fetch_string(content, key, color, increment) { + this.count[increment] += 1; + let value = increment === 'folders' ? this.folders[key] : this.files[key]; + // todo -- port this line + // how it works for my future self: + // - value.gsub(/\\u[\da-f]{4}/i) matches and replaces all strings that look like unicode characters + // - it does the replacement by running each match through the function m -> m[-4..-1.to_i(16)].pack('U') + // this last part needs to be disected still, but it seems to make enough sense. + // logo = value.gsub(/\\u[\da-f]{4}/i) { |m| [m[-4..-1].to_i(16)].pack('U') } + return `${value} ${content}`[color]; + } + + load_from_yaml(filename, aliases) { + const location = path.join(__dirname, filename); + let loaded = yaml.safeLoad(fs.readFileSync(location, 'utf8')); + return loaded; + } + + ls_line(chunk) { + process.stdout.write(util.format('\n')); + for (let i = 0; i < chunk.length; ++i) { + let content = chunk[i]; + if (content.length === 0) { + break; + } + let opt = this.options(content); + let fetch = this.fetch_string(content, opt[0], opt[1], opt[2]); + process.stdout.write(util.format(fetch)); + let isFile = fs.existsSync(`${this.input}/${content}`) && + fs.lstatSync(`${this.input}/${content}`).isDirectory(); + process.stdout.write(util.format(isFile ? '/ '.blue : ' ')); + let space = new Array(this.max_widths[i] - content.length + 2).join(' '); + process.stdout.write(util.format(space)); + } + } + + options(content) { + let key; + if (fs.existsSync(`${this.input}/${content}`) && + fs.lstatSync(`${this.input}/${content}`).isDirectory()) { + key = content; + if (this.all_folders.indexOf(key) === -1) { + return ['folder', 'blue', 'folders']; + } + if (this.folder_keys.indexOf(key) === -1) { + key = this.folder_aliases[key]; + } + return [key, 'blue', 'folders']; + } + let split = content.split('.'); + key = split[split.length - 1].toLowerCase(); + if (this.all_files.indexOf(key) === -1) { + return ['file', 'yellow', 'unrecognized_files']; + } + if (this.file_keys.indexOf(key) === -1) { + key = this.file_aliases[key]; + } + return [key, 'green', 'recognized_files']; + } }; -var args = process.argv.slice(2); +let args = process.argv.slice(2); -var report = args.indexOf('--report') !== -1 || args.indexOf('-r') !== -1; +let report = args.indexOf('--report') !== -1 || args.indexOf('-r') !== -1; -var filtered = args.filter((arg) => !arg.startsWith('-')); +let filtered = args.filter((arg) => !arg.startsWith('-')); if (filtered.length === 0) { - new ColorLS(null, report).ls(); + new ColorLS(null, report).ls(); } else { - args.forEach((path) => { - new ColorLS(path, report).ls(); - }); + args.forEach((path) => { + new ColorLS(path, report).ls(); + }); } From 81e94f504b84f863da29f58bd454e0efc2b6d700 Mon Sep 17 00:00:00 2001 From: Equim Date: Thu, 12 Oct 2017 01:30:24 +0800 Subject: [PATCH 2/3] use chalk, drop colors --- lc.js | 18 +++++++-------- package.json | 2 +- yarn.lock | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 yarn.lock diff --git a/lc.js b/lc.js index 8a20e58..27a058a 100755 --- a/lc.js +++ b/lc.js @@ -2,7 +2,7 @@ 'use strict'; const yaml = require('js-yaml'); -const colors = require('colors'); +const chalk = require('chalk'); const fs = require('fs'); const path = require('path'); @@ -88,13 +88,13 @@ class ColorLS { display_report() { const len = this.contents.reduce((a, b) => a.concat(b), []).length; - process.stdout.write(util.format(`\n Found ${len} contents in directory `.white)); + process.stdout.write(util.format(chalk.white(`\n Found ${len} contents in directory `))); - process.stdout.write(util.format(this.input.blue)); + process.stdout.write(util.format(chalk.blue(this.input))); - process.stdout.write(util.format(`\n\n\tFolders\t\t\t: ${this.count.folders}`.white)); - process.stdout.write(util.format(`\n\tRecognized files\t: ${this.count.recognized_files}`.white)); - process.stdout.write(util.format(`\n\tUnrecognized files\t: ${this.count.unrecognized_files}`.white)); + process.stdout.write(util.format(chalk.white(`\n\n\tFolders\t\t\t: ${this.count.folders}`))); + process.stdout.write(util.format(chalk.white(`\n\tRecognized files\t: ${this.count.recognized_files}`))); + process.stdout.write(util.format(chalk.white(`\n\tUnrecognized files\t: ${this.count.unrecognized_files}`))); process.stdout.write(util.format('\n\n')); } @@ -107,7 +107,7 @@ class ColorLS { // - it does the replacement by running each match through the function m -> m[-4..-1.to_i(16)].pack('U') // this last part needs to be disected still, but it seems to make enough sense. // logo = value.gsub(/\\u[\da-f]{4}/i) { |m| [m[-4..-1].to_i(16)].pack('U') } - return `${value} ${content}`[color]; + return chalk[color](`${value} ${content}`); } load_from_yaml(filename, aliases) { @@ -128,7 +128,7 @@ class ColorLS { process.stdout.write(util.format(fetch)); let isFile = fs.existsSync(`${this.input}/${content}`) && fs.lstatSync(`${this.input}/${content}`).isDirectory(); - process.stdout.write(util.format(isFile ? '/ '.blue : ' ')); + process.stdout.write(util.format(isFile ? chalk.blue('/ ') : ' ')); let space = new Array(this.max_widths[i] - content.length + 2).join(' '); process.stdout.write(util.format(space)); } @@ -172,5 +172,3 @@ if (filtered.length === 0) { new ColorLS(path, report).ls(); }); } - - diff --git a/package.json b/package.json index 6f84a49..da6b3df 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ }, "homepage": "https://github.com/sl/colorls-js#readme", "dependencies": { - "colors": "^1.1.2", + "chalk": "^2.1.0", "js-yaml": "^3.8.4" } } diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..fde7726 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,62 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +ansi-styles@^3.1.0: + version "3.2.0" + resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.0.tgz#c159b8d5be0f9e5a6f346dab94f16ce022161b88" + dependencies: + color-convert "^1.9.0" + +argparse@^1.0.7: + version "1.0.9" + resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.9.tgz#73d83bc263f86e97f8cc4f6bae1b0e90a7d22c86" + dependencies: + sprintf-js "~1.0.2" + +chalk@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.1.0.tgz#ac5becf14fa21b99c6c92ca7a7d7cfd5b17e743e" + dependencies: + ansi-styles "^3.1.0" + escape-string-regexp "^1.0.5" + supports-color "^4.0.0" + +color-convert@^1.9.0: + version "1.9.0" + resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.0.tgz#1accf97dd739b983bf994d56fec8f95853641b7a" + dependencies: + color-name "^1.1.1" + +color-name@^1.1.1: + version "1.1.3" + resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25" + +escape-string-regexp@^1.0.5: + version "1.0.5" + resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" + +esprima@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.0.tgz#4499eddcd1110e0b218bacf2fa7f7f59f55ca804" + +has-flag@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-2.0.0.tgz#e8207af1cc7b30d446cc70b734b5e8be18f88d51" + +js-yaml@^3.8.4: + version "3.10.0" + resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.10.0.tgz#2e78441646bd4682e963f22b6e92823c309c62dc" + dependencies: + argparse "^1.0.7" + esprima "^4.0.0" + +sprintf-js@~1.0.2: + version "1.0.3" + resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" + +supports-color@^4.0.0: + version "4.4.0" + resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-4.4.0.tgz#883f7ddabc165142b2a61427f3352ded195d1a3e" + dependencies: + has-flag "^2.0.0" From 5fbb9f1909b09d6459b19cb46f88ceb53f37f5b4 Mon Sep 17 00:00:00 2001 From: Equim Date: Thu, 12 Oct 2017 01:31:07 +0800 Subject: [PATCH 3/3] add .DS_Store to .gitignore --- .DS_Store | Bin 6148 -> 0 bytes .gitignore | 1 + 2 files changed, 1 insertion(+) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 32db4fb3c465dd03e14ac2405cd8bd9eaa34110e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK!A`%a%p|jKIysVU@r67^7ni9Mp2lg_4)@d6iXYMTUNPjm3ORLXCN~tb<;`G zamUx{Df9!+*cVbp3)}TB!yxNb_D*D+x?voQzl7ghg|RQQj-14PqSiTg(*fkFje3>p zblPmTYNFL>%xYrVZr5w#uz56_S=RnRVQGD0DvWMD*+pO3Cs}&ZG*W+m;vEB6;P*g(_(O)4t` p1>