diff options
author | Soumith Chintala <soumith@gmail.com> | 2016-04-27 02:18:49 +0300 |
---|---|---|
committer | Soumith Chintala <soumith@gmail.com> | 2016-04-27 02:18:49 +0300 |
commit | 9e879ae03fe8f593ef3d73bd73375f72c17c17e7 (patch) | |
tree | e3c637285849eabbb59a2ec9cec7f8594e6cfff6 | |
parent | 1781cd574bddd39fdad3b3833db510d3b8b1474f (diff) | |
parent | 4275211e4fab824f459f47761276bf4f9b83851c (diff) |
Merge pull request #8 from jbarbero/fix-colors
suppress ANSI color codes if stdout or stderr is not a terminal
-rw-r--r-- | graph.lua | 9 | ||||
-rw-r--r-- | init.lua | 2 | ||||
-rw-r--r-- | usage.lua | 20 |
3 files changed, 18 insertions, 13 deletions
@@ -351,20 +351,21 @@ function ACN:usage(...) function(self) if self.rules and not history[self.rules] then history[self.rules] = true - table.insert(txt, usage(true, self.rules)) + table.insert(txt, usage.usage(true, self.rules)) end end ) return string.format( "%s\n%s\n", table.concat(txt, '\n\nor\n\n'), - usage(false, self, ...) + usage.usage(false, self, ...) ) end function ACN:generate(upvalues) assert(upvalues, 'upvalues table missing') local code = {} + table.insert(code, 'local usage = require "argcheck.usage"') table.insert(code, 'return function(...)') table.insert(code, ' local narg = select("#", ...)') self:generate_ordered_or_named(code, upvalues, 'O') @@ -421,9 +422,9 @@ function ACN:generate(upvalues) end ) if quiet then - table.insert(code, ' return false, graph:usage(...)') + table.insert(code, ' return false, usage.render(graph:usage(...))') else - table.insert(code, ' error(string.format("%s\\ninvalid arguments!", graph:usage(...)))') + table.insert(code, ' error(string.format("%s\\ninvalid arguments!", usage.render(graph:usage(...))))') end table.insert(code, 'end') return table.concat(code, '\n') @@ -99,7 +99,7 @@ local function argcheck(rules) -- dump doc if any if rules.doc or rules.help then - doc(usage(true, rules, true)) + doc(usage.render(usage.usage(true, rules, true))) end local code, upvalues = generaterules(rules) @@ -4,6 +4,8 @@ pcall(function() sdascii = require 'sundown.ascii' end) +local usage = {} + local function generateargp(rules) local txt = {} for idx, rule in ipairs(rules) do @@ -76,7 +78,16 @@ local function generateargt(rules) return txt end -local function usage(truth, rules, ...) +function usage.render(doc) + -- We render any markdown in the input into ANSI color codes using sundown, but only if stdout and stderr are terminals + if sdascii and pcall(require, 'torch') and torch.isatty(io.stderr) and torch.isatty(io.stdout) then + doc = sdascii.render(doc) + end + + return doc +end + +function usage.usage(truth, rules, ...) if truth then local norender = select(1, ...) local doc = rules.help or rules.doc @@ -97,10 +108,6 @@ local function usage(truth, rules, ...) doc = '\n*Arguments:*\n' .. generateargt(rules) end - if sdascii and not norender then - doc = sdascii.render(doc) - end - return doc else local self = rules @@ -137,9 +144,6 @@ local function usage(truth, rules, ...) args[argtblidx] = string.format("**table**={ %s }", table.concat(argtbl, ', ')) end local doc = string.format("*Got:* %s", table.concat(args, ', ')) - if sdascii then - doc = sdascii.render(doc) - end return doc end end |