Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/torch/argcheck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSoumith Chintala <soumith@gmail.com>2016-04-27 02:18:49 +0300
committerSoumith Chintala <soumith@gmail.com>2016-04-27 02:18:49 +0300
commit9e879ae03fe8f593ef3d73bd73375f72c17c17e7 (patch)
treee3c637285849eabbb59a2ec9cec7f8594e6cfff6
parent1781cd574bddd39fdad3b3833db510d3b8b1474f (diff)
parent4275211e4fab824f459f47761276bf4f9b83851c (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.lua9
-rw-r--r--init.lua2
-rw-r--r--usage.lua20
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