Class: SpaceArchitect::Terminal
- Inherits:
-
Object
- Object
- SpaceArchitect::Terminal
- Defined in:
- lib/space_architect/terminal.rb
Constant Summary collapse
- SPINNER_FRAMES =
%w[⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏].freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Instance Method Summary collapse
- #error(message) ⇒ Object
-
#initialize(config:, stdout: $stdout, stderr: $stderr, color_mode: "auto") ⇒ Terminal
constructor
A new instance of Terminal.
- #interactive? ⇒ Boolean
- #pastel ⇒ Object
- #path(path) ⇒ Object
- #say(message = "") ⇒ Object
- #success(message) ⇒ Object
- #table(headers, rows) ⇒ Object
- #with_spinner(message) ⇒ Object
Constructor Details
#initialize(config:, stdout: $stdout, stderr: $stderr, color_mode: "auto") ⇒ Terminal
Returns a new instance of Terminal.
12 13 14 15 16 17 18 |
# File 'lib/space_architect/terminal.rb', line 12 def initialize(config:, stdout: $stdout, stderr: $stderr, color_mode: "auto") @config = config @stdout = stdout @stderr = stderr @color_mode = color_mode.to_s.downcase color_mode end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
10 11 12 |
# File 'lib/space_architect/terminal.rb', line 10 def config @config end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
10 11 12 |
# File 'lib/space_architect/terminal.rb', line 10 def stderr @stderr end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
10 11 12 |
# File 'lib/space_architect/terminal.rb', line 10 def stdout @stdout end |
Instance Method Details
#error(message) ⇒ Object
32 33 34 |
# File 'lib/space_architect/terminal.rb', line 32 def error() stderr.puts(colors_enabled? ? pastel.red() : ) end |
#interactive? ⇒ Boolean
20 21 22 |
# File 'lib/space_architect/terminal.rb', line 20 def interactive? stderr.tty? end |
#pastel ⇒ Object
24 25 26 |
# File 'lib/space_architect/terminal.rb', line 24 def pastel @pastel ||= Pastel.new(enabled: colors_enabled?) end |
#path(path) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/space_architect/terminal.rb', line 50 def path(path) value = path.to_s homes.each do |home| return "~" if value == home return "~#{value.delete_prefix(home)}" if value.start_with?("#{home}/") end value end |
#say(message = "") ⇒ Object
28 29 30 |
# File 'lib/space_architect/terminal.rb', line 28 def say( = "") stdout.puts() end |
#success(message) ⇒ Object
36 37 38 |
# File 'lib/space_architect/terminal.rb', line 36 def success() say pastel.green() end |
#table(headers, rows) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/space_architect/terminal.rb', line 40 def table(headers, rows) column_widths = headers.each_index.map do |index| ([headers[index]] + rows.map { |row| row[index].to_s }).map(&:length).max end ([headers] + rows).each_with_index.map do |row, row_index| table_row(headers, row, column_widths, header: row_index.zero?) end.join("\n") end |
#with_spinner(message) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/space_architect/terminal.rb', line 60 def with_spinner() return yield unless interactive? Async do |task| spinner_task = start_spinner(task, ) yield ensure spinner_task&.stop begin spinner_task&.wait rescue StandardError nil end clear_spinner end.wait end |