Module: SpecSelectorUtil::UI

Included in:
SpecSelector
Defined in:
lib/spec_selector/ui.rb

Overview

The UI module contains methods used to bind and process user input.

Constant Summary collapse

DIRECTION_KEYS =
["\e[A", "\e[B"].freeze
TREE_NAVIGATION_KEYS =
["\r", "\x7F", "\e"].freeze
OPTION_KEYS =
[
  /^t$/i,
  /^f$/i,
  /^p$/i,
  /^q$/i,
  /^i$/i,
  /^r$/i,
  /^m$/i,
  /^c$/i,
  /^a$/i,
  /^v$/i,
  /^ $/,
  /^e$/i,
  /^o$/i
].freeze

Instance Method Summary collapse

Instance Method Details

#back ⇒ Object



100
101
102
103
104
105
106
# File 'lib/spec_selector/ui.rb', line 100

def back
  return if top_level?

  parent_list
  set_selected
  display_list
end

#bind_input ⇒ Object



47
48
49
50
51
52
# File 'lib/spec_selector/ui.rb', line 47

def bind_input
  input = user_input
  direction_keys(input) if DIRECTION_KEYS.include?(input)
  tree_nav_keys(input) if TREE_NAVIGATION_KEYS.include?(input)
  option_keys(input) if OPTION_KEYS.any? { |key| input.match?(key) }
end

#direction_keys(input) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/spec_selector/ui.rb', line 121

def direction_keys(input)
  exit_instruction_page if @instructions
  dir = input == "\e[A" ? -1 : 1
  @selector_index = (@selector_index + dir) % @list.length
  @selected = @list[@selector_index]
  @example_display ? display_example : display_list
end

#exit_instruction_page_only ⇒ Object



87
88
89
90
# File 'lib/spec_selector/ui.rb', line 87

def exit_instruction_page_only
  exit_instruction_page
  refresh_display
end

#exit_only ⇒ Object



24
25
26
27
# File 'lib/spec_selector/ui.rb', line 24

def exit_only
  q_to_exit
  loop { quit if user_input.match?(/^q$/i) }
end


42
43
44
45
# File 'lib/spec_selector/ui.rb', line 42

def navigate
  @selector_index = @list.index(@selected) || 0
  loop { bind_input }
end

#option_keys(input) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/spec_selector/ui.rb', line 142

def option_keys(input) # rubocop:disable Metrics/CyclomaticComplexity
  case input
  when /^t$/i
    top_fail!
  when /^ $/
    top_fail
  when /^p$/i
    toggle_passing
  when /^f$/i
    run_only_fails
  when /^q$/i
    quit
  when /^i$/i
    toggle_instructions
  when /^r$/i
    rerun
  when /^a$/i
    rerun_all
  when /^m$/i
    add_or_remove_from_filter
  when /^c$/i
    clear_filter
  when /^v$/i
    view_inclusion_filter
  when /^e$/i
    display_stderr_log
  when /^o$/i
    display_stdout_log
  end
end

#parent_list ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/spec_selector/ui.rb', line 108

def parent_list
  if @example_display
    @example_display = false
    @list = @active_map[@selected.example_group.[:block]]
  else
    data = parent_data(@selected.)
    p_data = parent_data(data)
    parent_key = p_data ? p_data[:block] : :top_level
    @list = @active_map[parent_key]
    @selected = @groups[data[:block]]
  end
end

#quit ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/spec_selector/ui.rb', line 54

def quit
  close_alt_buffer if @instructions
  clear_frame
  delete_filter_data
  reveal_cursor
  stderr_log.unlink
  stdout_log.unlink
  exit
end

#select_item ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/spec_selector/ui.rb', line 73

def select_item
  return if @example_display

  if example?(@selected)
    display_example
    return
  end

  @list = @active_map[@selected.[:block]]
  @selected = nil
  set_selected
  display_list
end

#selector ⇒ Object



29
30
31
32
33
# File 'lib/spec_selector/ui.rb', line 29

def selector
  set_selected
  @example_count > 1 ? display_list : display_example
  navigate
end

#set_selected ⇒ Object



35
36
37
38
39
40
# File 'lib/spec_selector/ui.rb', line 35

def set_selected
  @list ||= @active_map[:top_level]
  @selected ||= @list.first

  nil
end

#top_fail ⇒ Object



92
93
94
95
96
97
98
# File 'lib/spec_selector/ui.rb', line 92

def top_fail
  exit_instruction_page if @instructions
  return if @failed.empty?

  @selected = @failed.first
  display_example
end

#top_level_list ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/spec_selector/ui.rb', line 64

def top_level_list
  exit_instruction_page if @instructions
  @example_display = false
  @selected = nil
  @list = @active_map[:top_level]
  set_selected
  display_list
end

#tree_nav_keys(input) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/spec_selector/ui.rb', line 129

def tree_nav_keys(input)
  exit_instruction_page_only if @instructions && input != "\e"

  case input
  when "\r"
    select_item
  when "\x7F"
    back
  when "\e"
    top_level_list
  end
end

#user_input ⇒ Object



173
174
175
176
177
178
179
# File 'lib/spec_selector/ui.rb', line 173

def user_input
  input = $stdin.getch
  return input unless IO.select([$stdin], nil, nil, 0.000001)

  input << $stdin.read_nonblock(2)
  input
end