Permalink
Browse files

First commit!

  • Loading branch information...
0 parents commit 1216e658ff2db701874768c9d026248e9c6e5e46 @jfhbrook committed Oct 28, 2011
Showing with 216 additions and 0 deletions.
  1. +73 −0 README.md
  2. +39 −0 examples/blacksmith.js
  3. +54 −0 examples/log.js
  4. +50 −0 lib/ansimator.js
73 README.md
@@ -0,0 +1,73 @@
+# ansimator
+
+Plop some simple animations onto the terminal with ANSI codes!
+
+## Example:
+
+ var ansimate = require("ansimator")(process);
+
+ var frames = [
+
+ [
+ " ▜▘ ",
+ " ▐ ☻ ",
+ " ▚▄█████▚ ",
+ " ▐███ ▐ ",
+ " ▜▛ ▐███ ",
+ " ▔▔ ▐ ▐ ",
+ " ▟ ▟ ",
+ " " ].join("\n"),
+
+ [
+ " ",
+ "*tink* ☻ ",
+ " ▖ ▄█████▚ ",
+ " ▛▀▀ ▐███ ▐ ",
+ " ▜▛ ▐███ ",
+ " ▔▔ ▐ ▐ ",
+ " ▟ ▟ ",
+ " " ].join("\n")
+
+
+ ];
+
+ var metalworker = ansimate(frames).move(0, 2, function (metalworker) {
+ setInterval(metalworker.next, 800);
+ });
+
+ metalworker.charm.on("^C", function () {
+ metalworker.charm.display("reset");
+ metalworker.charm.destroy();
+ process.exit();
+ });
+
+## Install:
+
+ npm install ansimator
+
+## Usage:
+
+### ansimator(...)
+
+This function takes the same arguments as [charm](https://github.com/substack/node-charm) and returns an instance of `ansimate`.
+
+## ansimate(frames)
+
+Frames consists of a list of strings, where each string represents a single
+unicode image.
+
+### ansimate(frames) Properties and Methods:
+
+* `position(x, y)`: Position the top-left cursor position as in charm.
+
+* `move(x, y, cb)`: Move the top-left cursor position as in charm. Takes a callback argument which fires after polling is complete.
+
+* `next()`: Draws the next frame.
+
+* `frames`: Feel free to modify the frames at runtime.
+
+* `charm`: Access the associated instance of charm directly.
+
+## License:
+
+MIT/X11.
39 examples/blacksmith.js
@@ -0,0 +1,39 @@
+var ansimator = require("../lib/ansimator")(process);
+
+
+// You should be able to pass frames into this thing and have them get drawn
+// wherever.
+var frames = [
+
+ [
+ " ▜▘ ",
+ " ▐ ☻ ",
+ " ▚▄█████▚ ",
+ " ▐███ ▐ ",
+ " ▜▛ ▐███ ",
+ " ▔▔ ▐ ▐ ",
+ " ▟ ▟ ",
+ " " ].join("\n"),
+
+ [
+ " ",
+ "*tink* ☻ ",
+ " ▖ ▄█████▚ ",
+ " ▛▀▀ ▐███ ▐ ",
+ " ▜▛ ▐███ ",
+ " ▔▔ ▐ ▐ ",
+ " ▟ ▟ ",
+ " " ].join("\n")
+
+
+];
+
+var metalworker = ansimator(frames).move(0, 2, function (metalworker) {
+ setInterval(metalworker.next, 800);
+});
+
+metalworker.charm.on("^C", function () {
+ metalworker.charm.display("reset");
+ metalworker.charm.destroy();
+ process.exit();
+});
54 examples/log.js
@@ -0,0 +1,54 @@
+var ansimator = require("../lib/ansimator")(process);
+var EE = require("events").EventEmitter;
+
+var width = 40,
+ height = 8;
+
+var frames = [
+ ""
+];
+
+var logger = new EE();
+
+
+var lineBreak = function (frame) {
+ return frame.split("\n").map(function (l) {
+ var c = width;
+ while( l[c] !== " ") {
+ c--;
+ }
+
+ return l.slice(0, c-1) + "\n" + l.slice(c);
+ }).join("\n");
+
+}
+
+
+var clipHeight = function (frame) {
+ var xs;
+
+ if (frame.length === 0) {
+ xs = [ xs ];
+ } else {
+ xs = frame.split("\n").concat(data);
+ }
+ return xs.slice(Math.max(0, xs.length - height)).join("\n");
+}
+
+
+var logbox = ansimator(frames).move(0, 2, function (logbox) {
+ logger.on("data", function (data) {
+ frames[0] = clipHeight(frames[0]);
+ logbox.next();
+ });
+});
+
+logbox.charm.on("^C", function () {
+ logbox.charm.display("reset");
+ logbox.charm.destroy();
+ process.exit();
+});
+
+setInterval(function () {
+ logger.emit("data", "THE BEST");
+}, 500);
50 lib/ansimator.js
@@ -0,0 +1,50 @@
+var charmer = require("charm");
+
+module.exports = function () {
+ var charm = charmer.apply(charmer, arguments);
+
+ return function (frames) {
+ var x = 0,
+ y = 0,
+ f = 0;
+
+ charm.write("\n").up(1);
+
+ var ansimator = {
+ frames: frames,
+ position: function (i,j) {
+ x = i;
+ y = j;
+ charm.position(x,y);
+ return ansimator;
+ },
+ move: function (i, j, cb) {
+ // fire/forget. This is a race condition.
+ charm.position(function (m, n) {
+ x = m + i;
+ y = n + j;
+ cb(ansimator);
+ });
+
+ return ansimator;
+ },
+ next: function () {
+ charm.push(true);
+
+ charm.position(x,y);
+
+ ansimator.frames[f].split("\n").forEach(function (l) {
+ charm.column(x).write(l+"\n");
+ });
+
+ f = (f + 1) % frames.length;
+
+ charm.pop(true);
+ },
+ charm: charm
+ };
+
+ return ansimator;
+
+ };
+}

0 comments on commit 1216e65

Please sign in to comment.