Module: Clack::Environment
- Defined in:
- lib/clack/environment.rb
Overview
Environment detection utilities for cross-platform compatibility and CI/terminal environment awareness.
Class Method Summary collapse
-
.ci? ⇒ Boolean
Check if running in a CI environment Common CI env vars: CI, CONTINUOUS_INTEGRATION, BUILD_NUMBER, GITHUB_ACTIONS, etc.
-
.colors_supported?(output = $stdout) ⇒ Boolean
Check if ANSI colors are supported.
-
.columns(output = $stdout, default: 80) ⇒ Integer
Get terminal columns (width).
-
.dimensions(output = $stdout) ⇒ Array<Integer>
Get terminal dimensions as [rows, columns].
-
.dumb_terminal? ⇒ Boolean
Check if running in a dumb terminal (no ANSI support).
-
.raw_mode_supported?(input = $stdin) ⇒ Boolean
Check if raw mode is supported for input.
-
.reset! ⇒ Object
Reset cached environment checks (useful for testing).
-
.rows(output = $stdout, default: 24) ⇒ Integer
Get terminal rows (height).
-
.tty?(output = $stdout) ⇒ Boolean
Check if stdout is a TTY (interactive terminal).
-
.windows? ⇒ Boolean
Check if running on Windows.
-
.windows_terminal? ⇒ Boolean
Check if running in Windows Terminal (modern).
Class Method Details
.ci? ⇒ Boolean
Check if running in a CI environment Common CI env vars: CI, CONTINUOUS_INTEGRATION, BUILD_NUMBER, GITHUB_ACTIONS, etc.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/clack/environment.rb', line 21 def ci? return @ci if defined?(@ci) @ci = ENV["CI"] == "true" || ENV["CONTINUOUS_INTEGRATION"] == "true" || ENV.key?("BUILD_NUMBER") || ENV.key?("GITHUB_ACTIONS") || ENV.key?("GITLAB_CI") || ENV.key?("CIRCLECI") || ENV.key?("TRAVIS") || ENV.key?("JENKINS_URL") || ENV.key?("TEAMCITY_VERSION") || ENV.key?("BUILDKITE") end |
.colors_supported?(output = $stdout) ⇒ Boolean
Check if ANSI colors are supported
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/clack/environment.rb', line 60 def colors_supported?(output = $stdout) return false if ENV["NO_COLOR"] return true if ENV["FORCE_COLOR"] return false unless tty?(output) return false if dumb_terminal? # Windows: Modern Windows Terminal, ConEmu, ANSICON, or Windows 10 1511+ # all support ANSI. We optimistically assume modern systems support it. true end |
.columns(output = $stdout, default: 80) ⇒ Integer
Get terminal columns (width)
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/clack/environment.rb', line 75 def columns(output = $stdout, default: 80) return default unless tty?(output) if output.respond_to?(:winsize) _, cols = output.winsize (cols > 0) ? cols : default else default end rescue IOError, SystemCallError default end |
.dimensions(output = $stdout) ⇒ Array<Integer>
Get terminal dimensions as [rows, columns]
108 109 110 |
# File 'lib/clack/environment.rb', line 108 def dimensions(output = $stdout) [rows(output), columns(output)] end |
.dumb_terminal? ⇒ Boolean
Check if running in a dumb terminal (no ANSI support)
53 54 55 |
# File 'lib/clack/environment.rb', line 53 def dumb_terminal? ENV["TERM"] == "dumb" end |
.raw_mode_supported?(input = $stdin) ⇒ Boolean
Check if raw mode is supported for input
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/clack/environment.rb', line 115 def raw_mode_supported?(input = $stdin) return false unless input.respond_to?(:raw) # On Windows without proper console, raw mode may fail if windows? && !windows_terminal? begin IO.console&.respond_to?(:raw) rescue IOError, SystemCallError false end else true end end |
.reset! ⇒ Object
Reset cached environment checks (useful for testing)
131 132 133 134 |
# File 'lib/clack/environment.rb', line 131 def reset! remove_instance_variable(:@windows) if defined?(@windows) remove_instance_variable(:@ci) if defined?(@ci) end |
.rows(output = $stdout, default: 24) ⇒ Integer
Get terminal rows (height)
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/clack/environment.rb', line 92 def rows(output = $stdout, default: 24) return default unless tty?(output) if output.respond_to?(:winsize) rows, = output.winsize (rows > 0) ? rows : default else default end rescue IOError, SystemCallError default end |
.tty?(output = $stdout) ⇒ Boolean
Check if stdout is a TTY (interactive terminal)
39 40 41 42 43 |
# File 'lib/clack/environment.rb', line 39 def tty?(output = $stdout) output.respond_to?(:tty?) && output.tty? rescue IOError false end |
.windows? ⇒ Boolean
Check if running on Windows
12 13 14 15 16 |
# File 'lib/clack/environment.rb', line 12 def windows? return @windows if defined?(@windows) @windows = !!(RUBY_PLATFORM =~ /mswin|mingw|cygwin|bccwin/i) end |
.windows_terminal? ⇒ Boolean
Check if running in Windows Terminal (modern)
47 48 49 |
# File 'lib/clack/environment.rb', line 47 def windows_terminal? windows? && ENV.key?("WT_SESSION") end |