Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add config file and img res

  • Loading branch information...
commit e82cc483eca7639f54211f0915900ba4465d4113 1 parent 6fc8282
@switer authored
View
31 bin/imaging
@@ -3,12 +3,14 @@ var childProcess = require('child_process'),
phantomjs = require('phantomjs'),
program = require('commander'),
colors = require('colors'),
- defaultWidth = 40,
+ config = require('../config.js'),
binPath = phantomjs.path;
program
.version('0.0.1')
.usage('<path> [options]')
+ .option('-c, --char <charater>', 'set the char as the pixels of image')
+ .option('-l, --left <left>', 'set the left position of image')
.option('-w, --width <width>', 'set the width to render the image in terminal');
program.parse(process.argv);
@@ -34,16 +36,33 @@ if (!imgUri) {
}
var childArgs = [
- __dirname + '/../run.js',
- encodeURIComponent(imgUri)
-]
+ __dirname + '/../run.js',
+ encodeURIComponent(imgUri)
+ ],
+ optParams = {};
+
if (program.width) {
- childArgs.push(program.width);
+ optParams.width = program.width;
} else {
- childArgs.push(defaultWidth);
+ optParams.width = config.defaultWidth;
}
+
+if (program.char) {
+ optParams.char = program.char;
+} else {
+ optParams.char = config.char.default;
+}
+
+if (program.left) {
+ optParams.left = program.left;
+} else {
+ optParams.left = config.defaultLeft;
+}
+
+childArgs.push(JSON.stringify(optParams));
childArgs.push(encodeURIComponent(__dirname));
childArgs.push('../');
+
childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
if (err) console.log(err);
else console.log(stdout);
View
11 config.js
@@ -0,0 +1,11 @@
+module.exports = {
+ "defaultWidth": 40,
+ "defaultLeft": 0,
+ "char": {
+ "default": "",
+ "square": "",
+ "circle": "",
+ "rectangle": ""
+ }
+
+}
View
24 imaging.js
@@ -4,29 +4,41 @@ var childProcess = require('child_process'),
program = require('commander'),
colors = require('colors'),
binPath = phantomjs.path,
- config = {
- defaultWidth : 40
- };
+ config = require('./config.js');
function _fixedImageUrl(url) {
if (url.match(/http[s]*\:\/\//)) return url;
else if (url.match(/[a-zA-Z]\:/) || url.match(/^\//)) return url;
else return process.cwd().replace('/$','') + '/' + url;
}
-
function _run (imgUri, options, callback) {
+ var optParams = {};
+
var childArgs = [
__dirname + '/run.js',
encodeURIComponent(imgUri)
]
if (options.width) {
- childArgs.push(options.width);
+ optParams.width = options.width;
+ } else {
+ optParams.width = config.defaultWidth;
+ }
+ if (options.char) {
+ optParams.char = options.char;
} else {
- childArgs.push(config.defaultWidth);
+ optParams.char = config.char.default;
}
+ if (options.left) {
+ optParams.left = options.left;
+ } else {
+ optParams.left = config.defaultLeft;
+ }
+
+ childArgs.push(JSON.stringify(optParams));
childArgs.push(encodeURIComponent(__dirname));
childArgs.push('./');
+
childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
if (err) console.log(err);
// else console.log(stdout);
View
BIN  res/image/1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
38 run.js
@@ -14,11 +14,11 @@ fs.changeWorkingDirectory(dirname);
var page = require('webpage').create(),
color = require(dirname.replace(/\/$/) + '/' + relativePath + '/node_modules/colors/colors.js'),
+ config = require(dirname.replace(/\/$/) + '/' + relativePath + '/config.js'),
pix = '',
- // pix = '●',
- // pix = 'o',
- // pix = '█',
- width = 40;
+ width = 40,
+ options = {};
+
var colorMap = {
/*normal*/
@@ -55,10 +55,20 @@ var colorMap = {
var params = argsParse(args);
function argsParse (args) {
- width = args[2];
+ options = JSON.parse(args[2]);
+
+ var charType = options.char;
+
+ width = options.width;
+
+ if (config.char[charType]) pix = config.char[charType];
+ else if (charType.length === 1) pix = charType;
+ else pix = config.char.default;
+
return {
img: args[1],
- width: args[2]
+ width: width,
+ left: parseInt(options.left)
}
}
function canvasProcess (height) {
@@ -88,13 +98,23 @@ function canvasProcess (height) {
draw(colors, function () {
setTimeout(function () {
phantom.exit();
- }, 500);
+ }, 50);
});
return pixData;
}
+function repeat (str, times) {
+ var index = 0,
+ ctn = '';
+ while(index < times) {
+ ctn += str;
+ index ++;
+ }
+ return ctn;
+}
function draw (colors, callback) {
- var line = '';
+ var line = repeat(' ', params.left);
for (var i =0; i < colors.length; i ++) {
+
if (i % width !== 0) {
if (typeof(colors[i]) === 'string') {
line += pix[colors[i]];
@@ -103,7 +123,7 @@ function draw (colors, callback) {
}
} else {
console.log(line);
- line = '';
+ line = repeat(' ', params.left);
}
}
callback && callback();
View
18 test.js
@@ -4,20 +4,8 @@
'use strict';
var args = require('system').args,
- fs = require('fs'),
- cwd = fs.workingDirectory;
+ fs = require('fs');
-var dirname = decodeURIComponent(args[3]);
-fs.changeWorkingDirectory(dirname);
-
-console.log(dirname);
-
-var page = require('webpage').create(),
- colors = require(dirname.replace(/\/$/) + '/../node_modules/colors/colors.js'),
- pix = '',
- // pix = '●',
- // pix = 'o',
- // pix = '█',
- width = 40;
-console.log(colors)
+ console.log(args);
+ console.log(JSON.parse(args[2]).width );
phantom.exit();
View
7 test/test2.js
@@ -1,7 +1,4 @@
-var imaging = require('imaging');
-imaging.draw('images/1.jpg', function (resp) {
- console.log(resp);
-});
-imaging.draw('images/1.jpg', {width:50}, function (resp) {
+var imaging = require('../imaging.js');
+imaging.draw('../res/image/1.png' , {width:40, char: 'M', left: 20}, function (resp) {
console.log(resp);
});
Please sign in to comment.
Something went wrong with that request. Please try again.