Class: Tryouts::CLI::TTYStatusDisplay

Inherits:
Object
  • Object
show all
Defined in:
lib/tryouts/cli/formatters/tty_status_display.rb

Overview

Encapsulates TTY manipulation for live status display with fallback

Constant Summary collapse

STATUS_LINES =

Lines reserved for fixed status display (4 content + 1 separator)

5

Instance Method Summary collapse

Constructor Details

#initialize(io = $stdout, options = {}) ⇒ TTYStatusDisplay

Returns a new instance of TTYStatusDisplay.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tryouts/cli/formatters/tty_status_display.rb', line 14

def initialize(io = $stdout, options = {})
  @io         = io
  @available  = check_tty_availability
  @show_debug = options.fetch(:debug, false)
  @cleanup_registered = false

  return unless @available

  @cursor        = TTY::Cursor
  @pastel        = Pastel.new
  @status_active = false
  @original_cursor_position = nil

  register_cleanup_handlers
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/tryouts/cli/formatters/tty_status_display.rb', line 30

def available?
  @available
end

#cleanup!Object

Explicit cleanup method for manual invocation



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tryouts/cli/formatters/tty_status_display.rb', line 106

def cleanup!
  return unless @available

  with_terminal_safety do
    if @status_active
      # Clear the status area completely
      @io.print @cursor.move_to(0, TTY::Screen.height - STATUS_LINES + 1)
      STATUS_LINES.times do |i|
        @io.print @cursor.clear_line
        @io.print @cursor.down(1) if i < STATUS_LINES - 1
      end
    end

    # Reset cursor to a safe position (start of line below cleared area)
    @io.print @cursor.move_to(0, TTY::Screen.height - STATUS_LINES)
    @io.print "\n"
    @io.flush
  end

  @status_active = false
end

#clear_status_areaObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/tryouts/cli/formatters/tty_status_display.rb', line 81

def clear_status_area
  return unless @available && @status_active

  with_terminal_safety do
    # Move to status area and clear it completely - start from first status line
    @io.print @cursor.move_to(0, TTY::Screen.height - STATUS_LINES + 1)

    # Clear each line thoroughly
    STATUS_LINES.times do |i|
      @io.print @cursor.clear_line
      @io.print @cursor.down(1) if i < STATUS_LINES - 1  # Don't go down after last line
    end

    # Move cursor to a clean area for final output - position it well above the cleared area
    # This ensures no interference with the cleared status content
    target_row = TTY::Screen.height - STATUS_LINES - 2  # Leave some buffer space
    @io.print @cursor.move_to(0, target_row)
    @io.print "\n"  # Add a clean line for final output to start
    @io.flush
  end

  @status_active = false
end

#reserve_status_areaObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tryouts/cli/formatters/tty_status_display.rb', line 34

def reserve_status_area
  return unless @available && !@status_active

  with_terminal_safety do
    # Store original cursor position if possible
    @original_cursor_position = get_cursor_position

    # Simply print empty lines to push content up and make room at bottom
    STATUS_LINES.times { @io.print "\n" }

    @status_active = true
  end
end

#update_status(state) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tryouts/cli/formatters/tty_status_display.rb', line 55

def update_status(state)
  return unless @available && @status_active

  with_terminal_safety do
    # Move to status area (bottom of screen) and update in place
    current_row, current_col = get_cursor_position

    # Move to status area at bottom
    @io.print @cursor.move_to(0, TTY::Screen.height - STATUS_LINES + 1)

    # Clear and write status content
    STATUS_LINES.times do
      @io.print @cursor.clear_line
      @io.print @cursor.down(1) unless STATUS_LINES == 1
    end

    # Move back to start of status area and write content
    @io.print @cursor.move_to(0, TTY::Screen.height - STATUS_LINES + 1)
    write_status_content(state)

    # Move cursor back to where content should continue (just before status area)
    @io.print @cursor.move_to(0, TTY::Screen.height - STATUS_LINES)
    @io.flush
  end
end

#write_scrolling(text) ⇒ Object



48
49
50
51
52
53
# File 'lib/tryouts/cli/formatters/tty_status_display.rb', line 48

def write_scrolling(text)
  return @io.print(text) unless @available

  # Always write content normally - the status will be updated separately
  @io.print text
end