Class: RgssDb::AppCli

Inherits:
Object
  • Object
show all
Includes:
Strings
Defined in:
lib/rgss_db/view/app_cli.rb

Overview

Application CLI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_folder: nil, app_version: nil, rgss_version: nil) ⇒ AppCli

Creates a new app cli instance

Parameters:

  • data_folder (String) (defaults to: nil)

    RPG Maker database path

  • app_version (String) (defaults to: nil)

    Application version

  • rgss_version (Symbol) (defaults to: nil)

    RGSS version



48
49
50
51
52
53
# File 'lib/rgss_db/view/app_cli.rb', line 48

def initialize(data_folder: nil, app_version: nil, rgss_version: nil)
  @app_version = app_version
  @rgss_version = rgss_version
  @data_folder = data_folder
  @prompt = TTY::Prompt.new
end

Instance Attribute Details

#app_versionString

Application version shown on the CLI

Returns:

  • (String)


31
32
33
# File 'lib/rgss_db/view/app_cli.rb', line 31

def app_version
  @app_version
end

#data_folderSymbol

Data folder path shown on the CLI

Returns:

  • (Symbol)


39
40
41
# File 'lib/rgss_db/view/app_cli.rb', line 39

def data_folder
  @data_folder
end

#rgss_versionSymbol

RPG Maker version shown on the CLI

Returns:

  • (Symbol)


35
36
37
# File 'lib/rgss_db/view/app_cli.rb', line 35

def rgss_version
  @rgss_version
end

Instance Method Details

#draw(*strings, color: nil) ⇒ Object

Draws all strings on the standard output

Parameters:

  • strings (Array<String>)

    Strings

  • color (Symbol) (defaults to: nil)

    Color symbol



68
69
70
71
72
# File 'lib/rgss_db/view/app_cli.rb', line 68

def draw(*strings, color: nil)
  strings.each do |str|
    puts color ? str.to_s.colorize(color) : str.to_s
  end
end

#draw_app_headerObject

Draws the app header on the standard output



229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/rgss_db/view/app_cli.rb', line 229

def draw_app_header
  # App title
  draw <<~EOF
    ██████   ██████  ███████ ███████     ██████   █████  ████████  █████  ██████   █████  ███████ ███████
    ██   ██ ██       ██      ██          ██   ██ ██   ██    ██    ██   ██ ██   ██ ██   ██ ██      ██
    ██████  ██   ███ ███████ ███████     ██   ██ ███████    ██    ███████ ██████  ███████ ███████ █████
    ██   ██ ██    ██      ██      ██     ██   ██ ██   ██    ██    ██   ██ ██   ██ ██   ██      ██ ██
    ██   ██  ██████  ███████ ███████     ██████  ██   ██    ██    ██   ██ ██████  ██   ██ ███████ ███████
                                                                                                   v#{@app_version}
  EOF
    .colorize(:green)
end

#draw_app_infoObject

Draws the app information on the standard output



245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/rgss_db/view/app_cli.rb', line 245

def draw_app_info
  # Data folder
  draw(format(StrAppInfo::DATA_FOLDER, @data_folder), color: :green)
  # Detected RGSS version
  draw(format(StrAppInfo::RPG_VERSION, @rgss_version || StrAppInfo::VERSION_INVALID_LABEL), color: :green)

  # Draw warning frame in case rpg maker version is unknown
  return unless @rgss_version.nil?

  draw_empty_line
  draw_warning_frame(StrAppInfo::VERSION_INVALID_TEXT)
end

#draw_app_menu(*menu_contents, breadcrumbs: nil) ⇒ Object

Draws a menu header on the terminal

This header is common for every application menu

Parameters:

  • menu_contents (Array<String>)

    Menu information contents

  • breadcrumbs (Array<String>) (defaults to: nil)

    Breadcrumbs list



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/rgss_db/view/app_cli.rb', line 266

