Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Strip ANSI characters when fitting terminal width
Browse files Browse the repository at this point in the history
  • Loading branch information
HaraldNordgren committed Mar 26, 2018
1 parent 836525b commit f372ce6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/commands/releases/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const cli = require('heroku-cli-util')
const co = require('co')
const stripAnsi = require('strip-ansi')

function * run (context, heroku) {
const statusHelper = require('../../status_helper')
Expand Down Expand Up @@ -74,7 +75,7 @@ function * run (context, heroku) {

col.optimizationWidth = Math.max(
col.optimizationWidth,
formattedValue.length
stripAnsi(formattedValue).length
)
}
}
Expand All @@ -87,6 +88,24 @@ function * run (context, heroku) {
}
}

let handleColorStatus = function (options) {
if (context.flags.forceColor !== true) {
return
}

cli.color.enabled = true

let concatArguments = function (args) {
return Array.prototype.map.call(args, function (arg) {
return arg + ''
}).join(' ')
}

options.printLine = function (...args) {
cli.stdout += concatArguments(args) + '\n'
}
}

if (context.flags.json) {
cli.log(JSON.stringify(releases, null, 2))
} else if (context.flags.extended) {
Expand All @@ -102,6 +121,7 @@ function * run (context, heroku) {
{key: 'extended.slug_uuid'}
]
}
handleColorStatus(options)
optimizeWidth(releases, options.columns, 'description')
cli.table(releases, options)
} else if (releases.length === 0) {
Expand All @@ -117,6 +137,7 @@ function * run (context, heroku) {
{key: 'created_at', format: (t) => time.ago(new Date(t))}
]
}
handleColorStatus(options)
optimizeWidth(releases, options.columns, 'description')
cli.table(releases, options)
}
Expand Down
35 changes: 35 additions & 0 deletions test/commands/releases/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const cli = require('heroku-cli-util')
const nock = require('nock')
const cmd = require('../../../src/commands/releases')
const expect = require('unexpected')
const stripAnsi = require('strip-ansi')

const assertLineWidths = function (blob, lineWidth) {
let lines = blob.split('\n')
Expand Down Expand Up @@ -323,6 +324,27 @@ v37 first commit jeff@heroku.com 2015/11/18 01:36:38 +0000
.then(() => api.done())
})

it('shows releases with correct width for ansi colors', () => {
process.stdout.columns = 80
let api = nock('https://api.heroku.com:443')
.get('/apps/myapp/releases')
.reply(200, releases)
.get('/apps/myapp/slugs/37994c83-39a3-4cbf-b318-8f9dc648f701')
.reply(200, slug)
return cmd.run({app: 'myapp', flags: {forceColor: true}})
.then(() => expect(stripAnsi(cli.stdout), 'to equal', `=== myapp Releases - Current: v37
v41 thir… release command executing jeff@heroku.com 2015/11/18 01:36:38 +0000
v40 Set foo config vars jeff@heroku.com 2015/11/18 01:37:41 +0000
v39 Remove … release command failed jeff@heroku.com 2015/11/18 01:36:38 +0000
v38 seco… release command executing jeff@heroku.com 2015/11/18 01:36:38 +0000
v37 first commit jeff@heroku.com 2015/11/18 01:36:38 +0000
`))
.then(() => assertLineWidths(stripAnsi(cli.stdout), 80))
.then(() => expect(stripAnsi(cli.stdout), 'not to equal', cli.stdout))
.then(() => expect(cli.stderr, 'to be empty'))
.then(() => api.done())
})

it('shows pending releases without release phase', () => {
process.stdout.columns = 80
let api = nock('https://api.heroku.com:443')
Expand Down Expand Up @@ -404,6 +426,19 @@ v40 Set foo config vars jeff@heroku.com 2015/11/18 01:37:41 +0000 1
.then(() => api.done())
})

it('shows extended info with correct width for ansi colors', () => {
let api = nock('https://api.heroku.com:443')
.get('/apps/myapp/releases?extended=true')
.reply(200, extended)
return cmd.run({app: 'myapp', flags: {extended: true, forceColor: true}})
.then(() => expect(stripAnsi(cli.stdout), 'to equal', `=== myapp Releases
v40 Set foo config vars jeff@heroku.com 2015/11/18 01:37:41 +0000 1 uuid
`))
.then(() => expect(stripAnsi(cli.stdout), 'not to equal', cli.stdout))
.then(() => expect(cli.stderr, 'to be empty'))
.then(() => api.done())
})

it('shows no current release', () => {
process.stdout.columns = 80
releases[releases.length - 1].current = false
Expand Down

0 comments on commit f372ce6

Please sign in to comment.