Permalink
Browse files

Ansi gets annoyed when lacking its examples

  • Loading branch information...
1 parent 0cf98d9 commit a0d858930c1afd00eff38c03a113437dc192411c @isaacs isaacs committed Jun 6, 2012
View
16 node_modules/ansi/examples/beep/index.js
@@ -0,0 +1,16 @@
+#!/usr/bin/env node
+
+/**
+ * Invokes the terminal "beep" sound once per second on every exact second.
+ */
+
+process.title = 'beep'
+
+var cursor = require('../../')(process.stdout)
+
+function beep () {
+ cursor.beep()
+ setTimeout(beep, 1000 - (new Date()).getMilliseconds())
+}
+
+setTimeout(beep, 1000 - (new Date()).getMilliseconds())
View
15 node_modules/ansi/examples/clear/index.js
@@ -0,0 +1,15 @@
+#!/usr/bin/env node
+
+/**
+ * Like GNU ncurses "clear" command.
+ * https://github.com/mscdex/node-ncurses/blob/master/deps/ncurses/progs/clear.c
+ */
+
+process.title = 'clear'
+
+function lf () { return '\n' }
+
+require('../../')(process.stdout)
+ .write(Array.apply(null, Array(process.stdout.getWindowSize()[1])).map(lf).join(''))
+ .eraseData(2)
+ .goto(1, 1)
View
24 node_modules/ansi/examples/cursorPosition.js
@@ -0,0 +1,24 @@
+#!/usr/bin/env node
+
+var tty = require('tty')
+var cursor = require('../')(process.stdout)
+
+// listen for the queryPosition report on stdin
+process.stdin.resume()
+tty.setRawMode(true)
+
+process.stdin.once('data', function (b) {
+ var match = /\[(\d+)\;(\d+)R$/.exec(b.toString())
+ if (match) {
+ var xy = match.slice(1, 3).reverse().map(Number)
+ console.error(xy)
+ }
+
+ // cleanup and close stdin
+ tty.setRawMode(false)
+ process.stdin.pause()
+})
+
+
+// send the query position request code to stdout
+cursor.queryPosition()
View
50 node_modules/ansi/examples/imgcat/index.js
@@ -0,0 +1,50 @@
+#!/usr/bin/env node
+
+process.title = 'imgcat'
+
+var ansi = require('../../')
+ , cursor = ansi(process.stdout, { enabled: true })
+ , tty = require('tty')
+ , Canvas = require('canvas')
+ , imageFile = process.argv[2] || __dirname + '/yoshi.png'
+ , screenWidth = process.stdout.isTTY ? process.stdout.getWindowSize()[0] : Infinity
+ , maxWidth = parseInt(process.argv[3], 10) || screenWidth
+ , image = require('fs').readFileSync(imageFile)
+ , pixel = ' '
+ , alphaThreshold = 0
+
+var img = new Canvas.Image();
+img.src = image;
+
+function draw () {
+ var width = maxWidth / pixel.length
+ , scaleW = img.width > width ? width / img.width : 1
+ , w = Math.floor(img.width * scaleW)
+ , h = Math.floor(img.height * scaleW);
+
+ var canvas = new Canvas(w, h)
+ , ctx = canvas.getContext('2d');
+
+ ctx.drawImage(img, 0, 0, w, h);
+
+ var data = ctx.getImageData(0, 0, w, h).data;
+
+ for (var i=0, l=data.length; i<l; i+=4) {
+ var r = data[i]
+ , g = data[i+1]
+ , b = data[i+2]
+ , alpha = data[i+3];
+ if (alpha > alphaThreshold) {
+ cursor.bg.rgb(r, g, b);
+ } else {
+ cursor.bg.reset();
+ }
+ process.stdout.write(pixel);
+ if ((i/4|0) % w === (w-1)) {
+ cursor.bg.reset();
+ process.stdout.write('\n');
+ }
+ }
+}
+
+draw();
View
BIN node_modules/ansi/examples/imgcat/yoshi.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
87 node_modules/ansi/examples/progress/index.js
@@ -0,0 +1,87 @@
+#!/usr/bin/env node
+
+var assert = require('assert')
+ , ansi = require('../../')
+
+function Progress (stream, width) {
+ this.cursor = ansi(stream)
+ this.delta = this.cursor.newlines
+ this.width = width | 0 || 10
+ this.open = '['
+ this.close = ']'
+ this.complete = ''
+ this.incomplete = '_'
+
+ // initial render
+ this.progress = 0
+}
+
+Object.defineProperty(Progress.prototype, 'progress', {
+ get: get
+ , set: set
+ , configurable: true
+ , enumerable: true
+})
+
+function get () {
+ return this._progress
+}
+
+function set (v) {
+ this._progress = Math.max(0, Math.min(v, 100))
+
+ var w = this.width - this.complete.length - this.incomplete.length
+ , n = w * (this._progress / 100) | 0
+ , i = w - n
+ , com = c(this.complete, n)
+ , inc = c(this.incomplete, i)
+ , delta = this.cursor.newlines - this.delta
+
+ assert.equal(com.length + inc.length, w)
+
+ if (delta > 0) {
+ this.cursor.up(delta)
+ this.delta = this.cursor.newlines
+ }
+
+ this.cursor
+ .horizontalAbsolute(0)
+ .eraseLine(2)
+ .fg.white()
+ .write(this.open)
+ .fg.grey()
+ .bold()
+ .write(com)
+ .resetBold()
+ .write(inc)
+ .fg.white()
+ .write(this.close)
+ .fg.reset()
+ .write('\n')
+}
+
+function c (char, length) {
+ return Array.apply(null, Array(length)).map(function () {
+ return char
+ }).join('')
+}
+
+
+
+
+// Usage
+var width = parseInt(process.argv[2], 10) || process.stdout.getWindowSize()[0] / 2
+ , p = new Progress(process.stdout, width)
+
+;(function tick () {
+ p.progress += Math.random() * 5
+ p.cursor
+ .eraseLine(2)
+ .write('Progress: ')
+ .bold().write(p.progress.toFixed(2))
+ .write('%')
+ .resetBold()
+ .write('\n')
+ if (p.progress < 100)
+ setTimeout(tick, 100)
+})()
View
39 node_modules/ansi/examples/starwars.js
@@ -0,0 +1,39 @@
+#!/usr/bin/env node
+
+/**
+ * A little script to play the ASCII Star Wars, but with a hidden cursor,
+ * since over `telnet(1)` the cursor remains visible which is annoying.
+ */
+
+process.title = 'starwars'
+
+var net = require('net')
+ , cursor = require('../')(process.stdout)
+
+// enable "raw mode" so that keystrokes aren't visible
+process.stdin.resume()
+if (process.stdin.setRawMode) {
+ process.stdin.setRawMode(true)
+} else {
+ require('tty').setRawMode(true)
+}
+
+// connect to the ASCII Star Wars server
+var socket = net.connect(23, 'towel.blinkenlights.nl')
+
+socket.on('connect', function () {
+ cursor.hide()
+ socket.pipe(process.stdout)
+})
+
+process.stdin.on('data', function (data) {
+ if (data.toString() === '\u0003') {
+ // Ctrl+C; a.k.a SIGINT
+ socket.destroy()
+ process.stdin.pause()
+ }
+})
+
+process.on('exit', function () {
+ cursor.show().write('\n')
+})
View
5 node_modules/ansi/package.json
@@ -49,5 +49,8 @@
"_npmVersion": "1.1.24",
"_nodeVersion": "v0.7.10-pre",
"_defaultsLoaded": true,
- "_from": "ansi"
+ "dist": {
+ "shasum": "6139b23459bcd74b04572cf56b36102983d0a7d4"
+ },
+ "_from": "ansi@~0.1.2"
}

0 comments on commit a0d8589

Please sign in to comment.