def draw_app_menu(*menu_contents, breadcrumbs: nil)
  reset_screen
  draw_app_header
  draw_empty_line
  draw_app_info
  draw_empty_line

  # Draw breadcrumbs on screen
  unless breadcrumbs.nil?
    draw_navigation(*breadcrumbs)
    draw_empty_line
  end

  # Draw information panel on screen
  return if menu_contents.empty?

  draw_info_frame(*menu_contents)
  draw_empty_line
end

#draw_app_submenu(*menu_contents, breadcrumbs: nil) ⇒ Object

Draws a submenu header on the terminal

This header is common for every application submenu

Parameters:

  • menu_contents (Array<String>)

    Menu information contents

  • breadcrumbs (Array<String>) (defaults to: nil)

    Breadcrumbs list



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/rgss_db/view/app_cli.rb', line 294

def draw_app_submenu(*menu_contents, breadcrumbs: nil)
  reset_screen

  # Draw breadcrumbs on screen
  unless breadcrumbs.nil?
    draw_navigation(*breadcrumbs)
    draw_empty_line
  end

  # Draw menu contents on screen
  return if menu_contents.empty?

  draw_info_frame(*menu_contents)
  draw_empty_line
end

#draw_cancel_operation(cancel_message = nil) ⇒ Object

Draws a cancel operation to the terminal

Parameters:

  • cancel_message (String) (defaults to: nil)

    Cancel message



220
221
222
223
224
# File 'lib/rgss_db/view/app_cli.rb', line 220

def draw_cancel_operation(cancel_message = nil)
  draw_empty_line
  draw(cancel_message || StrPrompts::CANCEL_TEXT, color: :red)
  draw_empty_line
end

#draw_empty_lineObject

Draws an empty line on the standard output



77
78
79
# File 'lib/rgss_db/view/app_cli.rb', line 77

def draw_empty_line
  draw("")
end

#draw_error(string) ⇒ Object

Draws “string“ on the standard output as an error

Parameters:

  • string (String)

    String



104
105
106
# File 'lib/rgss_db/view/app_cli.rb', line 104

def draw_error(string)
  draw(string, color: :red)
end

#draw_error_frame(*contents) ⇒ Object

Draws an error frame with the given contents on the standard output

Nothing is drawn if the “contents“ array is empty

Parameters:

  • contents (Array<String>)

    Frame contents



174
175
176
177
178
179
180
181
182
# File 'lib/rgss_db/view/app_cli.rb', line 174

def draw_error_frame(*contents)
  return if contents.empty?

  box = TTY::Box.frame(
    *contents,
    title: { top_left: StrOthers::ERRO_FRAME_LABEL }
  )
  draw(box, color: :red)
end

#draw_frame(*contents) ⇒ Object

Draws a frame with the given contents on the standard output

Nothing is drawn if the “contents“ array is empty

Parameters:

  • contents (Array<String>)

    Frame contents



126
127
128
129
130
131
# File 'lib/rgss_db/view/app_cli.rb', line 126

def draw_frame(*contents)
  return if contents.empty?

  box = TTY::Box.frame(*contents)
  draw(box)
end

#draw_info(string) ⇒ Object

Draws “string“ on the standard output as info

Parameters:

  • string (String)

    String



86
87
88
# File 'lib/rgss_db/view/app_cli.rb', line 86

def draw_info(string)
  draw(string, color: :blue)
end

#draw_info_frame(*contents) ⇒ Object

Draws an information frame with the given contents on the standard output

Nothing is drawn if the “contents“ array is empty

Parameters:

  • contents (Array<String>)

    Frame contents



140
141
142
143
144
145
146
147
148
# File 'lib/rgss_db/view/app_cli.rb', line 140

def draw_info_frame(*contents)
  return if contents.empty?

  box = TTY::Box.frame(
    *contents,
    title: { top_left: StrOthers::INFO_FRAME_LABEL }
  )
  draw(box)
end

#draw_navigation(*breadcrumbs) ⇒ Object

Draws the user’s navigation in the standard output

Nothing is drawn if the “breadcrumbs“ array is empty

Parameters:

  • breadcrumbs (Array<String>)

    List of breadcrumbs



