Class: Rufio::DialogRenderer

Inherits:
Object
  • Object
show all
Includes:
TextUtils
Defined in:
lib/rufio/dialog_renderer.rb

Overview

Renders floating dialog windows in the terminal

Constant Summary

Constants included from TextUtils

TextUtils::BREAK_POINT_THRESHOLD, TextUtils::ELLIPSIS, TextUtils::ELLIPSIS_MIN_WIDTH, TextUtils::FULLWIDTH_CHAR_WIDTH, TextUtils::HALFWIDTH_CHAR_WIDTH

Instance Method Summary collapse

Methods included from TextUtils

char_width, display_width, find_break_point, pad_string_to_width, truncate_to_width, wrap_preview_lines

Instance Method Details

#calculate_center(content_width, content_height) ⇒ Array<Integer>

Calculate center position for a window

Parameters:

  • content_width (Integer)

    Window width

  • content_height (Integer)

    Window height

Returns:

  • (Array<Integer>)
    x, y

    position



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/rufio/dialog_renderer.rb', line 208

def calculate_center(content_width, content_height)
  # Get terminal size
  console = IO.console
  if console
    screen_width, screen_height = console.winsize.reverse
  else
    screen_width = 80
    screen_height = 24
  end

  # Calculate center position
  x = [(screen_width - content_width) / 2, 1].max
  y = [(screen_height - content_height) / 2, 1].max

  [x, y]
end

#calculate_dimensions(content_lines, options = {}) ⇒ Array<Integer>

Calculate appropriate dimensions for content

Parameters:

  • content_lines (Array<String>)

    Content lines

  • options (Hash) (defaults to: {})

    Options

Options Hash (options):

  • :title (String, nil)

    Window title

  • :min_width (Integer)

    Minimum width (default: 30)

  • :max_width (Integer)

    Maximum width (default: 80)

Returns:

  • (Array<Integer>)
    width, height


243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/rufio/dialog_renderer.rb', line 243

def calculate_dimensions(content_lines, options = {})
  title = options[:title]
  min_width = options[:min_width] || 30
  max_width = options[:max_width] || 80

  # Calculate required width based on content
  max_content_width = content_lines.map { |line| TextUtils.display_width(line) }.max || 0
  title_width = title ? TextUtils.display_width(title) : 0
  required_width = [max_content_width, title_width].max + 4 # +4 for borders and padding

  width = [[required_width, min_width].max, max_width].min

  # Calculate height: borders + title (if exists) + separator + content
  height = content_lines.length + 2 # +2 for top and bottom borders
  height += 2 if title # +2 for title line and separator

  [width, height]
end

#clear_area(x, y, width, height) ⇒ Object

Clear a rectangular area on the screen

Parameters:

  • x (Integer)

    X position

  • y (Integer)

    Y position

  • width (Integer)

    Area width

  • height (Integer)

    Area height



230
231
232
233
234
# File 'lib/rufio/dialog_renderer.rb', line 230

def clear_area(x, y, width, height)
  height.times do |row|
    print "\e[#{y + row};#{x}H#{' ' * width}"
  end
end

#draw_floating_window(x, y, width, height, title, content_lines, options = {}) ⇒ Object

Draw a floating window with title, content, and customizable colors

Parameters:

  • x (Integer)

    X position (column)

  • y (Integer)

    Y position (row)

  • width (Integer)

    Window width

  • height (Integer)

    Window height

  • title (String, nil)

    Window title (optional)

  • content_lines (Array<String>)

    Content lines to display

  • options (Hash) (defaults to: {})

    Customization options

Options Hash (options):

  • :border_color (String)

    Border color ANSI code

  • :title_color (String)

    Title color ANSI code

  • :content_color (String)

    Content color ANSI code



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rufio/dialog_renderer.rb', line 156

