esphome 1.2.2 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 29b56d539f72912af49e49855d3439083d3a390f13a4011c9092df388624d42d
4
- data.tar.gz: f5460983572f1590a28ceb2fd07ce760e17b3ba407772b66536f577cd670a182
3
+ metadata.gz: 20189bddab6e8f38b3ec20aaf3f110b4938c609f507b5aaa4b7c1a08e3fcea59
4
+ data.tar.gz: e316590bf254ba38d6d9af9cdceb251b7a250e99a284a6a1b5abdf92ad1c25fa
5
5
  SHA512:
6
- metadata.gz: eaef947a2ab31fc23a939d36900dd994582837d654ed8fa09c7a914c6df8c5044cebee7155aa233c6ab1d96143d4b4cc09deddeff2fbfa7452be4f40e275f237
7
- data.tar.gz: 5bee0eb4684bdd9f86446894f059d6e73ec2bc93584b03822bfed24f9c823ec8bed010790fe45c322b890f35106b99be54cec1fc7897688b7eb9df9440e399ce
6
+ metadata.gz: 17c2dd17dcea9be45136bcad9f85ae6e1e7028d188f8ee4449e643522f503b3d10bee068dfab1f193b7a1a7d2f0f7a77dd08cd17969eec0298bd29c93e5907fc
7
+ data.tar.gz: cb3dca8372d8f3fbe7753623c9857551e0722d41bfd7bba556954901e560c6ac8d63f666a5295b97294b10d1e454f654239f4d4b8962d6e32a632a633e40d65c
@@ -9,28 +9,70 @@ module ESPHome
9
9
  private
10
10
 
11
11
  def prompt_form(title, initial_value:, suffix:, field_width:)
12
- field, form, win = build_form_dialog(title, suffix, field_width)
12
+ max_length = field_width
13
+ field_width = form_field_width(suffix, max_length)
14
+ return if field_width <= 0
15
+
16
+ field, form, win, sub = build_form_dialog(title, suffix, field_width, max_length:)
13
17
  return unless win
14
18
 
15
- field.set_buffer(0, initial_value) if initial_value
16
- form.post
17
- win.box(0, 0)
18
- win.setpos(0, 2)
19
- safe_addstr(win, " #{title} ")
20
- if suffix
21
- win.setpos(1, field_width + 3)
22
- safe_addstr(win, suffix)
23
- end
24
- win.setpos(1, 2)
25
- win.refresh
19
+ selected = initial_value && !initial_value.empty?
20
+ post_form_dialog(field, form, win, title, suffix, field_width, initial_value, selected)
26
21
 
27
22
  loop do
28
- case (ch = win.getch)
23
+ ch = (dialog_fits?(win) ? win : cli.win).getch
24
+ if cli.resize_pending? || ch == Curses::KEY_RESIZE
25
+ cli.resize_display
26
+ new_width = form_field_width(suffix, max_length)
27
+ width = [form_window_width(title, suffix, new_width), cli.win.maxx].min
28
+ if new_width.positive? && cli.win.maxy >= 3 &&
29
+ (new_width != field_width || win.maxx != width || win.maxy != 3)
30
+ form.driver(Curses::REQ_VALIDATION)
31
+ value = field.buffer(0)
32
+ cursor = form_cursor_position(form)
33
+ form.unpost
34
+ sub.close
35
+ win.close
36
+ field_width = new_width
37
+ field, form, win, sub = build_form_dialog(title, suffix, field_width, max_length:)
38
+ post_form_dialog(field, form, win, title, suffix, field_width, value, selected)
39
+ cursor.times { form.driver(Curses::REQ_NEXT_CHAR) }
40
+ win.refresh
41
+ elsif dialog_fits?(win)
42
+ position_dialog_window(win)
43
+ sub.move(win.begy + 1, win.begx + 2)
44
+ win.touch
45
+ win.refresh
46
+ end
47
+ next if ch == Curses::KEY_RESIZE
48
+ end
49
+
50
+ if selected && (ch == Curses::KEY_LEFT || ch == Curses::KEY_RIGHT ||
51
+ ch == Curses::KEY_BACKSPACE || ch == Curses::KEY_DC ||
52
+ ch == 8 || ch == 127 || (ch.is_a?(String) && ch.match?(/[[:print:]]/)) ||
53
+ (ch.is_a?(Integer) && ch.between?(32, 255)))
54
+ selected = false
55
+ field.set_fore(Curses::A_NORMAL)
56
+ field.set_back(Curses::A_UNDERLINE)
57
+ case ch
58
+ when Curses::KEY_LEFT
59
+ form.driver(Curses::REQ_BEG_FIELD)
60
+ next
61
+ when Curses::KEY_RIGHT
62
+ form.driver(Curses::REQ_END_FIELD)
63
+ next
64
+ else
65
+ form.driver(Curses::REQ_CLR_FIELD)
66
+ next if [Curses::KEY_BACKSPACE, Curses::KEY_DC, 8, 127].include?(ch)
67
+ end
68
+ end
69
+
70
+ case ch
29
71
  when Curses::KEY_RIGHT
