From 4275211e4fab824f459f47761276bf4f9b83851c Mon Sep 17 00:00:00 2001 From: jbarbero Date: Tue, 26 Apr 2016 16:10:48 -0700 Subject: suppress ANSI color codes if stdout or stderr is not a terminal --- graph.lua | 9 +++++---- init.lua | 2 +- usage.lua | 20 ++++++++++++-------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/graph.lua b/graph.lua index 32e3a0c..7d839fb 100644 --- a/graph.lua +++ b/graph.lua @@ -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') diff --git a/init.lua b/init.lua index 0461de3..8e67716 100644 --- a/init.lua +++ b/init.lua @@ -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) diff --git a/usage.lua b/usage.lua index 3eed346..fe6165b 100644 --- a/usage.lua +++ b/usage.lua @@ -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 -- cgit v1.2.3