Module: WifiWand::Commands::OutputFormatter

Included in:
WifiWand::CommandLineInterface, OutputSupport
Defined in:
lib/wifi_wand/commands/output_formatter.rb

Instance Method Summary collapse

Instance Method Details

#colorize_network_name(text) ⇒ Object



44
# File 'lib/wifi_wand/commands/output_formatter.rb', line 44

def colorize_network_name(text) = text.gsub(/"([^"]*)"/) { |match| colorize_text(match, :cyan) }

#colorize_status(text) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/wifi_wand/commands/output_formatter.rb', line 33

def colorize_status(text)
  case text.to_s.downcase
  when /\b(true|on|connected|yes)\b/
    colorize_text(text, :green)
  when /\b(false|off|disconnected|no)\b/
    colorize_text(text, :red)
  else
    text
  end
end

#colorize_text(text, color = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wifi_wand/commands/output_formatter.rb', line 15

def colorize_text(text, color = nil)
  return text unless out_stream.respond_to?(:tty?) && out_stream.tty? && color

  color_codes = {
    red:         "\e[31m",
    green:       "\e[32m",
    yellow:      "\e[33m",
    blue:        "\e[34m",
    bright_blue: "\e[94m",
    cyan:        "\e[36m",
    magenta:     "\e[35m",
    bold:        "\e[1m",
    reset:       "\e[0m",
  }

  "#{color_codes[color]}#{text}#{color_codes[:reset]}"
end

#colorize_values(text) ⇒ Object



46
47
48
49
# File 'lib/wifi_wand/commands/output_formatter.rb', line 46

def colorize_values(text)
  # Match signal percentages, IPv4 addresses, and standalone signed/unsigned numbers.
  text.gsub(/-?\b\d+%|\b\d+\.\d+\.\d+\.\d+|-?\b\d+\b/) { |match| colorize_text(match, :bright_blue) }
end

#format_boolean_status(value, true_str: '✅ YES', false_str: '❌ NO', pending_str: '⏳ WAIT') ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wifi_wand/commands/output_formatter.rb', line 51

def format_boolean_status(value, true_str: '✅ YES', false_str: '❌ NO', pending_str: '⏳ WAIT')
  value = !!value unless value.nil? # convert non-Boolean non-nil values to true or false
  status_text, color = case value
                       when nil
                         [pending_str, :yellow]
                       when true
                         [true_str, :green]
                       when false
                         [false_str, :red]
  end

  colorize_text(status_text, color)
end

#format_internet_status(state, check_complete: false) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/wifi_wand/commands/output_formatter.rb', line 65

def format_internet_status(state, check_complete: false)
  status_text, color = case state
                       when ConnectivityStates::INTERNET_REACHABLE
                         ['✅ YES', :green]
                       when ConnectivityStates::INTERNET_UNREACHABLE
                         ['❌ NO', :red]
                       when ConnectivityStates::INTERNET_PENDING
                         ['⏳ WAIT', :yellow]
                       else
                         [check_complete ? '⚠️ UNKNOWN' : '⏳ WAIT', :yellow]
  end

  colorize_text(status_text, color)
end

#format_object(object) ⇒ Object



10
11
12
13
# File 'lib/wifi_wand/commands/output_formatter.rb', line 10

def format_object(object)
  require 'amazing_print'
  object_for_display(object).ai
end

#post_process(object) ⇒ Object



119
120
121
122
# File 'lib/wifi_wand/commands/output_formatter.rb', line 119

def post_process(object)
  display_object = object_for_display(object)
  post_processor ? post_processor.(display_object) : display_object
end

#post_processorObject



124
# File 'lib/wifi_wand/commands/output_formatter.rb', line 124

def post_processor = options.post_processor

#status_line(status_data) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/wifi_wand/commands/output_formatter.rb', line 80

def status_line(status_data)
  return colorize_text('WiFi: [status unavailable]', :yellow) if status_data.nil?

  wifi_status = format_boolean_status(status_data[:wifi_on], true_str: '✅ ON', false_str: '❌ OFF')
  dns_status = format_boolean_status(status_data[:dns_working])
  internet_status = format_internet_status(
    status_data[:internet_state],
    check_complete: status_data[:internet_check_complete]
  )

  # Format network name
  network_name = status_data[:network_name]
  network_text, network_color =
    if network_name == :pending
      ['WAIT', :yellow]
    elsif status_data.key?(:connected) && status_data[:connected].nil?
      ['UNKNOWN', :yellow]
    elsif status_data[:connected] == true && (network_name.nil? || network_name.to_s.empty?)
      [NetworkIdentity::SSID_UNAVAILABLE_LABEL, :yellow]
    elsif network_name.nil? || network_name.to_s.empty?
      ['[none]', :yellow]
    else
      [network_name.to_s, :cyan]
    end

  signal_quality = status_data[:signal_quality]
  signal_text = signal_quality ? " (#{colorize_values(signal_quality.to_s)})" : ''
  wifi_network_status = colorize_text(network_text, network_color) + signal_text

  line = "WiFi: #{wifi_status} | WiFi Network: #{wifi_network_status} | " \
    "DNS: #{dns_status} | Internet: #{internet_status}"

  if status_data[:captive_portal_login_required] == :yes
    line = "#{line} | #{colorize_text('⚠️ Captive Portal Login Required', :red)}"
  end

  line
end