Class: LanguageOperator::CLI::Formatters::LogStyle

Inherits:
Object
  • Object
show all
Defined in:
lib/language_operator/cli/formatters/log_style.rb

Overview

Centralized configuration for log level icons and colors

Single source of truth for all logging/notification output across the codebase. This ensures consistent styling and makes it easy to customize icons and colors for all log levels in one place.

Examples:

Using LogStyle with Pastel

pastel = Pastel.new
puts LogStyle.format(:success, "Operation completed", pastel)
# => "\e[32m✔ Operation completed\e[0m"

Getting icon and color separately

icon = LogStyle.icon(:error)    # => "✗"
color = LogStyle.color(:error)  # => :red

Constant Summary collapse

STYLES =

Style configuration for each log level

Returns:

  • (Hash)

    Mapping of log levels to icon and color

{
  debug: { icon: '', color: :dim },
  info: { icon: '', color: :cyan },
  success: { icon: '', color: :green },
  warn: { icon: '', color: :yellow },
  error: { icon: '', color: :red }
}.freeze

Class Method Summary collapse

Class Method Details

.ansi_icon(level) ⇒ String

Get ANSI color code for a level (for Logger compatibility)

Parameters:

  • level (Symbol, String)

    Log level

Returns:

  • (String)

    ANSI escape code with icon and color



119
120
121
122
123
124
125
126
# File 'lib/language_operator/cli/formatters/log_style.rb', line 119

def ansi_icon(level)
  level_sym = level.to_s.downcase.to_sym
  style = STYLES[level_sym] || STYLES[:info]
  icon = style[:icon]
  color_code = ansi_color_code(style[:color])

  "#{color_code}#{icon}\e[0m"
end

.color(level) ⇒ Symbol

Get the color for a log level

Parameters:

  • level (Symbol)

    Log level

Returns:

  • (Symbol)

    Color name (e.g., :red, :green)



59
60
61
# File 'lib/language_operator/cli/formatters/log_style.rb', line 59

def color(level)
  STYLES.dig(level, :color) || STYLES[:info][:color]
end

.detect_level_from_message(message) ⇒ Symbol

Detect log level from message content

Useful for inferring log level from message text when not explicitly provided.

Parameters:

  • message (String)

    Message text to analyze

Returns:

  • (Symbol)

    Detected log level



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/language_operator/cli/formatters/log_style.rb', line 79

def detect_level_from_message(message)
  case message
  when /error|fail|✗|❌/i
    :error
  when /warn|⚠/i
    :warn
  when /completed|finished|success|✔|✅/i
    :success
  when /debug|☢/i
    :debug
  else
    :info
  end
end

.emoji_to_level(emoji) ⇒ Symbol

Convert emoji to log level symbol

Parameters:

  • emoji (String)

    Emoji character

Returns:

  • (Symbol)

    Log level symbol



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/language_operator/cli/formatters/log_style.rb', line 98

def emoji_to_level(emoji)
  case emoji
  when /[☰ℹ]/
    :info
  when /[☢🔍]/
    :debug
  when //
    :warn
  when /[✗❌]/
    :error
  when /[✔✅]/
    :success
  else
    :info
  end
end

.format(level, message, pastel) ⇒ String

Format a message with icon and color for the given log level

Parameters:

  • level (Symbol)

    Log level (:debug, :info, :success, :warn, :error)

  • message (String)

    Message to format

  • pastel (Pastel)

    Pastel instance for applying colors

Returns:

  • (String)

    Formatted message with icon and color



39
40
41
42
43
44
45
# File 'lib/language_operator/cli/formatters/log_style.rb', line 39

def format(level, message, pastel)
  style = STYLES[level] || STYLES[:info]
  icon = style[:icon]
  color = style[:color]

  pastel.send(color, "#{icon} #{message}")
end

.icon(level) ⇒ String

Get the icon for a log level

Parameters:

  • level (Symbol)

    Log level

Returns:

  • (String)

    Icon character



51
52
53
# File 'lib/language_operator/cli/formatters/log_style.rb', line 51

def icon(level)
  STYLES.dig(level, :icon) || STYLES[:info][:icon]
end

.styled_icon(level, pastel) ⇒ String

Get a styled icon with ANSI color codes embedded

Parameters:

  • level (Symbol)

    Log level

  • pastel (Pastel)

    Pastel instance for applying colors

Returns:

  • (String)

    Colored icon with ANSI escape codes



68
69
70
71
# File 'lib/language_operator/cli/formatters/log_style.rb', line 68

def styled_icon(level, pastel)
  style = STYLES[level] || STYLES[:info]
  pastel.send(style[:color], style[:icon])
end