Class: Binocs::TUI::FilterMenu

Inherits:
Window
  • Object
show all
Defined in:
lib/binocs/tui/filter_menu.rb

Constant Summary collapse

FILTERS =
[
  { key: :method, label: 'HTTP Method', options: %w[GET POST PUT PATCH DELETE] },
  { key: :status, label: 'Status Code', options: %w[2xx 3xx 4xx 5xx] },
  { key: :has_exception, label: 'Has Exception', options: [true, false] },
].freeze

Instance Attribute Summary collapse

Attributes inherited from Window

#height, #left, #top, #width, #win

Instance Method Summary collapse

Methods inherited from Window

#clear, #close, #copy_to_clipboard, #draw_box, #noutrefresh, #refresh, #resize, #write, #write_centered

Constructor Details

#initialize(height:, width:, top:, left:) ⇒ FilterMenu

Returns a new instance of FilterMenu.



14
15
16
17
18
19
20
# File 'lib/binocs/tui/filter_menu.rb', line 14

def initialize(height:, width:, top:, left:)
  super
  @selected_index = 0
  @selected_filters = {}
  @expanded_filter = nil
  @option_index = 0
end

Instance Attribute Details

#selected_filtersObject (readonly)

Returns the value of attribute selected_filters.



12
13
14
# File 'lib/binocs/tui/filter_menu.rb', line 12

def selected_filters
  @selected_filters
end

Instance Method Details

#backObject



61
62
63
64
65
66
67
68
69
# File 'lib/binocs/tui/filter_menu.rb', line 61

def back
  if @expanded_filter
    @expanded_filter = nil
    @option_index = 0
    false
  else
    true
  end
end

#drawObject



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/binocs/tui/filter_menu.rb', line 71

def draw
  clear
  draw_box('Filters')

  y = 2

  FILTERS.each_with_index do |filter, i|
    is_selected = i == @selected_index && @expanded_filter.nil?
    is_expanded = i == @expanded_filter

    # Draw filter label
    label = "#{filter[:label]}: "
    current_value = @selected_filters[filter[:key]]
    value_text = current_value ? current_value.to_s : 'Any'

    if is_selected
      @win.attron(Curses.color_pair(Colors::SELECTED)) do
        @win.setpos(y, 2)
        @win.addstr(' ' * (@width - 4))
      end
      write(y, 3, label, Colors::SELECTED, Curses::A_BOLD)
      write(y, 3 + label.length, value_text, Colors::SELECTED)
      write(y, @width - 5, '', Colors::SELECTED)
    else
      write(y, 3, label, Colors::MUTED)
      color = current_value ? Colors::HEADER : Colors::MUTED
      write(y, 3 + label.length, value_text, color)
    end

    y += 1

    # Draw expanded options
    if is_expanded
      filter[:options].each_with_index do |option, oi|
        is_option_selected = oi == @option_index
        is_current = @selected_filters[filter[:key]] == option

        if is_option_selected
          @win.attron(Curses.color_pair(Colors::SELECTED)) do
            @win.setpos(y, 4)
            @win.addstr(' ' * (@width - 8))
          end
          write(y, 5, option.to_s, Colors::SELECTED)
          write(y, @width - 7, '', Colors::SELECTED) if is_current
        else
          color = is_current ? Colors::STATUS_SUCCESS : Colors::NORMAL
          write(y, 5, option.to_s, color)
          write(y, @width - 7, '', Colors::STATUS_SUCCESS) if is_current
        end
        y += 1
      end

      # Clear option
      is_clear_selected = @option_index >= filter[:options].length
      if is_clear_selected
        @win.attron(Curses.color_pair(Colors::SELECTED)) do
          @win.setpos(y, 4)
          @win.addstr(' ' * (@width - 8))
        end
        write(y, 5, '(Clear)', Colors::SELECTED)
      else
        write(y, 5, '(Clear)', Colors::MUTED, Curses::A_DIM)
      end
      y += 1
    end

    y += 1
    break if y >= @height - 4
  end

  # Footer
  draw_footer

  refresh
end

#move_downObject



34
35
36
37
38
39
40
41
# File 'lib/binocs/tui/filter_menu.rb', line 34

def move_down
  if @expanded_filter
    max = current_filter[:options].length
    @option_index = [@option_index + 1, max].min # +1 for "Clear" option
  else
    @selected_index = [@selected_index + 1, FILTERS.length - 1].min
  end
end

#move_upObject



26
27
28
29
30
31
32
# File 'lib/binocs/tui/filter_menu.rb', line 26

def move_up
  if @expanded_filter
    @option_index = [@option_index - 1, 0].max
  else
    @selected_index = [@selected_index - 1, 0].max
  end
end

#selectObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/binocs/tui/filter_menu.rb', line 43

def select
  if @expanded_filter
    filter = current_filter
    if @option_index >= filter[:options].length
      # Clear option selected
      @selected_filters.delete(filter[:key])
    else
      value = filter[:options][@option_index]
      @selected_filters[filter[:key]] = value
    end
    @expanded_filter = nil
    @option_index = 0
  else
    @expanded_filter = @selected_index
    @option_index = 0
  end
end

#set_filters(filters) ⇒ Object



22
23
24
# File 'lib/binocs/tui/filter_menu.rb', line 22

def set_filters(filters)
  @selected_filters = filters.dup
end