Class: WifiWand::Commands::Status

Inherits:
Base
  • Object
show all
Defined in:
lib/wifi_wand/commands/status.rb

Constant Summary

Constants inherited from Base

Base::DEFAULT_INVOCATION_OPTIONS

Instance Attribute Summary

Attributes inherited from Base

#cli, #metadata

Instance Method Summary collapse

Methods inherited from Base

#aliases, allow_invocation_options, allowed_invocation_options, #bind, binding_sources, binds, command_metadata, declared_metadata, #help_text, #initialize, #validate_options

Constructor Details

This class inherits a constructor from WifiWand::Commands::Base

Instance Method Details

#call(*args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/wifi_wand/commands/status.rb', line 22

def call(*args)
  validate_max_arguments!(args, 0)

  progress_mode = output_support.status_progress_mode
  current_snapshot = { wifi_on: nil, internet_state: ConnectivityStates::INTERNET_PENDING }
  last_visible_length = 0
  inline_progress_printed = false
  saw_progress_error = false

  progress_callback = if progress_mode == :inline
    ->(update) do
      if update.nil?
        saw_progress_error = true
        next
      end

      current_snapshot.merge!(update)
      rendered = output_support.status_line(current_snapshot)
      next if rendered.to_s.empty?

      visible_length = output_support.display_width(rendered)
      padding = [last_visible_length - visible_length, 0].max
      padded_render = padding.zero? ? rendered : "#{rendered}#{' ' * padding}"

      prefix = inline_progress_printed ? "\r" : ''
      out_stream.print("#{prefix}#{padded_render}")
      out_stream.flush if out_stream.respond_to?(:flush)

      last_visible_length = visible_length
      inline_progress_printed = true
    end
  end

  progress_callback&.call(current_snapshot.dup)

  status_data = model.status_line_data(progress_callback: progress_callback)

  if progress_mode == :inline
    if inline_progress_printed
      if saw_progress_error || status_data.nil?
        rendered = output_support.status_line(status_data)
        if rendered.to_s.empty?
          out_stream.puts
        else
          visible_length = output_support.display_width(rendered)
          padding = [last_visible_length - visible_length, 0].max

          out_stream.puts "\r#{rendered}#{' ' * padding}"
        end
      else
        out_stream.puts
      end
    else
      rendered = output_support.status_line(status_data)
      out_stream.puts(rendered) unless rendered.to_s.empty?
    end
  end

  if interactive_mode
    out_stream.puts output_support.status_line(status_data) if progress_mode == :none
    nil
  else
    if progress_mode == :none
      if status_data.nil?
        rendered = output_support.status_line(status_data)
        out_stream.puts(rendered) unless rendered.to_s.empty?
      else
        output_support.handle_output(status_data, -> { output_support.status_line(status_data) })
      end
    end
    raise WifiWand::StatusUnavailableError if status_data.nil?

    status_data
  end
end