Socket
Socket
Sign inDemoInstall

ansi

Package Overview
Dependencies
0
Maintainers
2
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.0 to 0.1.1

examples/starwars.js

5

examples/imgcat/index.js

@@ -8,3 +8,4 @@ #!/usr/bin/env node

, imageFile = process.argv[2] || __dirname + '/yoshi.png'
, maxWidth = parseInt(process.argv[3]) || process.stdout.getWindowSize()[0]
, screenWidth = process.stdout.isTTY ? process.stdout.getWindowSize()[0] : Infinity
, maxWidth = parseInt(process.argv[3], 10) || screenWidth
, image = require('fs').readFileSync(imageFile)

@@ -18,3 +19,3 @@ , pixel = ' '

function draw () {
var width = maxWidth / pixel.length | 0
var width = maxWidth / pixel.length
, scaleW = img.width > width ? width / img.width : 1

@@ -21,0 +22,0 @@ , w = Math.floor(img.width * scaleW)

23

examples/progress/index.js

@@ -12,4 +12,4 @@ #!/usr/bin/env node

this.close = ']'
this.complete = '▬'
this.incomplete = '⋅'
this.complete = '█'
this.incomplete = '_'

@@ -51,8 +51,10 @@ // initial render

.eraseLine(2)
.fg.white()
.write(this.open)
.fg.grey()
.write(this.open)
.fg.white()
.bold()
.write(com)
.resetBold()
.write(inc)
.fg.grey()
.fg.white()
.write(this.close)

@@ -77,7 +79,12 @@ .fg.reset()

;(function tick () {
p.progress = p.progress + (Math.random() * 5)
p.cursor.eraseLine(2)
console.log('progress: ' + p.progress)
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)
})()

@@ -61,4 +61,2 @@

, inverse: 27
, foreground: 39
, background: 49
}

@@ -72,3 +70,2 @@

white: 37
, grey: 90
, black: 30

@@ -81,2 +78,11 @@ , blue: 34

, yellow: 33
, grey: 90
, brightBlack: 90
, brightRed: 91
, brightGreen: 92
, brightYellow: 93
, brightBlue: 94
, brightMagenta: 95
, brightCyan: 96
, brightWhite: 97
}

@@ -110,5 +116,17 @@

// the stream to use
this.stream = stream
// controls the foreground and background colors
this.fg = this.foreground = new Colorer(this, 0)
this.bg = this.background = new Colorer(this, 10)
// defaults
this.Bold = false
this.Italic = false
this.Underline = false
this.Inverse = false
// keep track of the number of "newlines" that get encountered
this.newlines = 0
emitNewlineEvents(stream)

