Browse Source

refactor1

main
pvincent 5 months ago
parent
commit
8640502db1
  1. 3
      lib/formatters/ansi_colors.rb
  2. 14
      lib/formatters/ansi_common.rb
  3. 2
      lib/formatters/ansi_dimensions.rb
  4. 17
      lib/formatters/ansi_formatter.rb
  5. 18
      lib/formatters/ansi_wrapper.rb
  6. 6
      lib/hot_constants/live_constants.rb

3
lib/formatters/ansi_colors.rb

@ -1,4 +1,7 @@
# common definitions and constants
module AnsiColors module AnsiColors
ANSI_REGEX = /\e\[[0-9;]*m/ # TODO: support for \x1b and \033
# FORMAT # FORMAT
CLEAR = "\e[0m".freeze CLEAR = "\e[0m".freeze
BOLD = "\e[1m".freeze BOLD = "\e[1m".freeze

14
lib/formatters/ansi_common.rb

@ -1,14 +0,0 @@
require_relative 'ansi_colors'
# gather common definitions and functions
class AnsiCommon
ANSI_REGEX = /\e\[[0-9;]*m/ # TODO: support for \x1b and \033
def self.ansi_trace(trace, symbol)
match = trace.match(/(↳ )?(.*:\d+)(:in `)?(.*'?)/) # only m2(=file) and m4(=optional function) are useful
return trace unless match
_, m2, _, m4 = match.captures
"#{symbol} #{m2} #{AnsiColors::BOLD}#{m4.chop}#{AnsiColors::CLEAR}"
end
end

2
lib/formatters/ansi_dimensions.rb

@ -1,6 +1,6 @@
require 'ostruct' require 'ostruct'
# extra dimensions to customize ansi_loggingg
# extra dimensions for customizing the logging format
module AnsiDimensions module AnsiDimensions
def self.new(rails: '╣x╠', before: 0, after: 0, terminus: false) def self.new(rails: '╣x╠', before: 0, after: 0, terminus: false)
OpenStruct.new(rails:, before:, after:, terminus:) # rubocop:disable Style/OpenStructUse OpenStruct.new(rails:, before:, after:, terminus:) # rubocop:disable Style/OpenStructUse

17
lib/formatters/ansi_formatter.rb

@ -1,7 +1,6 @@
require_relative 'ansi_wrapper' require_relative 'ansi_wrapper'
require_relative 'ansi_colors' require_relative 'ansi_colors'
require_relative 'ansi_dimensions' require_relative 'ansi_dimensions'
require_relative 'ansi_common'
require 'io/console' require 'io/console'
require 'amazing_print' require 'amazing_print'
@ -49,6 +48,14 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
private private
def ansi_trace(trace, symbol)
match = trace.match(/(↳ )?(.*:\d+)(:in `)?(.*'?)/) # only m2(=file) and m4(=optional function) are useful
return trace unless match
_, m2, _, m4 = match.captures
"#{symbol} #{m2} #{BOLD}#{m4.chop}#{CLEAR}"
end
def two_captures_last_as_bold(message, regex) def two_captures_last_as_bold(message, regex)
match = message.match(regex) match = message.match(regex)
return "unmatched: #{message}" unless match return "unmatched: #{message}" unless match
@ -122,13 +129,13 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
unbold!(log.message) unbold!(log.message)
if log.message.starts_with?('↳ ') if log.message.starts_with?('↳ ')
log.message = AnsiCommon.ansi_trace(log.message, '⇄')
log.message = ansi_trace(log.message, '⇄')
else else
sql_match = log.message.match(/\s+\[\[.*\]\]$/) sql_match = log.message.match(/\s+\[\[.*\]\]$/)
if sql_match if sql_match
sql_command = sql_match.pre_match sql_command = sql_match.pre_match
sql_type, sql_entry = sql_command.split("\e[0m") sql_type, sql_entry = sql_command.split("\e[0m")
sql_color_entry = sql_entry.match(AnsiCommon::ANSI_REGEX).to_s
sql_color_entry = sql_entry.match(ANSI_REGEX).to_s
sql_args = JSON.parse(sql_match.to_s).map(&:last) sql_args = JSON.parse(sql_match.to_s).map(&:last)
sql_args.each_with_index do |val, index| sql_args.each_with_index do |val, index|
sql_arg = val.inspect sql_arg = val.inspect
@ -154,7 +161,7 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
end end
def draw_fatal(char = CHAR_FATAL) def draw_fatal(char = CHAR_FATAL)
AnsiColors::BG_MAGENTA + AnsiColors::BOLD + AnsiColors::TEXT_WHITE + char + AnsiColors::CLEAR
BG_MAGENTA + BOLD + TEXT_WHITE + char + CLEAR
end end
def origin = colorize(centerize(log.name), TEXT_CYAN) def origin = colorize(centerize(log.name), TEXT_CYAN)
@ -166,7 +173,7 @@ class AnsiFormatter < SemanticLogger::Formatters::Color
def stackisize(items) def stackisize(items)
return '' if items.empty? return '' if items.empty?
traces = items.map { |item| AnsiCommon.ansi_trace(item, '➟') }
traces = items.map { |item| ansi_trace(item, '➟') }
"\n#{traces.join("\n")}" "\n#{traces.join("\n")}"
end end

18
lib/formatters/ansi_wrapper.rb

@ -1,5 +1,9 @@
require_relative 'ansi_colors'
# AnsiWrapper cares about Ansi Colour Code \e[... # AnsiWrapper cares about Ansi Colour Code \e[...
class AnsiWrapper class AnsiWrapper
include AnsiColors
TAB_TO_SPACES = 2 TAB_TO_SPACES = 2
def self.wrap(text, length, prefix = '', continuation = prefix) def self.wrap(text, length, prefix = '', continuation = prefix)
@ -19,21 +23,21 @@ class AnsiWrapper
last_ansi = '' last_ansi = ''
lines.each_with_index.map do |line, index| lines.each_with_index.map do |line, index|
current = index.zero? ? prefix : continuation current = index.zero? ? prefix : continuation
current += last_ansi unless last_ansi.empty? || last_ansi == AnsiColors::CLEAR
current += last_ansi unless last_ansi.empty? || last_ansi == CLEAR
current += line current += line
last_ansi = scan_for_actual_ansi(line, last_ansi) last_ansi = scan_for_actual_ansi(line, last_ansi)
current += AnsiColors::CLEAR if last_ansi.empty? || last_ansi != AnsiColors::CLEAR
current += CLEAR if last_ansi.empty? || last_ansi != CLEAR
current current
end end
end end
private_class_method def self.scan_for_actual_ansi(line, last_ansi) private_class_method def self.scan_for_actual_ansi(line, last_ansi)
line.scan(AnsiCommon::ANSI_REGEX).each do |match|
line.scan(ANSI_REGEX).each do |match|
ansi_code = match.to_s ansi_code = match.to_s
if ansi_code == AnsiColors::CLEAR
last_ansi = AnsiColors::CLEAR
if ansi_code == CLEAR
last_ansi = CLEAR
else else
last_ansi += ansi_code last_ansi += ansi_code
end end
@ -52,13 +56,13 @@ class AnsiWrapper
private_class_method def self.visible_length(line) private_class_method def self.visible_length(line)
raise 'line should not contain carriage return character!' if line.match "\n" raise 'line should not contain carriage return character!' if line.match "\n"
ansi_code_length = line.scan(AnsiCommon::ANSI_REGEX).map(&:length).sum
ansi_code_length = line.scan(ANSI_REGEX).map(&:length).sum
line.length - ansi_code_length line.length - ansi_code_length
end end
# TODO: might be refactored with less complexity # TODO: might be refactored with less complexity
private_class_method def self.visible_split(line, length, stack = '') # rubocop:disable Metrics/AbcSize,Metrics/MethodLength private_class_method def self.visible_split(line, length, stack = '') # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
before, ansi_code, after = line.partition(AnsiCommon::ANSI_REGEX)
before, ansi_code, after = line.partition(ANSI_REGEX)
stack_length = visible_length(stack) stack_length = visible_length(stack)
visible_length = before.length + stack_length visible_length = before.length + stack_length
if visible_length == length if visible_length == length

6
lib/hot_constants/live_constants.rb

@ -10,8 +10,6 @@ module LiveConstants
def log_active_record = load_boolean('LOG_ACTIVE_RECORD', true) def log_active_record = load_boolean('LOG_ACTIVE_RECORD', true)
def log_action_view = load_boolean('LOG_ACTION_VIEW', true) def log_action_view = load_boolean('LOG_ACTION_VIEW', true)
def load_boolean(key, default) = HOTENV.fetch(key, default).to_s.downcase == 'true'
def reload! def reload!
HOTENV.replace Dotenv.parse HOTENV.replace Dotenv.parse
# LOGGER.warn 'reloaded!' # LOGGER.warn 'reloaded!'
@ -19,6 +17,10 @@ module LiveConstants
ActiveRecord::Base.logger.level = log_active_record ? :debug : :fatal ActiveRecord::Base.logger.level = log_active_record ? :debug : :fatal
ActionView::Base.logger.level = log_action_view ? :debug : :fatal ActionView::Base.logger.level = log_action_view ? :debug : :fatal
end end
private
def load_boolean(key, default) = HOTENV.fetch(key, default).to_s.downcase == 'true'
end end
reload! reload!

Loading…
Cancel
Save