Add --no-color cli flag

closes #72
This commit is contained in:
Vitalii Elenhaupt 2018-08-13 00:04:39 +03:00
parent 248c5a656b
commit d60aea102f
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
3 changed files with 18 additions and 2 deletions

View file

@ -57,6 +57,16 @@ module Ameba::Cli
c.formatter.should eq :todo c.formatter.should eq :todo
end end
it "accepts --no-color flag" do
c = Cli.parse_args %w(--no-color)
c.colors?.should be_false
end
it "doesn't disable colors by default" do
c = Cli.parse_args %w(--all)
c.colors?.should be_true
end
it "ignores --config if --gen-config flag passed" do it "ignores --config if --gen-config flag passed" do
c = Cli.parse_args %w(--gen-config --config my_config.yml) c = Cli.parse_args %w(--gen-config --config my_config.yml)
c.formatter.should eq :todo c.formatter.should eq :todo

View file

@ -7,7 +7,7 @@ module Ameba::Cli
def run(args) def run(args)
opts = parse_args args opts = parse_args args
config = Config.load opts.config config = Config.load opts.config, opts.colors?
config.files = opts.files config.files = opts.files
configure_formatter(config, opts) configure_formatter(config, opts)
@ -26,6 +26,7 @@ module Ameba::Cli
property only : Array(String)? property only : Array(String)?
property except : Array(String)? property except : Array(String)?
property? all = false property? all = false
property? colors = true
end end
def parse_args(args, opts = Opts.new) def parse_args(args, opts = Opts.new)
@ -66,6 +67,10 @@ module Ameba::Cli
opts.formatter = :todo opts.formatter = :todo
opts.config = "" opts.config = ""
end end
parser.on("--no-color", "Disable colors") do
opts.colors = false
end
end end
opts opts

View file

@ -46,7 +46,8 @@ class Ameba::Config
# config = Ameba::Config.load # config = Ameba::Config.load
# ``` # ```
# #
def self.load(path = PATH) def self.load(path = PATH, colors? = true)
Colorize.enabled = colors?
content = File.exists?(path) ? File.read path : "" content = File.exists?(path) ? File.read path : ""
Config.new YAML.parse(content) Config.new YAML.parse(content)
rescue e rescue e