def draw_floating_window(x, y, width, height, title, content_lines, options = {})
  # Default options
  border_color = options[:border_color] || "\e[37m"  # White
  title_color = options[:title_color] || "\e[1;33m"  # Bold yellow
  content_color = options[:content_color] || "\e[37m" # White
  reset_color = "\e[0m"

  # Draw top border
  print "\e[#{y};#{x}H#{border_color}#{'' * (width - 2)}#{reset_color}"

  # Draw title line if title exists
  if title
    title_width = TextUtils.display_width(title)
    title_padding = (width - 2 - title_width) / 2
    padded_title = ' ' * title_padding + title
    title_line = TextUtils.pad_string_to_width(padded_title, width - 2)
    print "\e[#{y + 1};#{x}H#{border_color}#{title_color}#{title_line}#{border_color}#{reset_color}"

    # Draw title separator
    print "\e[#{y + 2};#{x}H#{border_color}#{'' * (width - 2)}#{reset_color}"
    content_start_y = y + 3
  else
    content_start_y = y + 1
  end

  # Draw content lines
  content_height = title ? height - 4 : height - 2
  content_lines.each_with_index do |line, index|
    break if index >= content_height

    line_y = content_start_y + index
    line_content = TextUtils.pad_string_to_width(line, width - 2)
    print "\e[#{line_y};#{x}H#{border_color}#{content_color}#{line_content}#{border_color}#{reset_color}"
  end

  # Fill remaining lines with empty space
  remaining_lines = content_height - content_lines.length
  remaining_lines.times do |i|
    line_y = content_start_y + content_lines.length + i
    empty_line = ' ' * (width - 2)
    print "\e[#{line_y};#{x}H#{border_color}#{empty_line}#{reset_color}"
  end

  # Draw bottom border
  bottom_y = y + height - 1
  print "\e[#{bottom_y};#{x}H#{border_color}#{'' * (width - 2)}#{reset_color}"
end

#draw_floating_window_to_buffer(screen, x, y, width, height, title, content_lines, options = {}) ⇒ Object

Phase 4: Screenバッファにフローティングウィンドウを描画

Parameters:

  • screen (Screen)

    Screen buffer to draw to

  • x (Integer)

    X position (column)

  • y (Integer)

    Y position (row)

  • width (Integer)

    Window width

  • height (Integer)

    Window height

  • title (String, nil)

    Window title (optional)

  • content_lines (Array<String>)

    Content lines to display

  • options (Hash) (defaults to: {})

    Customization options

Options Hash (options):

  • :border_color (String)

    Border color ANSI code

  • :title_color (String)

    Title color ANSI code

  • :content_color (String)

    Content color ANSI code



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
# File 'lib/rufio/dialog_renderer.rb', line 22

def draw_floating_window_to_buffer(screen, x, y, width, height, title, content_lines, options = {})
  # Default options
  border_color = options[:border_color] || "\e[37m"  # White
  title_color = options[:title_color] || "\e[1;33m"  # Bold yellow
  content_color = options[:content_color] || "\e[37m" # White

  # Draw top border
  screen.put_string(x, y, "#{'' * (width - 2)}", fg: border_color)

  # Draw title line if title exists
  if title
    title_width = TextUtils.display_width(title)
    title_padding = (width - 2 - title_width) / 2
    padded_title = ' ' * title_padding + title
    title_line = TextUtils.pad_string_to_width(padded_title, width - 2)

    screen.put(x, y + 1, '', fg: border_color)
    screen.put_string(x + 1, y + 1, title_line, fg: title_color)
    screen.put(x + width - 1, y + 1, '', fg: border_color)

    # Draw title separator
    screen.put_string(x, y + 2, "#{'' * (width - 2)}", fg: border_color)
    content_start_y = y + 3
  else
    content_start_y = y + 1
  end

  # Draw content lines
  content_height = title ? height - 4 : height - 2
  content_lines.each_with_index do |line, index|
    break if index >= content_height

    line_y = content_start_y + index
    line_content = TextUtils.pad_string_to_width(line, width - 2)

    screen.put(x, line_y, '', fg: border_color)
    screen.put_string(x + 1, line_y, line_content, fg: content_color)
    screen.put(x + width - 1, line_y, '', fg: border_color)
  end

  # Fill remaining lines with empty space
  remaining_lines = content_height - content_lines.length
  remaining_lines.times do |i|
    line_y = content_start_y + content_lines.length + i
    empty_line = ' ' * (width - 2)

    screen.put(x, line_y, '', fg: border_color)
    screen.put_string(x + 1, line_y, empty_line)
    screen.put(x + width - 1, line_y, '', fg: border_color)
  end

  # Draw bottom border
  bottom_y = y + height - 1
  screen.put_string(x, bottom_y, "#{'' * (width - 2)}", fg: border_color)
end

#draw_floating_window_to_overlay(screen, x, y, width, height, title, content_lines, options = {}) ⇒ Object

オーバーレイレイヤーにフローティングウィンドウを描画

