Skip to content
This repository was archived by the owner on Jan 22, 2026. It is now read-only.

Commit 96add3f

Browse files
committed
Add color support for output in diff, show, and history commands
1 parent 3b28b6e commit 96add3f

File tree

6 files changed

+72
-16
lines changed

6 files changed

+72
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- Consistent error handling across all commands (JSON errors when `--format=json`)
55
- `git pkgs update` now uses a transaction for atomicity and better performance
66
- `GIT_PKGS_DB` environment variable to customize database location
7+
- Colored output for diff, show, and history commands (respects `NO_COLOR`)
78
- Renamed `git pkgs outdated` to `git pkgs stale` (outdated remains as alias)
89
- `git pkgs log` command to list commits with dependency changes
910
- `git pkgs schema` command to output database schema in text, SQL, JSON, or markdown

lib/git/pkgs.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_relative "pkgs/version"
44
require_relative "pkgs/output"
5+
require_relative "pkgs/color"
56
require_relative "pkgs/cli"
67
require_relative "pkgs/database"
78
require_relative "pkgs/repository"

lib/git/pkgs/color.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
module Git
4+
module Pkgs
5+
module Color
6+
CODES = {
7+
red: 31,
8+
green: 32,
9+
yellow: 33,
10+
blue: 34,
11+
magenta: 35,
12+
cyan: 36,
13+
bold: 1,
14+
dim: 2
15+
}.freeze
16+
17+
def self.enabled?
18+
return @enabled if defined?(@enabled)
19+
20+
@enabled = determine_color_support
21+
end
22+
23+
def self.enabled=(value)
24+
@enabled = value
25+
end
26+
27+
def self.determine_color_support
28+
return false unless $stdout.respond_to?(:tty?) && $stdout.tty?
29+
return false if ENV["NO_COLOR"] && !ENV["NO_COLOR"].empty?
30+
return false if ENV["TERM"] == "dumb"
31+
32+
true
33+
end
34+
35+
def self.colorize(text, *codes)
36+
return text unless enabled?
37+
38+
code_str = codes.map { |c| CODES[c] || c }.join(";")
39+
"\e[#{code_str}m#{text}\e[0m"
40+
end
41+
42+
def self.red(text) = colorize(text, :red)
43+
def self.green(text) = colorize(text, :green)
44+
def self.yellow(text) = colorize(text, :yellow)
45+
def self.blue(text) = colorize(text, :blue)
46+
def self.cyan(text) = colorize(text, :cyan)
47+
def self.bold(text) = colorize(text, :bold)
48+
def self.dim(text) = colorize(text, :dim)
49+
end
50+
end
51+
end

lib/git/pkgs/commands/diff.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,35 +60,38 @@ def run
6060
removed = changes.select { |c| c.change_type == "removed" }
6161

6262
if added.any?
63-
puts "Added:"
63+
puts Color.green("Added:")
6464
added.group_by(&:name).each do |name, pkg_changes|
6565
latest = pkg_changes.last
66-
puts " + #{name} #{latest.requirement} (#{latest.manifest.path})"
66+
puts Color.green(" + #{name} #{latest.requirement} (#{latest.manifest.path})")
6767
end
6868
puts
6969
end
7070

7171
if modified.any?
72-
puts "Modified:"
72+
puts Color.yellow("Modified:")
7373
modified.group_by(&:name).each do |name, pkg_changes|
7474
first = pkg_changes.first
7575
latest = pkg_changes.last
76-
puts " ~ #{name} #{first.previous_requirement} -> #{latest.requirement}"
76+
puts Color.yellow(" ~ #{name} #{first.previous_requirement} -> #{latest.requirement}")
7777
end
7878
puts
7979
end
8080

8181
if removed.any?
82-
puts "Removed:"
82+
puts Color.red("Removed:")
8383
removed.group_by(&:name).each do |name, pkg_changes|
8484
latest = pkg_changes.last
85-
puts " - #{name} (was #{latest.requirement})"
85+
puts Color.red(" - #{name} (was #{latest.requirement})")
8686
end
8787
puts
8888
end
8989

9090
# Summary
91-
puts "Summary: +#{added.map(&:name).uniq.count} -#{removed.map(&:name).uniq.count} ~#{modified.map(&:name).uniq.count}"
91+
added_count = Color.green("+#{added.map(&:name).uniq.count}")
92+
removed_count = Color.red("-#{removed.map(&:name).uniq.count}")
93+
modified_count = Color.yellow("~#{modified.map(&:name).uniq.count}")
94+
puts "Summary: #{added_count} #{removed_count} #{modified_count}"
9295
end
9396

9497
def find_or_create_commit(repo, sha)

lib/git/pkgs/commands/history.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ def output_text(changes, package_name)
7676

7777
case change.change_type
7878
when "added"
79-
action = "Added"
79+
action = Color.green("Added")
8080
version_info = change.requirement
8181
when "modified"
82-
action = "Updated"
82+
action = Color.yellow("Updated")
8383
version_info = "#{change.previous_requirement} -> #{change.requirement}"
8484
when "removed"
85-
action = "Removed"
85+
action = Color.red("Removed")
8686
version_info = change.requirement
8787
end
8888

lib/git/pkgs/commands/show.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,25 @@ def output_text(commit, changes)
5656
removed = changes.select { |c| c.change_type == "removed" }
5757

5858
if added.any?
59-
puts "Added:"
59+
puts Color.green("Added:")
6060
added.each do |change|
61-
puts " #{change.name} #{change.requirement} (#{change.ecosystem}, #{change.manifest.path})"
61+
puts Color.green(" + #{change.name} #{change.requirement} (#{change.ecosystem}, #{change.manifest.path})")
6262
end
6363
puts
6464
end
6565

6666
if modified.any?
67-
puts "Modified:"
67+
puts Color.yellow("Modified:")
6868
modified.each do |change|
69-
puts " #{change.name} #{change.previous_requirement} -> #{change.requirement} (#{change.ecosystem}, #{change.manifest.path})"
69+
puts Color.yellow(" ~ #{change.name} #{change.previous_requirement} -> #{change.requirement} (#{change.ecosystem}, #{change.manifest.path})")
7070
end
7171
puts
7272
end
7373

7474
if removed.any?
75-
puts "Removed:"
75+
puts Color.red("Removed:")
7676
removed.each do |change|
77-
puts " #{change.name} #{change.requirement} (#{change.ecosystem}, #{change.manifest.path})"
77+
puts Color.red(" - #{change.name} #{change.requirement} (#{change.ecosystem}, #{change.manifest.path})")
7878
end
7979
puts
8080
end

0 commit comments

Comments
 (0)