30
72
  form.driver(Curses::REQ_NEXT_CHAR)
31
73
  when Curses::KEY_LEFT
32
74
  form.driver(Curses::REQ_PREV_CHAR)
33
- when Curses::KEY_BACKSPACE
75
+ when Curses::KEY_BACKSPACE, 8, 127
34
76
  form.driver(Curses::REQ_DEL_PREV)
35
77
  when Curses::KEY_DC
36
78
  form.driver(Curses::REQ_DEL_CHAR)
@@ -40,7 +82,7 @@ module ESPHome
40
82
  return nil if value.empty?
41
83
 
42
84
  return value
43
- when 27, Curses::KEY_RESIZE
85
+ when 27
44
86
  return nil
45
87
  when nil
46
88
  next
@@ -52,21 +94,60 @@ module ESPHome
52
94
  end
53
95
  ensure
54
96
  form&.unpost
97
+ sub&.close
55
98
  win&.close
56
99
  end
57
100
 
58
- def build_form_dialog(title, suffix, field_width)
59
- width = field_width + 4 + (suffix ? suffix.length + 1 : 0)
60
- win = build_dialog_window(3, width, title)
101
+ def form_field_width(suffix, max_length)
102
+ [max_length, cli.win&.maxx.to_i - 4 - (suffix ? suffix.length + 1 : 0)].min
103
+ end
104
+
105
+ def form_cursor_position(form)
106
+ position = 0
107
+ loop do
108
+ form.driver(Curses::REQ_PREV_CHAR)
109
+ position += 1
110
+ end
111
+ rescue Curses::RequestDeniedError
112
+ position
113
+ end
114
+
115
+ def post_form_dialog(field, form, win, title, suffix, field_width, value, selected)
116
+ field.set_buffer(0, value) if value
117
+ field.set_fore(selected ? Curses::A_REVERSE : Curses::A_NORMAL)
118
+ field.set_back(Curses::A_UNDERLINE)
119
+ form.post
120
+ win.box(0, 0)
121
+ win.setpos(0, 2)
122
+ safe_addstr(win, " #{title} "[0, win.maxx - 4])
123
+ if suffix
124
+ win.setpos(1, field_width + 3)
125
+ safe_addstr(win, suffix)
126
+ end
127
+ win.setpos(1, 2)
128
+ win.refresh
129
+ end
130
+
131
+ def build_form_dialog(title, suffix, field_width, max_length: field_width)
132
+ width = form_window_width(title, suffix, field_width)
133
+ win = build_dialog_window(3, width, title, fit_width: true)
61
134
  return [nil, nil, nil] unless win
62
135
 
63
136
  field = Curses::Field.new(1, field_width, 0, 0, 0, 0)
64
137
  field.set_back(Curses::A_UNDERLINE)
65
138
  field.opts_off(Curses::O_AUTOSKIP)
139
+ field.opts_off(Curses::O_BLANK)
140
+ field.opts_off(Curses::O_STATIC)
141
+ field.set_max(max_length)
66
142
  form = Curses::Form.new([field])
67
143
  form.set_win(win)
