This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Released under the MIT License.
|
||||
# Copyright, 2024, by Samuel Williams.
|
||||
|
||||
module Console
|
||||
module Terminal
|
||||
module Formatter
|
||||
class Failure
|
||||
KEY = :failure
|
||||
|
||||
def initialize(terminal)
|
||||
@terminal = terminal
|
||||
|
||||
@terminal[:exception_title] ||= @terminal.style(:red, nil, :bold)
|
||||
@terminal[:exception_detail] ||= @terminal.style(:yellow)
|
||||
@terminal[:exception_backtrace] ||= @terminal.style(:red)
|
||||
@terminal[:exception_backtrace_other] ||= @terminal.style(:red, nil, :faint)
|
||||
@terminal[:exception_message] ||= @terminal.style(:default)
|
||||
end
|
||||
|
||||
def format(event, output, prefix: nil, verbose: false, width: 80)
|
||||
title = event[:class]
|
||||
message = event[:message]
|
||||
backtrace = event[:backtrace]
|
||||
root = event[:root]
|
||||
|
||||
lines = message.lines.map(&:chomp)
|
||||
|
||||
output.puts " #{prefix}#{@terminal[:exception_title]}#{title}#{@terminal.reset}: #{lines.shift}"
|
||||
|
||||
lines.each do |line|
|
||||
output.puts " #{@terminal[:exception_detail]}#{line}#{@terminal.reset}"
|
||||
end
|
||||
|
||||
root_pattern = /^#{root}\// if root
|
||||
|
||||
backtrace&.each_with_index do |line, index|
|
||||
path, offset, message = line.split(":", 3)
|
||||
style = :exception_backtrace
|
||||
|
||||
# Make the path a bit more readable:
|
||||
if root_pattern and path.sub!(root_pattern, "").nil?
|
||||
style = :exception_backtrace_other
|
||||
end
|
||||
|
||||
output.puts " #{index == 0 ? "→" : " "} #{@terminal[style]}#{path}:#{offset}#{@terminal[:exception_message]} #{message}#{@terminal.reset}"
|
||||
end
|
||||
|
||||
if cause = event[:cause]
|
||||
format(cause, output, prefix: "Caused by ", verbose: verbose, width: width)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,58 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Released under the MIT License.
|
||||
# Copyright, 2020-2024, by Samuel Williams.
|
||||
|
||||
module Console
|
||||
module Terminal
|
||||
module Formatter
|
||||
class Progress
|
||||
KEY = :progress
|
||||
|
||||
BLOCK = [
|
||||
" ",
|
||||
"▏",
|
||||
"▎",
|
||||
"▍",
|
||||
"▌",
|
||||
"▋",
|
||||
"▊",
|
||||
"▉",
|
||||
"█",
|
||||
]
|
||||
|
||||
def initialize(terminal)
|
||||
@terminal = terminal
|
||||
@terminal[:progress_bar] ||= terminal.style(:blue, :white)
|
||||
end
|
||||
|
||||
def format(event, output, verbose: false, width: 80)
|
||||
current = event[:current].to_f
|
||||
total = event[:total].to_f
|
||||
value = current / total
|
||||
|
||||
# Clamp value to 1.0 to avoid rendering issues:
|
||||
if value > 1.0
|
||||
value = 1.0
|
||||
end
|
||||
|
||||
output.puts "#{@terminal[:progress_bar]}#{self.bar(value, width-10)}#{@terminal.reset} #{sprintf('%6.2f', value * 100)}%"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def bar(value, width)
|
||||
blocks = width * value
|
||||
full_blocks = blocks.floor
|
||||
partial_block = ((blocks - full_blocks) * BLOCK.size).floor
|
||||
|
||||
if partial_block.zero?
|
||||
BLOCK.last * full_blocks
|
||||
else
|
||||
"#{BLOCK.last * full_blocks}#{BLOCK[partial_block]}"
|
||||
end.ljust(width)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,42 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Released under the MIT License.
|
||||
# Copyright, 2019-2024, by Samuel Williams.
|
||||
|
||||
module Console
|
||||
module Terminal
|
||||
module Formatter
|
||||
# Format spawn events.
|
||||
class Spawn
|
||||
KEY = :spawn
|
||||
|
||||
def initialize(terminal)
|
||||
@terminal = terminal
|
||||
@terminal[:spawn_command] ||= @terminal.style(:blue, nil, :bold)
|
||||
end
|
||||
|
||||
def format(event, output, verbose: false, width: 80)
|
||||
environment, arguments, options = event.values_at(:environment, :arguments, :options)
|
||||
|
||||
arguments = arguments.flatten.collect(&:to_s)
|
||||
|
||||
output.puts "#{@terminal[:spawn_command]}#{arguments.join(' ')}#{@terminal.reset}#{chdir_string(options)}"
|
||||
|
||||
if verbose and environment
|
||||
environment.each do |key, value|
|
||||
output.puts "export #{key}=#{value}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def chdir_string(options)
|
||||
if options and chdir = options[:chdir]
|
||||
" in #{chdir}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user