@@ -118,42 +136,41 @@ stream.on('newline', function () {

}.bind(this))
// controls the foreground and background colors
this.fg = this.foreground = new Foreground(this)
this.bg = this.background = new Background(this)
}
exports.Cursor = Cursor
/**
* The `Foreground` class.
* Helper function that calls `write()` on the underlying Stream.
* Returns `this` instead of the write() return value to keep
* the chaining going.
*/
function Foreground (cursor) {
this.cursor = cursor
Cursor.prototype.write = function () {
this.stream.write.apply(this.stream, arguments)
return this
}
exports.Foreground = Foreground
/**
* The `Background` class.
* The `Colorer` class manages both the background and foreground colors.
*/
function Background (cursor) {
function Colorer (cursor, base) {
this.current = null
this.cursor = cursor
this.base = base
}
exports.Background = Background
exports.Colorer = Colorer
/**
* Helper function that calls `write()` on the underlying Stream.
* Returns `this` instead of the write() return value to keep
* the chaining going.
* Write an ANSI color code, ensuring that the same code doesn't get rewritten.
*/
Cursor.prototype.write = function () {
this.stream.write.apply(this.stream, arguments)
Colorer.prototype._setColorCode = function setColorCode (code) {
var c = String(code)
if (this.current === c) return
this.cursor.write(prefix + c + suffix)
this.current = c
return this
}
/**

@@ -181,10 +198,16 @@ * Set up the positional ANSI codes.

var name = style[0].toUpperCase() + style.substring(1)
, c = styles[style]
, r = reset[style]
Cursor.prototype[style] = function () {
this.write(prefix + styles[style] + suffix)
if (this[name]) return
this.write(prefix + c + suffix)
this[name] = true
return this
}
Cursor.prototype['reset'+name] = function () {
this.write(prefix + reset[style] + suffix)
Cursor.prototype['reset' + name] = function () {
if (!this[name]) return
this.write(prefix + r + suffix)
this[name] = false
return this

@@ -199,10 +222,6 @@ }

Object.keys(colors).forEach(function (color) {
Foreground.prototype[color] = function () {
this.cursor.write(prefix + colors[color] + suffix)
return this.cursor
}
var code = colors[color]
var bgCode = colors[color] + 10
Background.prototype[color] = function () {
this.cursor.write(prefix + bgCode + suffix)
Colorer.prototype[color] = function () {
this._setColorCode(this.base + code)
return this.cursor

@@ -230,4 +249,4 @@ }

Cursor.prototype.goto = function (x, y) {
x = ~~x
y = ~~y
x = x | 0
y = y | 0
this.write(prefix + y + ';' + x + 'H')

@@ -238,7 +257,7 @@ return this

/**
* Reset the foreground color.
* Resets the color.
*/
Foreground.prototype.reset = function () {
this.cursor.write(prefix + reset.foreground + suffix)
Colorer.prototype.reset = function () {
this._setColorCode(this.base + 39)
return this.cursor

@@ -248,11 +267,2 @@ }

/**
* Reset the background color.
*/
Background.prototype.reset = function () {
this.cursor.write(prefix + reset.background + suffix)
return this.cursor
}
/**
* Resets all ANSI formatting on the stream.

@@ -263,2 +273,8 @@ */

this.write(prefix + '0' + suffix)
this.bold = false
this.italic = false
this.underline = false
this.inverse = false
this.foreground.current = null
this.background.current = null
return this

@@ -272,4 +288,6 @@ }

Foreground.prototype.rgb = function (r, g, b) {
this.cursor.write(prefix + '38;5;' + rgb(r, g, b) + suffix)
Colorer.prototype.rgb = function (r, g, b) {
var base = this.base + 38
, code = rgb(r, g, b)
this._setColorCode(base + ';5;' + code)
return this.cursor

@@ -279,15 +297,5 @@ }

/**
* Sets the background color with the given RGB values.
* The closest match out of the 216 colors is picked.
* Same as `cursor.fg.rgb(r, g, b)`.
*/
Background.prototype.rgb = function (r, g, b) {
this.cursor.write(prefix + '48;5;' + rgb(r, g, b) + suffix)
return this.cursor
}
/**
* Same as `cursor.fg.rgb()`.
*/
Cursor.prototype.rgb = function (r, g, b) {

@@ -302,9 +310,8 @@ return this.foreground.rgb(r, g, b)

Foreground.prototype.hex = Background.prototype.hex = function (color) {
var rgb = hex(color)
return this.rgb(rgb[0], rgb[1], rgb[2])
Colorer.prototype.hex = function (color) {
return this.rgb.apply(this, hex(color))
}
/**
* Same as `cursor.fg.hex()`.
* Same as `cursor.fg.hex(color)`.
*/

@@ -316,2 +323,5 @@

// UTIL FUNCTIONS //
/**

@@ -355,10 +365,2 @@ * Translates a 255 RGB value to a 0-5 ANSI RGV value,

/**
* Gets a option value from a options objects, or a default value.
*/
function getopt (options, prop, fallback) {
return options.hasOwnProperty(prop) ? options[prop] : fallback
}
/**
* Turns an array-like object into a real array.

@@ -365,0 +367,0 @@ */

@@ -67,3 +67,3 @@

function processByte (stream, b) {
assert(typeof b, 'number')
assert.equal(typeof b, 'number')
if (b === NEWLINE) {

@@ -70,0 +70,0 @@ stream.emit('newline')

{ "name": "ansi"
, "description": "Advanced ANSI formatting tool for Node.js"
, "keywords": [ "ansi", "formatting", "cursor", "color", "terminal", "rgb", "256", "stream" ]
, "version": "0.1.0"
, "version": "0.1.1"
, "author": "Nathan Rajlich <nathan@tootallnate.net> (http://tootallnate.net)"

@@ -12,2 +12,3 @@ , "repository": { "type": "git", "url": "git://github.com/TooTallNate/ansi.js.git" }

, "imgcat": "./examples/imgcat/index.js"
, "starwars": "./examples/starwars.js"
}

@@ -21,3 +22,3 @@ , "scripts": {

}
, "engines": { "node": ">= 0.4.0 && < 0.9.0" }
, "engines": { "node": "*" }
}

@@ -94,1 +94,7 @@ ansi.js

SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
----------
Additionally:
* The `yoshi.png` file inside `examples/imgcat` is copyright to Nintendo, Inc.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc