Module: Tui

Defined in:
lib/tui.rb,
lib/tui.rb,
lib/tui/block.rb,
lib/tui/format.rb

Overview

Root module / singleton. See Tui.run - the main entrypoint.

Defined Under Namespace

Modules: Assets, Format, Tools Classes: Block, Layout, Settings

Constant Summary collapse

DELAY_STEP_MS =

Delay correction step to maintain FPS

100

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.current_avg_fps ⇒ Object (readonly)

Average redraws count / sec



29
30
31
# File 'lib/tui.rb', line 29

def current_avg_fps
  @current_avg_fps
end

.delay_ms ⇒ Object (readonly)

Current delay between redraws



27
28
29
# File 'lib/tui.rb', line 27

def delay_ms
  @delay_ms
end

Class Method Details

.run(layout, settings = Settings::new) ⇒ Thread

The main entrypoint of the TUI

Parameters:

  • layout (Tui::Layout) —
    • UI's layout to render and draw
  • settings (Tui::Settings) (defaults to: Settings::new) —
    • UI's settings that affects UI's behavior

Returns:

  • (Thread)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tui.rb', line 36

def run layout, settings = Settings::new
  raise "expected Layout, found #{layout.class}" unless layout.is_a? Layout

  @settings = settings
  @started_at = DateTime.now.strftime('%Q').to_i
  @delay_ms = 1000 / @settings.target_fps

  @thread = Thread.new {
    loop {
      refresh
      content = @settings.draw_main_window ?
        layout.render
            .align!(**effective_window_size)
            .fit!(**effective_window_size, fill: true)
            .box!
        : layout.render
      clear
      print content

      sleep @delay_ms / 1000.0
    }
  }

  @thread
end