From bb0a3a80c446bdadc46778c2db8e5dc38a5d8f1e Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Thu, 26 May 2022 09:16:14 +0200 Subject: [PATCH] Add colorize gem + cli option --- Gemfile.lock | 2 ++ bin/git-format-staged | 4 ++++ format-staged.gemspec | 2 ++ lib/format-staged/io.rb | 1 + lib/format_staged.rb | 5 ++++- 5 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 049ae52..23520a5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,11 +2,13 @@ PATH remote: . specs: format-staged (0.0.3) + colorize GEM remote: http://rubygems.org/ specs: ast (2.4.2) + colorize (0.8.1) parallel (1.22.1) parser (3.1.2.0) ast (~> 2.4.1) diff --git a/bin/git-format-staged b/bin/git-format-staged index 6e20263..faa78af 100755 --- a/bin/git-format-staged +++ b/bin/git-format-staged @@ -57,6 +57,10 @@ parser = OptionParser.new do |opt| puts FormatStaged::VERSION exit end + + opt.on('--[no-]color', 'Colorizes output') do |value| + parameters[:color_output] = value + end end parser.parse! diff --git a/format-staged.gemspec b/format-staged.gemspec index bf7935a..ac7abdc 100644 --- a/format-staged.gemspec +++ b/format-staged.gemspec @@ -16,6 +16,8 @@ Gem::Specification.new do |s| s.homepage = 'https://github.com/5sw/format-staged' s.license = 'MIT' s.required_ruby_version = '~> 2.7' + + s.add_dependency 'colorize' s.add_development_dependency 'rake', '~> 13.0' s.add_development_dependency 'rubocop', '~> 1.29' diff --git a/lib/format-staged/io.rb b/lib/format-staged/io.rb index 6e99768..df472e3 100644 --- a/lib/format-staged/io.rb +++ b/lib/format-staged/io.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'English' + class FormatStaged def get_output(*args, lines: true, silent: false) puts "> #{args.join(' ')}" if @verbose diff --git a/lib/format_staged.rb b/lib/format_staged.rb index bac0ab4..d651d1c 100644 --- a/lib/format_staged.rb +++ b/lib/format_staged.rb @@ -5,18 +5,21 @@ require 'format-staged/version' require 'format-staged/entry' require 'format-staged/io' require 'shellwords' +require 'colorize' ## # Runs staged changes through a formatting tool class FormatStaged attr_reader :formatter, :patterns, :update, :write, :verbose - def initialize(formatter:, patterns:, update: true, write: true, verbose: true) + def initialize(formatter:, patterns:, update: true, write: true, verbose: true, color_output: nil) @formatter = formatter @patterns = patterns @update = update @write = write @verbose = verbose + + String.disable_colorization = !(color_output || STDOUT.isatty) end def run