115
116
117
# File 'lib/rgss_db/view/app_cli.rb', line 115

def draw_navigation(*breadcrumbs)
  draw(breadcrumbs.join(" -> "), color: :blue) unless breadcrumbs.empty?
end

#draw_success_operation(success_message = nil) ⇒ Object

Draws a success operation to the terminal

Parameters:

  • success_message (String) (defaults to: nil)

    Success message



209
210
211
212
213
# File 'lib/rgss_db/view/app_cli.rb', line 209

def draw_success_operation(success_message = nil)
  draw_empty_line
  draw(success_message || StrPrompts::SUCCESS_TEXT, color: :green)
  draw_empty_line
end

#draw_table(columns, *rows, render_type: :unicode) ⇒ Object

Draws a table on the standard output

The number of “columns“ and the size of each row in “rows“ must have the same length

Columns: [Column1, Column2] Rows: [[“a”, “b”], [“c”, “d”]]

Parameters:

  • columns (Array<String>)

    Columns

  • rows (Array<String>)

    List of rows

  • render_type (Symbol) (defaults to: :unicode)

    Table render type



196
197
198
199
200
201
202
# File 'lib/rgss_db/view/app_cli.rb', line 196

def draw_table(columns, *rows, render_type: :unicode)
  table = TTY::Table.new(
    columns,
    rows
  )
  draw(table.render(render_type))
end

#draw_warning(string) ⇒ Object

Draws “string“ on the standard output as a warning

Parameters:

  • string (String)

    String



95
96
97
# File 'lib/rgss_db/view/app_cli.rb', line 95

def draw_warning(string)
  draw(string, color: :yellow)
end

#draw_warning_frame(*contents) ⇒ Object

Draws a warning frame with the given contents on the standard output

Nothing is drawn if the “contents“ array is empty

Parameters:

  • contents (Array<String>)

    Frame contents



157
158
159
160
161
162
163
164
165
# File 'lib/rgss_db/view/app_cli.rb', line 157

def draw_warning_frame(*contents)
  return if contents.empty?

  box = TTY::Box.frame(
    *contents,
    title: { top_left: StrOthers::WARN_FRAME_LABEL }
  )
  draw(box, color: :yellow)
end

#prompt_ask(question, default: nil, value: nil, required: false, convert: nil, validate_text: nil, &validate) {|| ... } ⇒ String

Executes and draws an user input on the standard output

Parameters:

  • question (String)

    Question to ask

  • default (String) (defaults to: nil)

    Default option

  • value (String) (defaults to: nil)

    Pre-populated value

  • required (Boolean) (defaults to: false)

    Force input

  • convert (Symbol) (defaults to: nil)

    Conversion symbol

  • validate_text (String) (defaults to: nil)

    Validation fail text

  • validate (Proc)

    Validation callback

Yield Parameters:

  • (String)

Returns:

  • (String)


376
377
378
379
380
381
382
383
384
385
386
# File 'lib/rgss_db/view/app_cli.rb', line 376

def prompt_ask(question, default: nil, value: nil, required: false, convert: nil, validate_text: nil, &validate)
  @prompt.ask(
    question,
    default: default,
    value: value,
    required: required,
    convert: convert || :string
  ) do |ask|
    ask.validate ->(input) { yield input }, validate_text || StrPrompts::ASK_VALIDATION_FAIL_TEXT if block_given?
  end
end

#prompt_confirm?(input_text = nil, default: true) ⇒ Boolean

Executes and draws an user confirmation on the standard output

Parameters:

  • input_text (String) (defaults to: nil)

    Menu text

  • default (Boolean) (defaults to: true)

    Default option

Returns:

  • (Boolean)


357
358
359
# File 'lib/rgss_db/view/app_cli.rb', line 357

def prompt_confirm?(input_text = nil, default: true)
  @prompt.yes?(input_text || StrPrompts::CONFIRM_INPUT_TEXT, default: default)
end

#prompt_pause(*keys, timeout: nil) ⇒ Object

Executes and draws a terminal pause to the standard output

