Module: Tryouts::Console
- Defined in:
- lib/tryouts/console.rb
Defined Under Namespace
Modules: InstanceMethods
Constant Summary collapse
- ATTRIBUTES =
{ normal: 0, bright: 1, dim: 2, underline: 4, blink: 5, reverse: 7, hidden: 8, default: 0, }.freeze
- COLOURS =
{ black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37, default: 39, random: 30 + rand(10).to_i, }.freeze
- BGCOLOURS =
{ black: 40, red: 41, green: 42, yellow: 43, blue: 44, magenta: 45, cyan: 46, white: 47, default: 49, random: 40 + rand(10).to_i, }.freeze
Class Method Summary collapse
- .att(name, str, io = $stdout) ⇒ Object
- .bgcolor(col, str, io = $stdout) ⇒ Object
- .bright(str, io = $stdout) ⇒ Object
- .color(col, str, io = $stdout) ⇒ Object
- .default_style(io = $stdout) ⇒ Object
-
.pretty_backtrace(backtrace, limit: 10) ⇒ Object
Format backtrace entries with pretty file paths.
-
.pretty_path(filepath) ⇒ Object
Converts an absolute file path to a path relative to the current working directory.
- .reverse(str, io = $stdout) ⇒ Object
- .style(*att, io: nil) ⇒ Object
- .underline(str, io = $stdout) ⇒ Object
Class Method Details
.att(name, str, io = $stdout) ⇒ Object
105 106 107 108 109 |
# File 'lib/tryouts/console.rb', line 105 def att(name, str, io = $stdout) str = [style(ATTRIBUTES[name], io: io), str, default_style(io)].join str.extend Console::InstanceMethods str end |
.bgcolor(col, str, io = $stdout) ⇒ Object
111 112 113 114 115 |
# File 'lib/tryouts/console.rb', line 111 def bgcolor(col, str, io = $stdout) str = [style(ATTRIBUTES[col], io: io), str, default_style(io)].join str.extend Console::InstanceMethods str end |
.bright(str, io = $stdout) ⇒ Object
81 82 83 84 85 |
# File 'lib/tryouts/console.rb', line 81 def bright(str, io = $stdout) str = [style(ATTRIBUTES[:bright], io: io), str, default_style(io)].join str.extend Console::InstanceMethods str end |
.color(col, str, io = $stdout) ⇒ Object
99 100 101 102 103 |
# File 'lib/tryouts/console.rb', line 99 def color(col, str, io = $stdout) str = [style(COLOURS[col], io: io), str, default_style(io)].join str.extend Console::InstanceMethods str end |
.default_style(io = $stdout) ⇒ Object
158 159 160 |
# File 'lib/tryouts/console.rb', line 158 def default_style(io = $stdout) style(ATTRIBUTES[:default], COLOURS[:default], BGCOLOURS[:default], io: io) end |
.pretty_backtrace(backtrace, limit: 10) ⇒ Object
Format backtrace entries with pretty file paths
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/tryouts/console.rb', line 184 def pretty_backtrace(backtrace, limit: 10) return [] unless backtrace&.any? backtrace.first(limit).map do |frame| # Split the frame to get file path and line info # Use non-greedy match and more specific pattern to prevent ReDoS if frame.match(/^([^:]+(?::[^:0-9][^:]*)*):(\d+):(.*)$/) file_part = $1 line_part = $2 method_part = $3 pretty_file = pretty_path(file_part) || File.basename(file_part) "#{pretty_file}:#{line_part}#{method_part}" else frame end end end |
.pretty_path(filepath) ⇒ Object
Converts an absolute file path to a path relative to the current working directory. This simplifies logging and error reporting by showing only the relevant parts of file paths instead of lengthy absolute paths.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/tryouts/console.rb', line 166 def pretty_path(filepath) return nil if filepath.nil? || filepath.empty? basepath = Dir.pwd begin relative_path = Pathname.new(filepath).relative_path_from(basepath) if relative_path.to_s.start_with?('..') File.basename(filepath) else relative_path.to_s end rescue ArgumentError # Handle cases where filepath cannot be relativized (e.g., empty paths, different roots) File.basename(filepath) end end |
.reverse(str, io = $stdout) ⇒ Object
93 94 95 96 97 |
# File 'lib/tryouts/console.rb', line 93 def reverse(str, io = $stdout) str = [style(ATTRIBUTES[:reverse], io: io), str, default_style(io)].join str.extend Console::InstanceMethods str end |
.style(*att, io: nil) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/tryouts/console.rb', line 117 def style(*att, io: nil) # Only output ANSI codes if colors are supported target_io = io || $stdout # Explicit color control via environment variables # FORCE_COLOR/CLICOLOR_FORCE override NO_COLOR return "\e[%sm" % att.join(';') if ENV['FORCE_COLOR'] || ENV['CLICOLOR_FORCE'] return '' if ENV['NO_COLOR'] # Check if we're outputting to a real TTY tty_output = (target_io.respond_to?(:tty?) && target_io.tty?) || ($stdout.respond_to?(:tty?) && $stdout.tty?) || ($stderr.respond_to?(:tty?) && $stderr.tty?) # If we have a real TTY, always use colors return "\e[%sm" % att.join(';') if tty_output # For environments like Claude Code where TTY detection fails but we want colors # Check if output appears to be redirected to a file/pipe if ENV['TERM'] && ENV['TERM'] != 'dumb' # Check if stdout/stderr look like they're redirected using file stats begin stdout_stat = $stdout.stat stderr_stat = $stderr.stat # If either stdout or stderr looks like a regular file or pipe, disable colors stdout_redirected = stdout_stat.file? || stdout_stat.pipe? stderr_redirected = stderr_stat.file? || stderr_stat.pipe? # Enable colors if neither appears redirected return "\e[%sm" % att.join(';') unless stdout_redirected || stderr_redirected rescue StandardError # If stat fails, fall back to enabling colors with TERM set return "\e[%sm" % att.join(';') end end # Default: no colors '' end |
.underline(str, io = $stdout) ⇒ Object
87 88 89 90 91 |
# File 'lib/tryouts/console.rb', line 87 def underline(str, io = $stdout) str = [style(ATTRIBUTES[:underline], io: io), str, default_style(io)].join str.extend Console::InstanceMethods str end |