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
103 104 105 106 107 |
# File 'lib/tryouts/console.rb', line 103 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
109 110 111 112 113 |
# File 'lib/tryouts/console.rb', line 109 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
79 80 81 82 83 |
# File 'lib/tryouts/console.rb', line 79 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
97 98 99 100 101 |
# File 'lib/tryouts/console.rb', line 97 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
156 157 158 |
# File 'lib/tryouts/console.rb', line 156 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
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/tryouts/console.rb', line 182 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.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/tryouts/console.rb', line 164 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
91 92 93 94 95 |
# File 'lib/tryouts/console.rb', line 91 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
115 116 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 |
# File 'lib/tryouts/console.rb', line 115 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
85 86 87 88 89 |
# File 'lib/tryouts/console.rb', line 85 def underline(str, io = $stdout) str = [style(ATTRIBUTES[:underline], io: io), str, default_style(io)].join str.extend Console::InstanceMethods str end |