Browse Source

wrapper added

pagy
pvincent 10 months ago
parent
commit
a52ba18813
  1. 1
      app/controllers/scores_controller.rb
  2. 24
      lib/formatters/basic_formatter.rb
  3. 62
      lib/formatters/wrapper.rb

1
app/controllers/scores_controller.rb

@ -11,6 +11,7 @@ class ScoresController < ApplicationController
logger.debug 'this is a debug message'
logger.fatal 'FATAL'
logger.info @scores.inspect
logger.info File.basename($0)
end
# GET /scores/1

24
lib/formatters/basic_formatter.rb

@ -1,7 +1,12 @@
require_relative 'wrapper'
require 'io/console'
# Opinioned Rails custom formatter
class BasicFormatter < SemanticLogger::Formatters::Color
NAME_MAX_SIZE = 25
FOREMAN_PREFIX = 15
CONTENT_PREFIX = ' '.freeze
WRAP_PREFIX = ' > '.freeze
ANSI_DEBUG = "\e[90m".freeze
ANSI_INFO = SemanticLogger::AnsiColors::GREEN
@ -48,6 +53,10 @@ class BasicFormatter < SemanticLogger::Formatters::Color
message.rstrip!
message += "\n" unless message.starts_with?('Completed 5')
end
wrap_length = IO.console.winsize[1] - FOREMAN_PREFIX - before_message.length + CONTENT_PREFIX.length + 12
message = Wrapper.wrap("#{CONTENT_COLOR_MAP[log.level]}#{message.lstrip}", before_message(true), wrap_length)
"#{CONTENT_COLOR_MAP[log.level]}#{message}#{color_map.clear}"
end
@ -79,8 +88,15 @@ class BasicFormatter < SemanticLogger::Formatters::Color
private
def before_message
[name, level, tags, named_tags, duration, CONTENT_PREFIX].compact.join(' ')
def before_message(wrapped = false)
result = [name, level, tags, named_tags, duration, CONTENT_PREFIX].compact.join(' ')
if wrapped
[name, level, tags, named_tags, duration,
"#{color}#{WRAP_PREFIX}#{color_map.clear}"].compact.join(' ')
else
[name, level, tags, named_tags, duration,
CONTENT_PREFIX].compact.join(' ')
end
end
def backtrace(stack)
@ -88,4 +104,8 @@ class BasicFormatter < SemanticLogger::Formatters::Color
"\n#{before_message} #{ANSI_ERROR}#{stack.join("\n#{before_message} #{ANSI_ERROR}")}#{color_map.clear}"
end
def color_content
color
end
end

62
lib/formatters/wrapper.rb

@ -0,0 +1,62 @@
class Wrapper
def self.wrap(text, prefix = ' > ', length = 80)
pure = ''
ansi_code = {}
while (md = text.match(/\e\[\d+;?\d*m/))
pos = md.begin(0) + pure.length
pure += md.pre_match
text = md.post_match
append_in_hash(ansi_code, pos, md.match(0))
end
pure += text
offset = 0
ansi_extra = {}
rows = pure.length / length
last_code = nil
rows.times do |i|
pos = (i + 1) * length
last_code = last_ansi_by_range(ansi_code, last_code, offset, pos)
if last_code
append_in_hash(ansi_extra, pos, "\e[0m\n#{prefix}#{last_code}")
elsif pos < pure.length
append_in_hash(ansi_extra, pos, "\n#{prefix}")
end
offset = pos + 1
end
ansi_extra.each_pair do |k, v|
append_in_hash(ansi_code, k, v.first)
end
ansi_code = ansi_code.sort
final = pure
offset = 0
ansi_code.each do |k, v|
insert_text = v.join
final = final.insert(k + offset, insert_text)
offset += insert_text.length
end
final
end
def self.last_ansi_by_range(ansi_code, last_code, offset, pos)
pos.downto(offset) do |i|
next unless ansi_code.has_key?(i)
result = ansi_code[i].last
result = nil if result == "\e[0m"
return result
end
last_code
end
def self.append_in_hash(hash, key, value)
if hash.has_key?(key)
hash[key] << value
else
hash[key] = [value]
end
end
end
Loading…
Cancel
Save