Parameters:

  • screen (Screen)

    Screen buffer with overlay enabled

  • x (Integer)

    X position (column)

  • y (Integer)

    Y position (row)

  • width (Integer)

    Window width

  • height (Integer)

    Window height

  • title (String, nil)

    Window title (optional)

  • content_lines (Array<String>)

    Content lines to display

  • options (Hash) (defaults to: {})

    Customization options



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
# File 'lib/rufio/dialog_renderer.rb', line 87

def draw_floating_window_to_overlay(screen, x, y, width, height, title, content_lines, options = {})
  # オーバーレイが有効でない場合は有効化
  screen.enable_overlay unless screen.overlay_enabled?

  border_color = options[:border_color] || "\e[37m"
  title_color = options[:title_color] || "\e[1;33m"
  content_color = options[:content_color] || "\e[37m"

  # Draw top border
  screen.put_overlay_string(x, y, "#{'' * (width - 2)}", fg: border_color)

  # Draw title line if title exists
  if title
    title_width = TextUtils.display_width(title)
    title_padding = (width - 2 - title_width) / 2
    padded_title = ' ' * title_padding + title
    title_line = TextUtils.pad_string_to_width(padded_title, width - 2)

    screen.put_overlay(x, y + 1, '', fg: border_color)
    screen.put_overlay_string(x + 1, y + 1, title_line, fg: title_color)
    screen.put_overlay(x + width - 1, y + 1, '', fg: border_color)

    # Draw title separator
    screen.put_overlay_string(x, y + 2, "#{'' * (width - 2)}", fg: border_color)
    content_start_y = y + 3
  else
    content_start_y = y + 1
  end

  # Draw content lines
  content_height = title ? height - 4 : height - 2
  content_lines.each_with_index do |line, index|
    break if index >= content_height

    line_y = content_start_y + index
    line_content = TextUtils.pad_string_to_width(line, width - 2)

    screen.put_overlay(x, line_y, '', fg: border_color)
    screen.put_overlay_string(x + 1, line_y, line_content, fg: content_color)
    screen.put_overlay(x + width - 1, line_y, '', fg: border_color)
  end

  # Fill remaining lines with empty space
  remaining_lines = content_height - content_lines.length
  remaining_lines.times do |i|
    line_y = content_start_y + content_lines.length + i
    empty_line = ' ' * (width - 2)

    screen.put_overlay(x, line_y, '', fg: border_color)
    screen.put_overlay_string(x + 1, line_y, empty_line)
    screen.put_overlay(x + width - 1, line_y, '', fg: border_color)
  end

  # Draw bottom border
  bottom_y = y + height - 1
  screen.put_overlay_string(x, bottom_y, "#{'' * (width - 2)}", fg: border_color)
end

#show_input_dialog(title, prompt, options = {}) ⇒ String?

Show a floating input dialog and get user input

Parameters:

  • title (String)

    Dialog title

  • prompt (String)

    Prompt message

  • options (Hash) (defaults to: {})

    Customization options

Options Hash (options):

  • :border_color (String)

    Border color ANSI code

  • :title_color (String)

    Title color ANSI code

  • :content_color (String)

    Content color ANSI code

  • :width (Integer)

    Dialog width (default: 50)

Returns:

  • (String, nil)

    User input (nil if cancelled)



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/rufio/dialog_renderer.rb', line 271

def show_input_dialog(title, prompt, options = {})
  require 'io/console'

  width = options[:width] || 50
  height = 8 # title + separator + prompt + input line + blank + ESC message + borders

  x, y = calculate_center(width, height)

  content_lines = [
    '',
    prompt,
    '',
    '> ', # Input line with prompt indicator
    '',
    '',
    'Press ESC to cancel'
  ]

  # Draw initial dialog
  draw_floating_window(x, y, width, height, title, content_lines, options)

  # Position cursor at input line
  # y + 1 (top border) + 1 (title) + 1 (separator) + 1 (blank) + 1 (prompt) + 1 (blank) + 1 (input line) = y + 6
  input_y = y + 6
  input_x = x + 4 # Inside border with prompt '> ' (border 1 + space 1 + '> ' 2)
  print "\e[#{input_y};#{input_x}H"
  STDOUT.flush

  # Read input with escape support
  input = read_input_in_dialog(x, y, width, height, input_x, input_y, options)

  # Clear dialog
  clear_area(x, y, width, height)

  input
end