Class: Potty::Application
- Inherits:
-
Object
- Object
- Potty::Application
- Defined in:
- lib/potty/application.rb
Overview
Main application wrapper: owns the view stack, the tick loop, and a Surface (the render target). Mode picks the surface:
:curses (default) — full-screen curses display, input via getch.
:inline — N lines redrawn in place under the cursor via ANSI,
no init_screen, terminal stays cooked (input
ignored; host drives quit). Good for progress UIs.
Instance Attribute Summary collapse
-
#surface ⇒ Object
readonly
Returns the value of attribute surface.
-
#theme ⇒ Object
readonly
Returns the value of attribute theme.
-
#tick_interval ⇒ Object
When set (milliseconds), the event loop wakes every interval even without input, advancing time-based widgets (Animator, Countdown).
-
#view_stack ⇒ Object
readonly
Returns the value of attribute view_stack.
-
#window_manager ⇒ Object
readonly
Returns the value of attribute window_manager.
Instance Method Summary collapse
-
#initialize(mode: :curses, lines: nil, theme: nil, out: $stdout, listen: false, input: $stdin) ⇒ Application
constructor
A new instance of Application.
- #pop_view ⇒ Object
-
#push_view(view) ⇒ Object
View navigation.
- #quit ⇒ Object
- #refresh_all ⇒ Object (also: #redraw)
-
#resume ⇒ Object
Resume after suspension.
-
#run(root_view) ⇒ Object
Start the application with root view.
-
#suspend ⇒ Object
Suspend the surface for an external process (e.g., shelling out).
-
#tick ⇒ Object
Advance time-based widgets and repaint.
Constructor Details
#initialize(mode: :curses, lines: nil, theme: nil, out: $stdout, listen: false, input: $stdin) ⇒ Application
Returns a new instance of Application.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/potty/application.rb', line 28 def initialize(mode: :curses, lines: nil, theme: nil, out: $stdout, listen: false, input: $stdin) @view_stack = [] @running = false @theme = theme || Theme.new @mode = mode @lines = lines @out = out @listen = listen @input = input # Kept for back-compat: curses-mode consumers that draw straight to # window_manager.stdscr or read its dimensions. @window_manager = (mode == :curses ? WindowManager.new : nil) @surface = nil @tick_interval = nil end |
Instance Attribute Details
#surface ⇒ Object (readonly)
Returns the value of attribute surface.
20 21 22 |
# File 'lib/potty/application.rb', line 20 def surface @surface end |
#theme ⇒ Object (readonly)
Returns the value of attribute theme.
20 21 22 |
# File 'lib/potty/application.rb', line 20 def theme @theme end |
#tick_interval ⇒ Object
When set (milliseconds), the event loop wakes every interval even without input, advancing time-based widgets (Animator, Countdown). Leave nil for a purely blocking, input-driven loop (the default). ~33-50ms gives smooth animation. Required for :inline.
26 27 28 |
# File 'lib/potty/application.rb', line 26 def tick_interval @tick_interval end |
#view_stack ⇒ Object (readonly)
Returns the value of attribute view_stack.
20 21 22 |
# File 'lib/potty/application.rb', line 20 def view_stack @view_stack end |
#window_manager ⇒ Object (readonly)
Returns the value of attribute window_manager.
20 21 22 |
# File 'lib/potty/application.rb', line 20 def window_manager @window_manager end |
Instance Method Details
#pop_view ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/potty/application.rb', line 63 def pop_view return if @view_stack.size <= 1 view = @view_stack.pop view.deactivate @view_stack.last&.activate(self) refresh_all end |
#push_view(view) ⇒ Object
View navigation
56 57 58 59 60 61 |
# File 'lib/potty/application.rb', line 56 def push_view(view) @view_stack.last&.deactivate @view_stack.push(view) view.activate(self) refresh_all end |
#quit ⇒ Object
72 73 74 |
# File 'lib/potty/application.rb', line 72 def quit @running = false end |
#refresh_all ⇒ Object Also known as: redraw
76 77 78 79 80 |
# File 'lib/potty/application.rb', line 76 def refresh_all @surface.erase current_view&.render @surface.present end |
#resume ⇒ Object
Resume after suspension.
97 98 99 100 101 |
# File 'lib/potty/application.rb', line 97 def resume @surface&.start current_view&.activate(self) refresh_all end |
#run(root_view) ⇒ Object
Start the application with root view
45 46 47 48 49 50 51 52 53 |
# File 'lib/potty/application.rb', line 45 def run(root_view) @surface = build_surface @surface.start push_view(root_view) @running = true event_loop ensure @surface&.finalize end |
#suspend ⇒ Object
Suspend the surface for an external process (e.g., shelling out).
92 93 94 |
# File 'lib/potty/application.rb', line 92 def suspend @surface&.finalize end |
#tick ⇒ Object
Advance time-based widgets and repaint. Called automatically each event-loop frame; also public so a host that drives its own loop can pump animation/countdowns itself.
86 87 88 89 |
# File 'lib/potty/application.rb', line 86 def tick current_view&.tick(Time.now) refresh_all end |