philiprehberger-cli_kit 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c4606c83ce0c95c7b047c9db662457bce97852169987a96a44566d4811e78313
4
- data.tar.gz: 907327c09e57175ca6e1c07d03369fdfe5f157534836e64acf348b79dc439a5e
3
+ metadata.gz: eae06fcc20ac2c4b561e9344b149ce324226182696988a8a80409dd21b95fb13
4
+ data.tar.gz: 1f3717ee7a4fd6a60f3ba3df8f716538c8c7300deb2d3f7f54085a1d87e8c4c8
5
5
  SHA512:
6
- metadata.gz: '08bb60e7b9c05a9e6d5572a8a393f61b987c07a136f7cb1eaf6e6ac58168fea50230f5855d073c47bd0516b178d19e138d223ccc7ff4e529248b9aef16f08699'
7
- data.tar.gz: 20864d8ed45d20aabe85d072ba1adcb44590c76f346329e51314b4163bce07951b2759327ef852112bccaec05f82705e28b5534502d5b5dde6e2b0efaddb4733
6
+ metadata.gz: da8187a4ef2b10558b7d1950318da743851f60423580550c001dc61059a579fccedcca8a29161d2252b2a242fd61d7f8877fee666ed57e0cc6f73a97398b56fe
7
+ data.tar.gz: 4ef31ad27f1536ba102f013cad027d0d38597a5e3f6492307f2ff0a328d63de572cf42476c63e33cb73d44cb72ddc0182c8b8e9afafe706507114bb970c33d04
data/CHANGELOG.md CHANGED
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.6.0] - 2026-05-02
11
+
12
+ ### Added
13
+ - `CliKit.strip_color(text)` (and `Colorize.strip(text)`) to remove ANSI escape sequences from a string — useful for testing colored output and writing colored messages to log files
14
+
15
+ ## [0.5.0] - 2026-04-25
16
+
17
+ ### Added
18
+ - ANSI color helpers via `CliKit.color`, `CliKit.bold`, `CliKit.dim` and the `CliKit::Colorize` module
19
+ - Automatic disable when stdout is not a TTY or `NO_COLOR` is set
20
+
10
21
  ## [0.4.0] - 2026-04-18
11
22
 
12
23
  ### Added
@@ -68,7 +79,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
68
79
  - Animated spinner for long-running operations
69
80
  - Positional argument collection
70
81
 
71
- [Unreleased]: https://github.com/philiprehberger/rb-cli-kit/compare/v0.4.0...HEAD
82
+ [Unreleased]: https://github.com/philiprehberger/rb-cli-kit/compare/v0.5.0...HEAD
83
+ [0.5.0]: https://github.com/philiprehberger/rb-cli-kit/compare/v0.4.0...v0.5.0
72
84
  [0.4.0]: https://github.com/philiprehberger/rb-cli-kit/compare/v0.3.1...v0.4.0
73
85
  [0.3.1]: https://github.com/philiprehberger/rb-cli-kit/compare/v0.3.0...v0.3.1
74
86
  [0.3.0]: https://github.com/philiprehberger/rb-cli-kit/compare/v0.2.1...v0.3.0
data/README.md CHANGED
@@ -177,6 +177,21 @@ data = Philiprehberger::CliKit.spinner('Loading data...') do
177
177
  end
178
178
  ```
179
179
 
180
+ ### Color Output
181
+
182
+ Colors are auto-disabled when stdout is not a TTY or the `NO_COLOR` environment variable is set.
183
+
184
+ ```ruby
185
+ require "philiprehberger/cli_kit"
186
+
187
+ puts Philiprehberger::CliKit.color('OK', :green)
188
+ puts Philiprehberger::CliKit.bold('Important')
189
+
190
+ # Strip ANSI escapes (useful for tests or logging colored output to a file)
191
+ colored = Philiprehberger::CliKit.color('OK', :green)
192
+ Philiprehberger::CliKit.strip_color(colored) # => 'OK'
193
+ ```
194
+
180
195
  ## API
181
196
 
182
197
  | Method | Description |
@@ -189,6 +204,10 @@ end
189
204
  | `.select(message, choices)` | Present numbered menu and return one selection |
190
205
  | `.multi_select(message, choices, defaults:)` | Present numbered menu and return multiple selections |
191
206
  | `.spinner(message) { ... }` | Show spinner during block execution |
207
+ | `.color(text, name)` | Wrap text in ANSI color (no-op when not a TTY or NO_COLOR set) |
208
+ | `.bold(text)` | Wrap text in ANSI bold |
209
+ | `.dim(text)` | Wrap text in ANSI dim |
210
+ | `.strip_color(text)` | Remove ANSI escape sequences from text |
192
211
  | `Parser#option(name, multi: true)` | Collect repeated option values into an array |
