From 400a1318e0fcc92332a177b85dae4965be304369 Mon Sep 17 00:00:00 2001 From: Matt Wilson Date: Sun, 15 Nov 2026 18:20:35 +1100 Subject: [PATCH 1/4] chore: +x for examples and test programs --- examples/print_status.rb | 0 test/unit/tc_codes.rb | 0 test/unit/tc_version.rb | 0 test/unit/ts_all.rb | 0 4 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 examples/print_status.rb mode change 100644 => 100755 test/unit/tc_codes.rb mode change 100644 => 100755 test/unit/tc_version.rb mode change 100644 => 100755 test/unit/ts_all.rb diff --git a/examples/print_status.rb b/examples/print_status.rb old mode 100644 new mode 100755 diff --git a/test/unit/tc_codes.rb b/test/unit/tc_codes.rb old mode 100644 new mode 100755 diff --git a/test/unit/tc_version.rb b/test/unit/tc_version.rb old mode 100644 new mode 100755 diff --git a/test/unit/ts_all.rb b/test/unit/ts_all.rb old mode 100644 new mode 100755 From 729ea3eeda0ca873a99d1b64b3881656de260c4f Mon Sep 17 00:00:00 2001 From: Matt Wilson Date: Sun, 15 Nov 2026 19:05:42 +1100 Subject: [PATCH 2/4] feat(woad.Ruby): add portable colour policy as 0.1.0-alpha1 Add `BOLD` and `BLINKING`, and cache `colour_policy` until `refresh`. `supports_colour` and `seq_for` follow Unix attachment and `TERM=dumb`. Decision rows 1-22 are component tests; Windows VT and mintty wait; --- CHANGES.md | 8 + EXAMPLES.md | 1 + NEWS.md | 16 +- README.md | 29 ++- TODO.md | 5 +- examples/conditional.md | 37 ++++ examples/conditional.rb | 12 ++ lib/woad.rb | 163 ++++++++++++++- lib/woad/version.rb | 4 +- test/component/tc_colour_policy.rb | 317 +++++++++++++++++++++++++++++ test/unit/tc_codes.rb | 12 ++ test/unit/tc_version.rb | 5 +- 12 files changed, 593 insertions(+), 16 deletions(-) create mode 100644 examples/conditional.md create mode 100755 examples/conditional.rb create mode 100755 test/component/tc_colour_policy.rb diff --git a/CHANGES.md b/CHANGES.md index c69b323..fde5179 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,14 @@ # woad.Ruby - Changes +## 0.1.0-alpha1 - 16th November 2026 + +* added `BLINKING` and `BOLD`, byte-for-byte with **woad** **0.3.0**; +* added cached `colour_policy`, `refresh`, `supports_colour`, and `seq_for` for the portable colour contract (Unix attachment and `TERM=dumb`; no console probe); +* added **test/component/tc_colour_policy.rb** (decision rows 1-22) and **examples/conditional**; +* left Windows virtual-terminal mode and mintty PTY recognition for a later alpha; + + ## 0.0.4 - 30th August 2026 * added shared **.vimrc** editor configuration; diff --git a/EXAMPLES.md b/EXAMPLES.md index d61c073..aa35681 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -2,6 +2,7 @@ |Name|Source & Description|Summary| |---|---|---| +|**conditional**|[examples/conditional.rb](./examples/conditional.rb)
[examples/conditional.md](./examples/conditional.md)|Prints a sequence only when the writer supports colour| |**print_status**|[examples/print_status.rb](./examples/print_status.rb)
[examples/print_status.md](./examples/print_status.md)|Prints sample status words with **woad** SGR colour constants| diff --git a/NEWS.md b/NEWS.md index caa2892..b69da4e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,12 +1,14 @@ # woad.Ruby - News -| Date | News Item | -| ---------------- | -------------------------------------------------------------------------------------- | -| 30th August 2026 | [**woad.Ruby** 0.0.4](https://github.com/synesissoftware/woad.Ruby/releases/tag/0.0.4) | -| 28th August 2026 | [**woad.Ruby** 0.0.3](https://github.com/synesissoftware/woad.Ruby/releases/tag/0.0.3) | -| 20th August 2026 | [**woad.Ruby** 0.0.2](https://github.com/synesissoftware/woad.Ruby/releases/tag/0.0.2) | -| 15th August 2026 | [**woad.Ruby** 0.0.1](https://github.com/synesissoftware/woad.Ruby/releases/tag/0.0.1) | -| 15th August 2026 | [**woad.Ruby** 0.0.0](https://github.com/synesissoftware/woad.Ruby/releases/tag/0.0.0) | + +| Date | News Item | Details | +| ------------------- | ------------------------- | -------------------------------------------------------------------- | +| 16th November 2026 | [**woad.Ruby** 0.1.0-alpha1](https://github.com/synesissoftware/woad.Ruby/releases/tag/0.1.0-alpha1) | portable colour policy; `BOLD` and `BLINKING`; conditional `seq_for` | +| 30th August 2026 | [**woad.Ruby** 0.0.4](https://github.com/synesissoftware/woad.Ruby/releases/tag/0.0.4) | shared **.vimrc**; dropped an obsolete CI branch | +| 28th August 2026 | [**woad.Ruby** 0.0.3](https://github.com/synesissoftware/woad.Ruby/releases/tag/0.0.3) | Actions checkout **v7**; Ruby 2.0+ requirement | +| 20th August 2026 | [**woad.Ruby** 0.0.2](https://github.com/synesissoftware/woad.Ruby/releases/tag/0.0.2) | **examples/print_status**; packaging and **README.md** | +| 15th August 2026 | [**woad.Ruby** 0.0.1](https://github.com/synesissoftware/woad.Ruby/releases/tag/0.0.1) | fixed SGR colour and reset sequences | +| 15th August 2026 | [**woad.Ruby** 0.0.0](https://github.com/synesissoftware/woad.Ruby/releases/tag/0.0.0) | initial scaffolding; Ruby 2.0+ | diff --git a/README.md b/README.md index a80a8ba..e752e11 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Minimal ANSI terminal colour codes, for Ruby ## Table of Contents - [Introduction](#introduction) +- [Developer Experience](#developer-experience) - [Installation](#installation) - [Components](#components) - [Examples](#examples) @@ -38,6 +39,30 @@ Minimal ANSI terminal colour codes, for Ruby It has **no dependencies** on any other non-standard library. +## Developer Experience + +**Ruby 2.0+** is already enough. There are no gems to install beyond **woad** itself. + +``` +gem install woad +``` + +The smallest useful call prints a sequence only when the writer supports colour: + +```Ruby +require 'woad' + +green = Woad.seq_for($stdout, Woad::FG_GREEN) +reset = Woad.seq_for($stdout, Woad::RESET) + +puts "#{green}ok#{reset}" +``` + +That is [examples/conditional.rb](./examples/conditional.rb). Fixed sequences (`BOLD`, `BLINKING`, `FG_*`, `BG_*`) always contain the SGR bytes. `colour_policy`, `supports_colour`, and `seq_for` apply the cached environment policy. `refresh` drops that cache. + +From a checkout, `bundle exec rake test` runs the unit and component suites. Installation detail is in [INSTALL.md](./INSTALL.md). + + ## Installation Install via **gem** as in: @@ -59,7 +84,7 @@ puts "#{Woad::FG_GREEN}ok#{Woad::RESET}" ## Components -**woad.Ruby** ships SGR string constants (`RESET`, `FG_*`, `BG_*`, including bright variants). TTY/stream gating and Windows virtual-terminal opt-in are not implemented yet. +**woad.Ruby** ships fixed SGR sequences (`BLINKING`, `BOLD`, `RESET`, `FG_*`, `BG_*`, including bright variants) and the portable colour policy: `colour_policy`, `refresh`, `supports_colour`, and `seq_for`. On Unix a terminal supports colour when the policy is automatic and `TERM` is not `dumb`. Windows virtual-terminal mode and mintty PTY recognition are not implemented. ## Examples @@ -90,7 +115,7 @@ Libraries upon which **woad.Ruby** depends: ##### Runtime Dependencies (aka "Normal Dependencies") -* \; +* \. Windows virtual-terminal mode and mintty PTY recognition are not implemented, so this gem does not bind a console API; ##### Development Dependencies diff --git a/TODO.md b/TODO.md index 2539ea1..c323bfd 100644 --- a/TODO.md +++ b/TODO.md @@ -4,8 +4,9 @@ ## Functional improvements * [x] ~~~SGR colour and reset codes~~~ ✅; -* [ ] TTY-conditional colour codes (process and per-stream); -* [ ] Windows virtual-terminal gating (OS build + `GetConsoleMode`); +* [x] ~~~portable colour policy (`colour_policy`, `refresh`, `supports_colour`, `seq_for`), including `BOLD` and `BLINKING`~~~ - ✅; +* [ ] Windows virtual-terminal gating (OS build + `GetConsoleMode` + `WOAD_SET_CONSOLE_MODE`); +* [ ] mintty / MSYS2 / Cygwin PTY recognition; ## Performance improvements diff --git a/examples/conditional.md b/examples/conditional.md new file mode 100644 index 0000000..be8229c --- /dev/null +++ b/examples/conditional.md @@ -0,0 +1,37 @@ +# woad.Ruby - Example - **conditional** + + +## Summary + +Prints a status word with an SGR sequence only when standard output supports colour. + + +## Source + +```ruby + +#! /usr/bin/env ruby + +# examples/conditional.rb + +require 'woad' + +green = Woad.seq_for($stdout, Woad::FG_GREEN) +reset = Woad.seq_for($stdout, Woad::RESET) + +puts "#{green}ok#{reset}" +``` + +`seq_for` returns the sequence when the writer supports colour, and `""` when it does not. A pipe, `TERM=dumb` under the automatic policy, and an inhibit policy all print `ok` with no escape sequences. + + +## Usage + +When run, this produces the following output (colours omitted here): + +``` +ok +``` + + + diff --git a/examples/conditional.rb b/examples/conditional.rb new file mode 100755 index 0000000..8d085dd --- /dev/null +++ b/examples/conditional.rb @@ -0,0 +1,12 @@ +#! /usr/bin/env ruby + +# examples/conditional.rb + + +require 'woad' + + +green = Woad.seq_for($stdout, Woad::FG_GREEN) +reset = Woad.seq_for($stdout, Woad::RESET) + +puts "#{green}ok#{reset}" diff --git a/lib/woad.rb b/lib/woad.rb index 57ebba8..8f07fc4 100644 --- a/lib/woad.rb +++ b/lib/woad.rb @@ -5,7 +5,7 @@ # Purpose: Primary require for woad.Ruby library # # Created: 15th August 2026 -# Updated: 19th August 2026 +# Updated: 16th November 2026 # # Home: https://github.com/synesissoftware/woad.Ruby # @@ -50,10 +50,169 @@ ## Root module for *woad* module Woad - # Reset + # Attributes (always emit; they do not consult the environment) + BLINKING = "\e[5m" + BOLD = "\e[1m" RESET = "\e[0m" + # Colour policy (cached until refresh) + + COLOUR_POLICY_AUTO = 0 + COLOUR_POLICY_FORCE = 1 + COLOUR_POLICY_INHIBIT = 2 + + # @!visibility private + FALSEY_ = [ # :nodoc: + '0', + 'false', + 'never', + 'no', + 'none', + 'off', + ].freeze + # @!visibility private + TRUEY_ = [ # :nodoc: + '1', + 'always', + 'force', + 'on', + 'true', + 'yes', + ].freeze + private_constant :FALSEY_ + private_constant :TRUEY_ + + # Returns the cached colour policy: 0 auto, 1 force, 2 inhibit. + def self.colour_policy + + ensure_environment_ + + @policy + end + + # Drops the cached policy and the cached TERM=dumb flag. + def self.refresh + + @environment_determined = false + + nil + end + + # True when colour should be emitted for this writer. + def self.supports_colour(io) + + ensure_environment_ + + case @policy + when COLOUR_POLICY_FORCE + + return true + when COLOUR_POLICY_INHIBIT + + return false + end + + return false unless attached_(io) + return false if @term_is_dumb + + true + end + + # The sequence when the writer supports colour; otherwise "". + def self.seq_for(io, seq) + + return '' if seq.nil? + return '' unless supports_colour(io) + + seq + end + + class << self + + private + + def attached_(io) + + return false if io.nil? + return false unless io.respond_to?(:isatty) + + io.isatty ? true : false + end + + def ensure_environment_ + + return if @environment_determined + + @policy = policy_from_environment_ + @term_is_dumb = term_is_dumb_ + @environment_determined = true + end + + def env_nonempty_(name) + + value = ENV[name] + + return nil if value.nil? || value.empty? + + value + end + + def listed_?(value, tokens) + + tokens.any? { |token| value.casecmp(token) == 0 } + end + + def policy_from_environment_ + + value = env_nonempty_('WOAD_COLOUR') + value = env_nonempty_('WOAD_COLOR') if value.nil? + + unless value.nil? + + return COLOUR_POLICY_FORCE if listed_?(value, TRUEY_) + return COLOUR_POLICY_INHIBIT if listed_?(value, FALSEY_) + + return COLOUR_POLICY_AUTO + end + + return COLOUR_POLICY_INHIBIT unless env_nonempty_('NO_COLOR').nil? + + value = env_nonempty_('CLICOLOR_FORCE') + + unless value.nil? + + return COLOUR_POLICY_FORCE if value != '0' + end + + value = env_nonempty_('FORCE_COLOR') + + unless value.nil? + + if value != '0' && value.casecmp('false') != 0 + + return COLOUR_POLICY_FORCE + end + end + + value = env_nonempty_('CLICOLOR') + + unless value.nil? + + return COLOUR_POLICY_INHIBIT if value == '0' + end + + COLOUR_POLICY_AUTO + end + + def term_is_dumb_ + + value = env_nonempty_('TERM') + + !value.nil? && value.casecmp('dumb') == 0 + end + end + # Foreground (standard) FG_BLACK = "\e[30m" diff --git a/lib/woad/version.rb b/lib/woad/version.rb index 93395d2..1d6fa86 100644 --- a/lib/woad/version.rb +++ b/lib/woad/version.rb @@ -5,7 +5,7 @@ # Purpose: Version for woad.Ruby library # # Created: 15th August 2026 -# Updated: 30th August 2026 +# Updated: 16th November 2026 # # Home: https://github.com/synesissoftware/woad.Ruby # @@ -50,7 +50,7 @@ module Woad # Current version of the woad.Ruby library - VERSION = '0.0.4' + VERSION = '0.1.0-alpha1' private # @!visibility private diff --git a/test/component/tc_colour_policy.rb b/test/component/tc_colour_policy.rb new file mode 100755 index 0000000..d5781ac --- /dev/null +++ b/test/component/tc_colour_policy.rb @@ -0,0 +1,317 @@ +#! /usr/bin/env ruby + +$:.unshift File.join(File.dirname(__FILE__), '../../lib') + + +require 'woad' + +require 'test/unit' + + +class Test_colour_policy < Test::Unit::TestCase + + ENV_NAMES_ = [ + 'CLICOLOR', + 'CLICOLOR_FORCE', + 'FORCE_COLOR', + 'NO_COLOR', + 'TERM', + 'WOAD_COLOR', + 'WOAD_COLOUR', + ].freeze + + class Writer_ + + def initialize(attached) + + @attached = attached + end + + def isatty + + @attached + end + end + + TERMINAL_ = Writer_.new(true) + DETACHED_ = Writer_.new(false) + + def observe(assignments, attached) + + saved = {} + + ENV_NAMES_.each do |name| + + saved[name] = ENV.key?(name) ? ENV[name] : nil + ENV.delete(name) + end + + assignments.each do |name, value| + + ENV[name] = value + end + + Woad.refresh + + io = attached ? TERMINAL_ : DETACHED_ + + [ + Woad.colour_policy, + Woad.supports_colour(io), + Woad.seq_for(io, Woad::FG_RED), + Woad.seq_for(io, nil), + ] + ensure + + ENV_NAMES_.each do |name| + + if saved[name].nil? + + ENV.delete(name) + else + + ENV[name] = saved[name] + end + end + + Woad.refresh + end + + def test_row_1_none_terminal + + policy, colour, seq, empty = observe({}, true) + + assert_equal Woad::COLOUR_POLICY_AUTO, policy + assert_equal true, colour + assert_equal Woad::FG_RED, seq + assert_equal '', empty + end + + def test_row_2_none_detached + + policy, colour, seq, empty = observe({}, false) + + assert_equal Woad::COLOUR_POLICY_AUTO, policy + assert_equal false, colour + assert_equal '', seq + assert_equal '', empty + end + + def test_row_3_term_dumb + + policy, colour, = observe({ 'TERM' => 'dumb' }, true) + + assert_equal Woad::COLOUR_POLICY_AUTO, policy + assert_equal false, colour + end + + def test_row_4_term_DUMB + + policy, colour, = observe({ 'TERM' => 'DUMB' }, true) + + assert_equal Woad::COLOUR_POLICY_AUTO, policy + assert_equal false, colour + end + + def test_row_5_woad_colour_1 + + policy, colour, seq, = observe({ 'WOAD_COLOUR' => '1' }, false) + + assert_equal Woad::COLOUR_POLICY_FORCE, policy + assert_equal true, colour + assert_equal Woad::FG_RED, seq + end + + def test_row_6_woad_color_always + + policy, colour, = observe({ 'WOAD_COLOR' => 'always' }, false) + + assert_equal Woad::COLOUR_POLICY_FORCE, policy + assert_equal true, colour + end + + def test_row_7_woad_colour_never + + policy, colour, = observe({ 'WOAD_COLOUR' => 'never' }, true) + + assert_equal Woad::COLOUR_POLICY_INHIBIT, policy + assert_equal false, colour + end + + def test_row_8_woad_colour_auto_terminal + + policy, colour, = observe({ 'WOAD_COLOUR' => 'auto' }, true) + + assert_equal Woad::COLOUR_POLICY_AUTO, policy + assert_equal true, colour + end + + def test_row_9_woad_colour_auto_detached + + policy, colour, = observe({ 'WOAD_COLOUR' => 'auto' }, false) + + assert_equal Woad::COLOUR_POLICY_AUTO, policy + assert_equal false, colour + end + + def test_row_10_woad_colour_purple + + policy, colour, = observe({ 'WOAD_COLOUR' => 'purple' }, true) + + assert_equal Woad::COLOUR_POLICY_AUTO, policy + assert_equal true, colour + end + + def test_row_11_no_color_outranks_force + + policy, colour, = observe({ + 'CLICOLOR_FORCE' => '1', + 'NO_COLOR' => '1', + }, true) + + assert_equal Woad::COLOUR_POLICY_INHIBIT, policy + assert_equal false, colour + end + + def test_row_12_clicolor_force + + policy, colour, = observe({ 'CLICOLOR_FORCE' => '1' }, false) + + assert_equal Woad::COLOUR_POLICY_FORCE, policy + assert_equal true, colour + end + + def test_row_13_clicolor_force_0_then_clicolor_0 + + policy, colour, = observe({ + 'CLICOLOR' => '0', + 'CLICOLOR_FORCE' => '0', + }, true) + + assert_equal Woad::COLOUR_POLICY_INHIBIT, policy + assert_equal false, colour + end + + def test_row_14_force_color + + policy, colour, = observe({ 'FORCE_COLOR' => '1' }, false) + + assert_equal Woad::COLOUR_POLICY_FORCE, policy + assert_equal true, colour + end + + def test_row_15_force_color_false_then_clicolor_0 + + policy, colour, = observe({ + 'CLICOLOR' => '0', + 'FORCE_COLOR' => 'false', + }, true) + + assert_equal Woad::COLOUR_POLICY_INHIBIT, policy + assert_equal false, colour + end + + def test_row_16_force_color_FALSE_detached + + policy, colour, = observe({ 'FORCE_COLOR' => 'FALSE' }, false) + + assert_equal Woad::COLOUR_POLICY_AUTO, policy + assert_equal false, colour + end + + def test_row_17_force_color_0_terminal + + policy, colour, = observe({ 'FORCE_COLOR' => '0' }, true) + + assert_equal Woad::COLOUR_POLICY_AUTO, policy + assert_equal true, colour + end + + def test_row_18_clicolor_0 + + policy, colour, = observe({ 'CLICOLOR' => '0' }, true) + + assert_equal Woad::COLOUR_POLICY_INHIBIT, policy + assert_equal false, colour + end + + def test_row_19_clicolor_1 + + policy, colour, = observe({ 'CLICOLOR' => '1' }, true) + + assert_equal Woad::COLOUR_POLICY_AUTO, policy + assert_equal true, colour + end + + def test_row_20_empty_woad_colour_is_absent + + policy, colour, = observe({ + 'NO_COLOR' => '1', + 'WOAD_COLOUR' => '', + }, true) + + assert_equal Woad::COLOUR_POLICY_INHIBIT, policy + assert_equal false, colour + end + + def test_row_21_empty_no_color_is_absent + + policy, colour, = observe({ 'NO_COLOR' => '' }, true) + + assert_equal Woad::COLOUR_POLICY_AUTO, policy + assert_equal true, colour + end + + def test_row_22_refresh_drops_the_cache + + saved = {} + + ENV_NAMES_.each do |name| + + saved[name] = ENV.key?(name) ? ENV[name] : nil + ENV.delete(name) + end + + ENV['WOAD_COLOUR'] = '1' + Woad.refresh + + assert_equal Woad::COLOUR_POLICY_FORCE, Woad.colour_policy + assert_equal true, Woad.supports_colour(TERMINAL_) + + ENV.delete('WOAD_COLOUR') + ENV['NO_COLOR'] = '1' + + assert_equal Woad::COLOUR_POLICY_FORCE, Woad.colour_policy + assert_equal true, Woad.supports_colour(TERMINAL_) + + Woad.refresh + + assert_equal Woad::COLOUR_POLICY_INHIBIT, Woad.colour_policy + assert_equal false, Woad.supports_colour(TERMINAL_) + ensure + + ENV_NAMES_.each do |name| + + if saved[name].nil? + + ENV.delete(name) + else + + ENV[name] = saved[name] + end + end + + Woad.refresh + end + + def test_force_ignores_term_dumb + + policy, colour, = observe({ + 'TERM' => 'dumb', + 'WOAD_COLOUR' => 'yes', + }, false) + + assert_equal Woad::COLOUR_POLICY_FORCE, policy + assert_equal true, colour + end +end diff --git a/test/unit/tc_codes.rb b/test/unit/tc_codes.rb index 39a1006..5c4ab25 100755 --- a/test/unit/tc_codes.rb +++ b/test/unit/tc_codes.rb @@ -10,6 +10,16 @@ class Test_codes < Test::Unit::TestCase + def test_BLINKING + + assert_equal "\e[5m", Woad::BLINKING + end + + def test_BOLD + + assert_equal "\e[1m", Woad::BOLD + end + def test_RESET assert_equal "\e[0m", Woad::RESET @@ -28,6 +38,8 @@ def test_BG_BLUE def test_codes_are_csi_sgr [ + Woad::BLINKING, + Woad::BOLD, Woad::RESET, Woad::FG_BLACK, Woad::FG_BRIGHT_WHITE, diff --git a/test/unit/tc_version.rb b/test/unit/tc_version.rb index 546a943..f781fb8 100755 --- a/test/unit/tc_version.rb +++ b/test/unit/tc_version.rb @@ -32,6 +32,9 @@ def test_has_VERSION_REVISION def test_VERSION_has_consistent_format - assert_equal Woad::VERSION.split('.')[0..2].join('.'), "#{Woad::VERSION_MAJOR}.#{Woad::VERSION_MINOR}.#{Woad::VERSION_REVISION}" + core = Woad::VERSION.split('-', 2)[0] + + assert_equal '0.1.0-alpha1', Woad::VERSION + assert_equal "#{Woad::VERSION_MAJOR}.#{Woad::VERSION_MINOR}.#{Woad::VERSION_REVISION}", core end end From 0bdca45fb6b5eec4996b2273dac84662753eaa42 Mon Sep 17 00:00:00 2001 From: Matt Wilson Date: Mon, 16 Nov 2026 06:29:58 +1100 Subject: [PATCH 3/4] layout --- lib/woad.rb | 88 ++++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/lib/woad.rb b/lib/woad.rb index 8f07fc4..72bf8df 100644 --- a/lib/woad.rb +++ b/lib/woad.rb @@ -56,6 +56,50 @@ module Woad BOLD = "\e[1m" RESET = "\e[0m" + # Foreground (standard) + + FG_BLACK = "\e[30m" + FG_RED = "\e[31m" + FG_GREEN = "\e[32m" + FG_YELLOW = "\e[33m" + FG_BLUE = "\e[34m" + FG_MAGENTA = "\e[35m" + FG_CYAN = "\e[36m" + FG_WHITE = "\e[37m" + + # Foreground (bright) + + FG_BRIGHT_BLACK = "\e[90m" + FG_BRIGHT_RED = "\e[91m" + FG_BRIGHT_GREEN = "\e[92m" + FG_BRIGHT_YELLOW = "\e[93m" + FG_BRIGHT_BLUE = "\e[94m" + FG_BRIGHT_MAGENTA = "\e[95m" + FG_BRIGHT_CYAN = "\e[96m" + FG_BRIGHT_WHITE = "\e[97m" + + # Background (standard) + + BG_BLACK = "\e[40m" + BG_RED = "\e[41m" + BG_GREEN = "\e[42m" + BG_YELLOW = "\e[43m" + BG_BLUE = "\e[44m" + BG_MAGENTA = "\e[45m" + BG_CYAN = "\e[46m" + BG_WHITE = "\e[47m" + + # Background (bright) + + BG_BRIGHT_BLACK = "\e[100m" + BG_BRIGHT_RED = "\e[101m" + BG_BRIGHT_GREEN = "\e[102m" + BG_BRIGHT_YELLOW = "\e[103m" + BG_BRIGHT_BLUE = "\e[104m" + BG_BRIGHT_MAGENTA = "\e[105m" + BG_BRIGHT_CYAN = "\e[106m" + BG_BRIGHT_WHITE = "\e[107m" + # Colour policy (cached until refresh) COLOUR_POLICY_AUTO = 0 @@ -212,50 +256,6 @@ def term_is_dumb_ !value.nil? && value.casecmp('dumb') == 0 end end - - # Foreground (standard) - - FG_BLACK = "\e[30m" - FG_RED = "\e[31m" - FG_GREEN = "\e[32m" - FG_YELLOW = "\e[33m" - FG_BLUE = "\e[34m" - FG_MAGENTA = "\e[35m" - FG_CYAN = "\e[36m" - FG_WHITE = "\e[37m" - - # Foreground (bright) - - FG_BRIGHT_BLACK = "\e[90m" - FG_BRIGHT_RED = "\e[91m" - FG_BRIGHT_GREEN = "\e[92m" - FG_BRIGHT_YELLOW = "\e[93m" - FG_BRIGHT_BLUE = "\e[94m" - FG_BRIGHT_MAGENTA = "\e[95m" - FG_BRIGHT_CYAN = "\e[96m" - FG_BRIGHT_WHITE = "\e[97m" - - # Background (standard) - - BG_BLACK = "\e[40m" - BG_RED = "\e[41m" - BG_GREEN = "\e[42m" - BG_YELLOW = "\e[43m" - BG_BLUE = "\e[44m" - BG_MAGENTA = "\e[45m" - BG_CYAN = "\e[46m" - BG_WHITE = "\e[47m" - - # Background (bright) - - BG_BRIGHT_BLACK = "\e[100m" - BG_BRIGHT_RED = "\e[101m" - BG_BRIGHT_GREEN = "\e[102m" - BG_BRIGHT_YELLOW = "\e[103m" - BG_BRIGHT_BLUE = "\e[104m" - BG_BRIGHT_MAGENTA = "\e[105m" - BG_BRIGHT_CYAN = "\e[106m" - BG_BRIGHT_WHITE = "\e[107m" end # module Woad From 15c34611ae161e6b75428c19cae275137e29aaad Mon Sep 17 00:00:00 2001 From: Matt Wilson Date: Mon, 16 Nov 2026 06:47:38 +1100 Subject: [PATCH 4/4] refactor(woad.Ruby): extract SGR sequences and record the policy - Move the fixed sequences into `Woad::Sequences`, mixed into `Woad`; - Document precedence, truey and falsey forms, and `TERM=dumb`; - Drop the Ubuntu Ruby 2.0 job; ruby-builder has no tarball for ubuntu-26.04, and the gemspec floor stays `>= 2.0`; --- .github/workflows/ruby.yml | 7 +-- CHANGES.md | 2 + README.md | 19 +++++++ lib/woad.rb | 59 ++++---------------- lib/woad/sequences.rb | 108 +++++++++++++++++++++++++++++++++++++ 5 files changed, 143 insertions(+), 52 deletions(-) create mode 100644 lib/woad/sequences.rb diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index e0155df..ba360ec 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -7,7 +7,7 @@ # # Created: 15th August 2026 -# Updated: 28th August 2026 +# Updated: 16th November 2026 # name: Ruby @@ -58,8 +58,9 @@ jobs: ruby-version: '2.5' - os: ubuntu-latest ruby-version: '2.4' - - os: ubuntu-latest - ruby-version: '2.0' + # 2.0-2.2 have no ruby-builder tarball for ubuntu-26.04 + # (ubuntu-latest). The gem still claims Ruby 2.0; this cell cannot + # install it. - os: windows-latest ruby-version: mingw diff --git a/CHANGES.md b/CHANGES.md index fde5179..9e5f2ee 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,8 @@ * added cached `colour_policy`, `refresh`, `supports_colour`, and `seq_for` for the portable colour contract (Unix attachment and `TERM=dumb`; no console probe); * added **test/component/tc_colour_policy.rb** (decision rows 1-22) and **examples/conditional**; * left Windows virtual-terminal mode and mintty PTY recognition for a later alpha; +* moved the fixed SGR sequences into **Woad::Sequences**, mixed into **Woad**; +* documented the colour environment variables and their accepted forms in **README.md**; ## 0.0.4 - 30th August 2026 diff --git a/README.md b/README.md index e752e11..4894cea 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ Minimal ANSI terminal colour codes, for Ruby - [Developer Experience](#developer-experience) - [Installation](#installation) - [Components](#components) +- [Environment](#environment) - [Examples](#examples) - [Project Information](#project-information) - [Where to get help](#where-to-get-help) @@ -87,6 +88,24 @@ puts "#{Woad::FG_GREEN}ok#{Woad::RESET}" **woad.Ruby** ships fixed SGR sequences (`BLINKING`, `BOLD`, `RESET`, `FG_*`, `BG_*`, including bright variants) and the portable colour policy: `colour_policy`, `refresh`, `supports_colour`, and `seq_for`. On Unix a terminal supports colour when the policy is automatic and `TERM` is not `dumb`. Windows virtual-terminal mode and mintty PTY recognition are not implemented. +## Environment + +`Woad.colour_policy` returns `COLOUR_POLICY_AUTO` (0), `COLOUR_POLICY_FORCE` (1), or `COLOUR_POLICY_INHIBIT` (2). The result is cached until `Woad.refresh` is invoked. + +A variable that is unset or empty is absent. Values are not trimmed. The first present variable in this order wins: + +1. Non-empty `WOAD_COLOUR`, otherwise non-empty `WOAD_COLOR`. Compared case-insensitively. Truey forces: `1`, `always`, `force`, `on`, `true`, `yes`. Falsey inhibits: `0`, `false`, `never`, `no`, `none`, `off`. Anything else, including `auto`, selects auto and does not fall through; +2. Non-empty `NO_COLOR` inhibits, whatever its text is; +3. Non-empty `CLICOLOR_FORCE` other than the exact bytes `0` forces. Exact `0` does not force and does not inhibit; +4. Non-empty `FORCE_COLOR` other than exact `0` or `false` (any capitalisation) forces. Exact `0` and `false` do not force and do not inhibit; +5. `CLICOLOR` equal to the exact bytes `0` inhibits. Any other value is ignored; +6. Otherwise auto. + +`TERM` equal to `dumb`, in any capitalisation, is cached beside the policy. It is not a policy value. It suppresses colour only when the policy is auto. `force` ignores the terminal and `TERM`. `inhibit` never emits colour. + +On Unix, auto plus a terminal plus a `TERM` that is not `dumb` means colour. There is no console probe. `WOAD_SET_CONSOLE_MODE` is not read in this alpha. + + ## Examples Examples are provided in the ```examples``` directory, along with a markdown description for each. A detailed list TOC of them is provided in [EXAMPLES.md](./EXAMPLES.md). diff --git a/lib/woad.rb b/lib/woad.rb index 72bf8df..85115c2 100644 --- a/lib/woad.rb +++ b/lib/woad.rb @@ -47,58 +47,13 @@ =begin =end +require 'woad/sequences' + + ## Root module for *woad* module Woad - # Attributes (always emit; they do not consult the environment) - - BLINKING = "\e[5m" - BOLD = "\e[1m" - RESET = "\e[0m" - - # Foreground (standard) - - FG_BLACK = "\e[30m" - FG_RED = "\e[31m" - FG_GREEN = "\e[32m" - FG_YELLOW = "\e[33m" - FG_BLUE = "\e[34m" - FG_MAGENTA = "\e[35m" - FG_CYAN = "\e[36m" - FG_WHITE = "\e[37m" - - # Foreground (bright) - - FG_BRIGHT_BLACK = "\e[90m" - FG_BRIGHT_RED = "\e[91m" - FG_BRIGHT_GREEN = "\e[92m" - FG_BRIGHT_YELLOW = "\e[93m" - FG_BRIGHT_BLUE = "\e[94m" - FG_BRIGHT_MAGENTA = "\e[95m" - FG_BRIGHT_CYAN = "\e[96m" - FG_BRIGHT_WHITE = "\e[97m" - - # Background (standard) - - BG_BLACK = "\e[40m" - BG_RED = "\e[41m" - BG_GREEN = "\e[42m" - BG_YELLOW = "\e[43m" - BG_BLUE = "\e[44m" - BG_MAGENTA = "\e[45m" - BG_CYAN = "\e[46m" - BG_WHITE = "\e[47m" - - # Background (bright) - - BG_BRIGHT_BLACK = "\e[100m" - BG_BRIGHT_RED = "\e[101m" - BG_BRIGHT_GREEN = "\e[102m" - BG_BRIGHT_YELLOW = "\e[103m" - BG_BRIGHT_BLUE = "\e[104m" - BG_BRIGHT_MAGENTA = "\e[105m" - BG_BRIGHT_CYAN = "\e[106m" - BG_BRIGHT_WHITE = "\e[107m" + include Sequences # Colour policy (cached until refresh) @@ -176,6 +131,7 @@ class << self private + # True when the writer reports a terminal. Nil is not attached. def attached_(io) return false if io.nil? @@ -184,6 +140,7 @@ def attached_(io) io.isatty ? true : false end + # Fills the policy cache and the TERM=dumb flag from the environment. def ensure_environment_ return if @environment_determined @@ -193,6 +150,7 @@ def ensure_environment_ @environment_determined = true end + # The environment value, or nil when the variable is unset or empty. def env_nonempty_(name) value = ENV[name] @@ -202,11 +160,13 @@ def env_nonempty_(name) value end + # True when the value equals one of the tokens, ignoring case. def listed_?(value, tokens) tokens.any? { |token| value.casecmp(token) == 0 } end + # The colour policy from the environment. The first match wins. def policy_from_environment_ value = env_nonempty_('WOAD_COLOUR') @@ -249,6 +209,7 @@ def policy_from_environment_ COLOUR_POLICY_AUTO end + # True when TERM is "dumb" in any capitalisation. def term_is_dumb_ value = env_nonempty_('TERM') diff --git a/lib/woad/sequences.rb b/lib/woad/sequences.rb new file mode 100644 index 0000000..7bd7e1d --- /dev/null +++ b/lib/woad/sequences.rb @@ -0,0 +1,108 @@ +# frozen_string_literal: true +# ######################################################################## # +# File: woad/sequences.rb +# +# Purpose: Fixed SGR sequences for woad.Ruby +# +# Created: 16th November 2026 +# Updated: 16th November 2026 +# +# Home: https://github.com/synesissoftware/woad.Ruby +# +# Author: Matthew Wilson +# +# Copyright (c) 2026, Matthew Wilson and Synesis Information Systems +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# * Neither the names of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# ######################################################################## # + + +=begin +=end + +module Woad + + # Fixed SGR sequences. They always contain the escape bytes. + module Sequences + + # Attributes + + BLINKING = "\e[5m" + BOLD = "\e[1m" + RESET = "\e[0m" + + # Foreground (standard) + + FG_BLACK = "\e[30m" + FG_RED = "\e[31m" + FG_GREEN = "\e[32m" + FG_YELLOW = "\e[33m" + FG_BLUE = "\e[34m" + FG_MAGENTA = "\e[35m" + FG_CYAN = "\e[36m" + FG_WHITE = "\e[37m" + + # Foreground (bright) + + FG_BRIGHT_BLACK = "\e[90m" + FG_BRIGHT_RED = "\e[91m" + FG_BRIGHT_GREEN = "\e[92m" + FG_BRIGHT_YELLOW = "\e[93m" + FG_BRIGHT_BLUE = "\e[94m" + FG_BRIGHT_MAGENTA = "\e[95m" + FG_BRIGHT_CYAN = "\e[96m" + FG_BRIGHT_WHITE = "\e[97m" + + # Background (standard) + + BG_BLACK = "\e[40m" + BG_RED = "\e[41m" + BG_GREEN = "\e[42m" + BG_YELLOW = "\e[43m" + BG_BLUE = "\e[44m" + BG_MAGENTA = "\e[45m" + BG_CYAN = "\e[46m" + BG_WHITE = "\e[47m" + + # Background (bright) + + BG_BRIGHT_BLACK = "\e[100m" + BG_BRIGHT_RED = "\e[101m" + BG_BRIGHT_GREEN = "\e[102m" + BG_BRIGHT_YELLOW = "\e[103m" + BG_BRIGHT_BLUE = "\e[104m" + BG_BRIGHT_MAGENTA = "\e[105m" + BG_BRIGHT_CYAN = "\e[106m" + BG_BRIGHT_WHITE = "\e[107m" + end # module Sequences +end # module Woad + + +# ############################## end of file ############################# #