From 4fea7d525e890c78a7bbdac7008fb6c976dfb6da Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sun, 29 May 2022 09:25:09 +0200 Subject: [PATCH] Colour output --- lib/format-staged/io.rb | 16 ++++++++++++++++ lib/format_staged.rb | 11 +++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/format-staged/io.rb b/lib/format-staged/io.rb index c7cdaf5..04b633b 100644 --- a/lib/format-staged/io.rb +++ b/lib/format-staged/io.rb @@ -43,4 +43,20 @@ class FormatStaged lines ? splits : result end + + def fail!(message) + abort "💣 #{message.red}" + end + + def warning(message) + warn "⚠️ #{message.yellow}" + end + + def info(message) + puts message.blue + end + + def verbose_info(message) + puts "ℹ️ #{message}" if verbose + end end diff --git a/lib/format_staged.rb b/lib/format_staged.rb index 0eb0d9c..804dbf3 100644 --- a/lib/format_staged.rb +++ b/lib/format_staged.rb @@ -23,8 +23,11 @@ class FormatStaged end def run + verbose_info "Finding repository root" root = get_output('git', 'rev-parse', '--show-toplevel').first + verbose_info "Repo at #{root}" + verbose_info "Listing staged files" files = get_output('git', 'diff-index', '--cached', '--diff-filter=AM', '--no-renames', 'HEAD') .map { |line| Entry.new(line, root: root) } .reject(&:symlink?) @@ -41,12 +44,12 @@ class FormatStaged return true unless write if new_hash == file.dst_hash - puts "Unchanged #{file.src_path}" + info "Unchanged #{file.src_path}" return false end if object_is_empty new_hash - puts "Skipping #{file.src_path}, formatted file is empty" + info "Skipping #{file.src_path}, formatted file is empty" return false end @@ -56,7 +59,7 @@ class FormatStaged begin patch_working_file file, new_hash rescue StandardError => e - puts "Warning: failed updating #{file.src_path} in working copy: #{e}" + warning "failed updating #{file.src_path} in working copy: #{e}" end end @@ -64,7 +67,7 @@ class FormatStaged end def format_object(file) - puts "Formatting #{file.src_path}" + info "Formatting #{file.src_path}" format_command = formatter.sub('{}', file.src_path.shellescape)