193
212
  | `Parser#option(name, required: true)` | Raise `CliKit::Error` at parse time when the option is omitted |
194
213
  | `Parser#flags` | Hash of boolean flag values |
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Philiprehberger
4
+ module CliKit
5
+ # ANSI color and style helpers. Output is auto-disabled when stdout is not a
6
+ # TTY or the NO_COLOR environment variable is set (per https://no-color.org).
7
+ module Colorize
8
+ CODES = {
9
+ red: 31, green: 32, yellow: 33, blue: 34,
10
+ magenta: 35, cyan: 36, white: 37, gray: 90
11
+ }.freeze
12
+
13
+ module_function
14
+
15
+ # @return [Boolean] true when ANSI escape codes should be emitted
16
+ def enabled?
17
+ return false if ENV.key?('NO_COLOR')
18
+
19
+ $stdout.tty?
20
+ end
21
+
22
+ # Wraps text in an ANSI color escape, or returns it untouched when colors
23
+ # are disabled.
24
+ #
25
+ # @param text [String]
26
+ # @param name [Symbol] one of :red, :green, :yellow, :blue, :magenta, :cyan, :white, :gray
27
+ # @return [String]
28
+ def color(text, name)
29
+ return text unless enabled?
30
+
31
+ code = CODES.fetch(name) { raise ArgumentError, "unknown color: #{name.inspect}" }
32
+ "\e[#{code}m#{text}\e[0m"
33
+ end
34
+
35
+ # @param text [String]
36
+ # @return [String]
37
+ def bold(text)
38
+ return text unless enabled?
39
+
40
+ "\e[1m#{text}\e[0m"
41
+ end
42
+
43
+ # @param text [String]
44
+ # @return [String]
45
+ def dim(text)
46
+ return text unless enabled?
47
+
48
+ "\e[2m#{text}\e[0m"
49
+ end
50
+
51
+ # Remove ANSI escape sequences (color, bold, dim, reset, etc.) from text.
52
+ # Idempotent on already-plain strings. Returns text unchanged when input
53
+ # is not a String (e.g. nil → nil).
54
+ #
55
+ # @param text [String, Object]
56
+ # @return [String, Object]
57
+ def strip(text)
58
+ return text unless text.is_a?(String)
59
+
60
+ text.gsub(/\e\[[0-9;]*m/, '')
61
+ end
62
+ end
63
+ end
64
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Philiprehberger
4
4
  module CliKit
5
- VERSION = '0.4.0'
5
+ VERSION = '0.6.0'
6
6
  end
7
7
  end
@@ -5,6 +5,7 @@ require_relative 'cli_kit/parser'
5
5
  require_relative 'cli_kit/prompt'
6
6
  require_relative 'cli_kit/spinner'
7
7
  require_relative 'cli_kit/menu'
8
+ require_relative 'cli_kit/colorize'
8
9
 
9
10
  module Philiprehberger
10
11
  module CliKit
@@ -105,5 +106,35 @@ module Philiprehberger
105
106
  def self.multi_select(message, choices, defaults: [], input: $stdin, output: $stdout)
106
107
  Menu.multi_select(message, choices, defaults: defaults, input: input, output: output)
107
108
  end
109
+
110
+ # Wraps text in ANSI color (auto-disabled when not a TTY or NO_COLOR is set).
111
+ #
112
+ # @param text [String]
113
+ # @param name [Symbol]
114
+ # @return [String]
115
+ def self.color(text, name)
116
+ Colorize.color(text, name)
117
+ end
118
+
119
+ # @param text [String]
120
+ # @return [String]
121
+ def self.bold(text)
122
+ Colorize.bold(text)
123
+ end
124
+
125
+ # @param text [String]
126
+ # @return [String]
127
+ def self.dim(text)
128
+ Colorize.dim(text)
129
+ end
130
+
131
+ # Remove ANSI escape sequences from text. Useful for testing colored output
132
+ # or for logging colored messages without escape codes leaking through.
133
+ #
134
+ # @param text [String]
135
+ # @return [String]
136
+ def self.strip_color(text)
137
+ Colorize.strip(text)
138
+ end
108
139
  end
109
140
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philiprehberger-cli_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philip Rehberger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-04-19 00:00:00.000000000 Z
11
+ date: 2026-05-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Lightweight CLI toolkit combining argument parsing with flags and options,
14
14
  interactive prompts with confirmation, and animated spinners for long-running operations.
@@ -22,6 +22,7 @@ files:
22
22
  - LICENSE
23
23
  - README.md
24
24
  - lib/philiprehberger/cli_kit.rb
25
+ - lib/philiprehberger/cli_kit/colorize.rb
25
26
  - lib/philiprehberger/cli_kit/menu.rb
26
27
  - lib/philiprehberger/cli_kit/parser.rb
27
28
  - lib/philiprehberger/cli_kit/prompt.rb