Browse Source

visible_length(prefix)

main
pvincent 7 months ago
parent
commit
91363c8e56
  1. 1
      Gemfile
  2. 7
      Gemfile.lock
  3. 54
      lib/formatters/ansi_formatter.rb
  4. 6
      lib/formatters/ansi_wrapper.rb

1
Gemfile

@ -28,5 +28,4 @@ group :development do
gem 'rubocop-thread_safety'
gem 'ruby-lsp-rails'
gem 'turbo-rails'
gem 'web-console'
end

7
Gemfile.lock

@ -79,7 +79,6 @@ GEM
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.8)
bindex (0.8.1)
bootsnap (1.18.3)
msgpack (~> 1.2)
builder (3.2.4)
@ -291,11 +290,6 @@ GEM
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
web-console (4.2.1)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
bindex (>= 0.4.0)
railties (>= 6.0.0)
webrick (1.8.1)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
@ -336,7 +330,6 @@ DEPENDENCIES
stimulus-rails
tailwindcss-rails
turbo-rails
web-console
RUBY VERSION
ruby 3.1.2p20

54
lib/formatters/ansi_formatter.rb

@ -5,14 +5,16 @@ require 'io/console'
require 'amazing_print'
require 'json'
# wraps meanwhile takes care of ansi colors
class AnsiFormatter < SemanticLogger::Formatters::Color
include AnsiColors
ANSI_DEBUG = TEXT_GRAY_400
ANSI_INFO = TEXT_GRAY_100
ANSI_WARN = BG_YELLOW + TEXT_BLACK
ANSI_ERROR = BG_RED + TEXT_BLACK
ANSI_FATAL = DARK_BG_RED + TEXT_BLACK
ANSI_DEBUG = CLEAR + TEXT_GRAY_400
ANSI_INFO = CLEAR + TEXT_GRAY_100
ANSI_WARN = CLEAR + BG_YELLOW + TEXT_BLACK
ANSI_ERROR = CLEAR + BG_RED + TEXT_WHITE
ANSI_FATAL = CLEAR + BG_MAGENTA + BOLD + TEXT_WHITE
NAME_MAX_SIZE = 20
def initialize
super(color_map: ColorMap.new(
@ -24,12 +26,6 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
))
end
def message
return unless log.message
colorize(log.message, color_map[log.level])
end
def call(log, logger)
self.log = log
self.logger = logger
@ -37,15 +33,43 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
wrap_level(level, message, payload, exception)
end
def origin
origin = log.name.truncate(NAME_MAX_SIZE).center(NAME_MAX_SIZE)
colorize(origin, AnsiColors::TEXT_CYAN)
end
def message
return unless log.message
colorize(log.message)
end
def exception
return unless log.exception
clazz = "#{log.exception.class}\n"
message = log.exception.message.chomp('')
backtrace = clean_backtrace(log.exception.backtrace)
"#{colorize(clazz, ANSI_FATAL)}#{colorize(message, ANSI_ERROR)}#{backtrace}"
end
private
def colorize(text, color)
"#{color}#{text}#{color_map.clear}"
def clean_backtrace(backtrace)
root_path = Rails.root.to_s
backtrace = backtrace.select { |line| line.starts_with?(root_path) }
backtrace = backtrace.map { |line| line.delete_prefix("#{root_path}/") }
backtrace.compact.join("\n")
end
def colorize(text, tint = color)
"#{tint}#{text}#{AnsiColors::CLEAR}"
end
def wrap_level(level, *items)
prefix = " #{colorize('>', color)} "
continuation = " #{colorize('#', color)} "
prefix = "#{origin} #{colorize('>')} "
continuation = "#{origin} #{colorize('#')} "
items.map do |item|
AnsiWrapper.wrap(item, 100, prefix, continuation)
end.compact.join("\n") # FIXME: previously was: join(' ')

6
lib/formatters/ansi_wrapper.rb

@ -5,14 +5,14 @@ class AnsiWrapper
ANSI_RESET = "\e[0m".freeze
def self.wrap(text, length, prefix = '', continuation = prefix)
if prefix.length != continuation.length
raise "continuation <#{continuation}> should have the same length as prefix <#{prefix}>"
if visible_length(prefix) != visible_length(continuation)
raise "continuation <#{continuation.inspect}> should have the same length as prefix <#{prefix.inspect}>"
end
return unless text
text = text.gsub("\t", ' ' * TAB_TO_SPACES)
lines = split_text_to_lines(text, length - prefix.length)
lines = split_text_to_lines(text, length - visible_length(prefix))
lines = inject_continuation_and_ansi_colors_to_lines(lines, prefix, continuation)
lines.join("\n")
end

Loading…
Cancel
Save