If “timeout“ is given, the execution will be resumed after that time

Parameters:

  • keys (Array<Symbol>)

    List of key symbols

  • timeout (Integer) (defaults to: nil)

    Time out in seconds



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/rgss_db/view/app_cli.rb', line 318

def prompt_pause(*keys, timeout: nil)
  if keys.empty?
    # No specific keys
    if timeout
      @prompt.keypress(
        StrPrompts::PAUSE_ANY_KEYS_TIMEOUT.colorize(:green),
        timeout: timeout
      )
    else
      @prompt.keypress(
        StrPrompts::PAUSE_ANY_KEYS.colorize(:green)
      )
    end
  else
    # Specific keys
    key_names = keys.map { |key| key.to_s.upcase }.join(", ")
    if timeout
      @prompt.keypress(
        format(StrPrompts::PAUSE_KEYS_TIMEOUT, key_names).colorize(:green),
        keys: keys,
        timeout: timeout
      )
    else
      @prompt.keypress(
        format(StrPrompts::PAUSE_KEYS, key_names).colorize(:green),
        keys: keys
      )
    end
  end
end

#prompt_select(*menu_options, default: nil, input_text: nil, menu_cycle: false) ⇒ String

Executes and draws a menu selection on the standard output+

If “menu_cycle“ is true, the selection will cycle around if the top/bottom option is reached

Parameters:

  • menu_options (Array)

    List of menu options

  • default (Array) (defaults to: nil)

    Default options

  • input_text (String) (defaults to: nil)

    Menu text

  • menu_cycle (Boolean) (defaults to: false)

    Menu cycle status

Returns:

  • (String)

    Selected option



400
401
402
403
404
405
406
407
408
# File 'lib/rgss_db/view/app_cli.rb', line 400

def prompt_select(*menu_options, default: nil, input_text: nil, menu_cycle: false)
  @prompt.select(
    input_text || StrPrompts::SELECT_INPUT_TEXT,
    menu_options,
    default: default,
    cycle: menu_cycle,
    per_page: APP_CLI_SELECT_PER_PAGE
  )
end

#prompt_select_multi(*menu_options, default: nil, input_text: nil, menu_cycle: false) ⇒ Object

Executes and draws a menu selection on the standard output+

If “menu_cycle“ is true, the selection will cycle around if the top/bottom option is reached

Parameters:

  • menu_options (Array)

    List of menu options

  • default (Array) (defaults to: nil)

    Default options

  • input_text (String) (defaults to: nil)

    Menu text

  • menu_cycle (Boolean) (defaults to: false)

    Menu cycle status

Returns:

  • Selected options



422
423
424
425
426
427
428
429
430
431
# File 'lib/rgss_db/view/app_cli.rb', line 422

def prompt_select_multi(*menu_options, default: nil, input_text: nil, menu_cycle: false)
  @prompt.multi_select(
    input_text || StrPrompts::SELECT_INPUT_TEXT,
    menu_options,
    default: default,
    cycle: menu_cycle,
    per_page: APP_CLI_MULTI_SELECT_PER_PAGE,
    enum: APP_CLI_MULTI_SELECT_ENUM_CHAR
  )
end

#prompt_spinner(text, &block) {|spinner| ... } ⇒ Object

Executes and draws a spinner with a routine on the standard output

This method allows to execute a block while a spinner is drawn on the screen

Parameters:

  • text (String)

    Spinner text

  • block (Proc)

Yield Parameters:

  • spinner (TTY::Spinner)


442
443
444
445
# File 'lib/rgss_db/view/app_cli.rb', line 442

def prompt_spinner(text, &block)
  spinner = TTY::Spinner.new("[:spinner] #{text}", format: :dots)
  spinner.run(StrPrompts::SPINNER_TASK_COMPLETED_TEXT) { yield spinner }
end

#reset_screenObject

Resets the console screen position with a escape sequence



58
59
60
# File 'lib/rgss_db/view/app_cli.rb', line 58

def reset_screen
  puts "\e[H\e[2J"
end