68
- form.set_sub(win.derwin(1, field_width, 1, 2))
69
- [field, form, win]
144
+ sub = win.derwin(1, field_width, 1, 2)
145
+ form.set_sub(sub)
146
+ [field, form, win, sub]
147
+ end
148
+
149
+ def form_window_width(title, suffix, field_width)
150
+ [field_width + 4 + (suffix ? suffix.length + 1 : 0), title.length + 6].max
70
151
  end
71
152
  end
72
153
 
@@ -13,16 +13,24 @@ module ESPHome
13
13
 
14
14
  index = options.index(current) || 0
15
15
  loop do
16
- render_select_options(win, title, options, index)
16
+ visible = dialog_fits?(win)
17
+ render_select_options(win, title, options, index) if visible
17
18
 
18
- case win.getch
19
+ ch = (visible ? win : cli.win).getch
20
+ if cli.resize_pending? || ch == Curses::KEY_RESIZE
21
+ cli.resize_display
22
+ position_dialog_window(win) if dialog_fits?(win)
23
+ next if ch == Curses::KEY_RESIZE
24
+ end
25
+
26
+ case ch
19
27
  when Curses::Key::UP
20
28
  index = (index - 1) % options.length
21
29
  when Curses::Key::DOWN
22
30
  index = (index + 1) % options.length
23
31
  when "\n".ord
24
32
  return options[index]
25
- when 27, Curses::KEY_RESIZE
33
+ when 27
26
34
  return nil
27
35
  when nil
28
36
  next
@@ -32,6 +40,16 @@ module ESPHome
32
40
  win&.close
33
41
  end
34
42
 
43
+ def dialog_fits?(win)
44
+ win.maxx <= cli.win.maxx && win.maxy <= cli.win.maxy
45
+ end
46
+
47
+ def position_dialog_window(win)
48
+ top = (index + Monitor::HEADER_ROWS).clamp(0, cli.win.maxy - win.maxy)
49
+ left = (cli.name_width + 3).clamp(0, cli.win.maxx - win.maxx)
50
+ win.move(top, left)
51
+ end
52
+
35
53
  def render_select_options(win, title, options, index)
36
54
  win.clear
37
55
  win.box(0, 0)
@@ -48,11 +66,12 @@ module ESPHome
48
66
  win.refresh
49
67
  end
50
68
 
51
- def build_dialog_window(height, width, title)
69
+ def build_dialog_window(height, width, title, fit_width: false)
52
70
  root = cli.win
53
71
  return unless root
54
72
 
55
73
  width = [width, title.length + 6].max
74
+ width = [width, root.maxx].min if fit_width
56
75
  return if width > root.maxx || height > root.maxy
57
76
 
58
77
  top = (index + Monitor::HEADER_ROWS).clamp(0, root.maxy - height)
@@ -6,6 +6,14 @@ module ESPHome
6
6
  module Cli
7
7
  module Entities
8
8
  class Text < Form
9
+ def length_range
10
+ __getobj__.length_range
11
+ end
12
+
13
+ def initial_value
14
+ state
15
+ end
16
+
9
17
  def command(value)
10
18
  cli.info("Setting #{object_id_} to #{value}")
11
19
  super
@@ -250,6 +250,12 @@ module ESPHome
250
250
  @resize_pending
251
251
  end
252
252
 
253
+ def resize_display
254
+ @resize_pending = true
255
+ process_ui_events
256
+ render_all if @sub_active
257
+ end
258
+
253
259
  private
254
260
 
255
261
  def parse_ansi_and_render(line)
@@ -389,8 +395,10 @@ module ESPHome
389
395
  if @resize_pending
390
396
  if @screen_initialized
391
397
  rows, cols = IO.console.winsize
392
- Curses.resizeterm(rows, cols)
393
- @win&.resize(rows, cols)
398
+ # resizeterm queues KEY_RESIZE even when the size is unchanged.
399
+ # A popup consumes that key, so only resize when necessary.
400
+ Curses.resizeterm(rows, cols) if Curses.lines != rows || Curses.cols != cols
401
+ @win.resize(rows, cols) if @win && (@win.maxy != rows || @win.maxx != cols)
394
402
  @win&.clear
395
403
  else
396
404
  close_screen
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ESPHome
4
- VERSION = "1.2.2"
4
+ VERSION = "1.2.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: esphome
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer