Class: CapistranoMulticonfigParallel::Cursor

Inherits:
Object
  • Object
show all
Includes:
ApplicationHelper
Defined in:
lib/capistrano_multiconfig_parallel/classes/cursor.rb

Overview

class used to fetch cursor position before displaying terminal table ispltd.org/mini_howto:ansi_terminal_codes

Instance Method Summary collapse

Methods included from ApplicationHelper

action_confirmed?, fetch_parsed_string, fetch_remaining_arguments, find_remaining_args, get_question_details, message_from_bundler?, message_is_about_a_task?, message_is_for_stdout?, msg_for_stdin?, parse_json, parse_task_string, percent_of, regex_last_match, setup_command_line_standard, truncate, wrap_string

Methods included from CapistranoHelper

env_key_format, env_prefix, filtered_env_keys_format, setup_flags_for_job, trace_flag

Methods included from GemHelper

fetch_gem_version, find_loaded_gem, find_loaded_gem_property, format_gem_version, get_parsed_version, verify_gem_version

Methods included from StagesHelper

app_names_from_stages, check_stage_path, checks_paths, fetch_stages_app, fetch_stages_from_file, fetch_stages_paths, independent_deploy?, multi_apps?, sorted_paths, stages, stages_paths, stages_root

Methods included from ParseHelper

check_hash_set, check_numeric, strip_characters_from_string, value_is_array?, verify_array_of_strings, verify_empty_options, warn_array_without_strings

Methods included from CoreHelper

app_debug_enabled?, ask_confirm, ask_stdout_confirmation, check_terminal_tty, debug_websocket?, development_debug?, error_filtered?, execute_with_rescue, find_worker_log, force_confirmation, format_error, log_error, log_output_error, log_to_file, multi_fetch_argv, print_to_log_file, rescue_error, rescue_interrupt, setup_filename_logger, setup_logger_formatter, show_warning, terminal_actor, terminal_errors?, websocket_config, websocket_server_config

Methods included from InternalHelper

arg_is_in_default_config?, check_file, create_log_file, custom_commands, default_config_keys, default_internal_config, default_internal_configuration_params, detect_root, enable_main_log_file, fail_capfile_not_found, fetch_default_internal_config, find_config_type, find_env_multi_cap_root, find_file_by_names, find_file_in_directory, get_current_gem_name, internal_config_directory, internal_config_file, log_directory, main_log_file, multi_level_prop, pathname_is_root?, pwd_directory, pwd_parent_dir, root, setup_default_configuration_types, sliced_default_config, try_detect_file

Instance Method Details

#display_on_screen(string, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/capistrano_multiconfig_parallel/classes/cursor.rb', line 8

def display_on_screen(string, options = {})
  begin
    options = options.is_a?(Hash) ? options.stringify_keys : {}
    handle_string_display(string, options)
  rescue => ex
    rescue_error(ex, 'stderr')
  end
end

#display_string_at_position(position, string) ⇒ Object



88
89
90
91
92
93
# File 'lib/capistrano_multiconfig_parallel/classes/cursor.rb', line 88

def display_string_at_position(position, string)
  go_to_position(position)
  erase_from_current_line_to_bottom
  go_to_position(position)
  puts string
end

#dynamic_size_sttyObject



59
60
61
62
# File 'lib/capistrano_multiconfig_parallel/classes/cursor.rb', line 59

def dynamic_size_stty
  size = `stty size 2>/dev/null`
  size.present? ? size : nil
end

#dynamic_size_tputObject



64
65
66
67
68
# File 'lib/capistrano_multiconfig_parallel/classes/cursor.rb', line 64

def dynamic_size_tput
  lines = `tput lines 2>/dev/null`
  cols = `tput cols 2>/dev/null`
  lines.present? && cols.present? ? "#{lines} #{cols}" : nil
end

#erase_from_current_line_to_bottomObject



95
96
97
# File 'lib/capistrano_multiconfig_parallel/classes/cursor.rb', line 95

def erase_from_current_line_to_bottom
  puts "\e[J"
end

#erase_screenObject



99
100
101
# File 'lib/capistrano_multiconfig_parallel/classes/cursor.rb', line 99

def erase_screen
  puts("\e[2J")
end

#fetch_cursor_position(table_size, position, previously_erased_screen) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/capistrano_multiconfig_parallel/classes/cursor.rb', line 28

def fetch_cursor_position(table_size, position, previously_erased_screen)
  final_position = position || fetch_position
  terminal_rows = fetch_terminal_size
  if previously_erased_screen == false && refetch_position?(table_size, terminal_rows, final_position)
    screen_erased = true
    move_to_home!
    final_position = fetch_position
    terminal_rows = fetch_terminal_size
  end
  [final_position, terminal_rows, screen_erased]
end

#fetch_positionObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/capistrano_multiconfig_parallel/classes/cursor.rb', line 46

def fetch_position
  res = ''
  $stdin.raw do |stdin|
    $stdout << "\e[6n"
    $stdout.flush
    while (line = stdin.getc) != 'R'
      res << line if line
    end
  end
  position = res.match(/(?<row>\d+);(?<column>\d+)/)
  { row: position[:row].to_i, column: position[:column].to_i }
end

#fetch_terminal_sizeObject



22
23
24
25
26
# File 'lib/capistrano_multiconfig_parallel/classes/cursor.rb', line 22

def fetch_terminal_size
  size = (dynamic_size_stty || dynamic_size_tput || `echo $LINES $COLUMNS`)
  size = strip_characters_from_string(size).split(' ')
  { rows: size[0].to_i, columns: size[1].to_i }
end

#go_to_position(position) ⇒ Object



103
104
105
# File 'lib/capistrano_multiconfig_parallel/classes/cursor.rb', line 103

def go_to_position(position)
  position_cursor(position[:row], position[:column])
end

#handle_string_display(string, options) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/capistrano_multiconfig_parallel/classes/cursor.rb', line 70

def handle_string_display(string, options)
  position = options.fetch('position', nil)
  table_size = options.fetch('table_size', 0)
  if options.fetch('clear_screen', false).to_s == 'true'
    terminal_clear_display(string)
    [0, 0, false]
  else
    new_position, terminal_rows, screen_erased = fetch_cursor_position(table_size, position, options.fetch('screen_erased', false))
    display_string_at_position(new_position, string)
    [new_position, terminal_rows, screen_erased]
  end
end

#move_to_home!(row = 0, column = 1) ⇒ Object



17
18
19
20
# File 'lib/capistrano_multiconfig_parallel/classes/cursor.rb', line 17

def move_to_home!(row = 0, column = 1)
  erase_screen
  position_cursor(row, column)
end

#position_cursor(line, column) ⇒ Object



107
108
109
# File 'lib/capistrano_multiconfig_parallel/classes/cursor.rb', line 107

def position_cursor(line, column)
  puts("\e[#{line};#{column}H")
end

#refetch_position?(table_size, terminal_size, position) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/capistrano_multiconfig_parallel/classes/cursor.rb', line 40

def refetch_position?(table_size, terminal_size, position)
  terminal_rows = terminal_size[:rows]
  row_position = position[:row]
  terminal_rows.zero? || (terminal_rows.nonzero? && row_position >= (terminal_rows / 2)) || (table_size >= (terminal_rows - row_position))
end

#terminal_clearObject



111
112
113
# File 'lib/capistrano_multiconfig_parallel/classes/cursor.rb', line 111

def terminal_clear
  system('cls') || system('clear') || puts("\e[H\e[2J")
end

#terminal_clear_display(string) ⇒ Object



83
84
85
86
# File 'lib/capistrano_multiconfig_parallel/classes/cursor.rb', line 83

def terminal_clear_display(string)
  terminal_clear
  puts string
end