Class: TTY::Command::Window::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/command/window/block.rb

Overview

One live window in the terminal: an emulator plus title-bar state.

Blocks are registered with a Coordinator, which stacks and paints them. All emulator access is synchronized through the block's mutex because the runner thread feeds data while the render thread paints.

Constant Summary collapse

SUCCESS_GLYPH =
""
FAILURE_GLYPH =
""
FOCUSED_MARKER =
""
UNFOCUSED_MARKER =
""

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(emulator:, title:, lines:, on_exit:, interactive: false) ⇒ Block

Returns a new instance of Block.

Parameters:

  • emulator (Emulator)
  • title (String, false)

    title text; false disables the title bar

  • lines (Integer)

    window height (emulator rows)

  • on_exit (Symbol)

    :freeze, :dump_on_failure or :collapse

  • interactive (Boolean) (defaults to: false)

    eligible for stdin focus



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tty/command/window/block.rb', line 27

def initialize(emulator:, title:, lines:, on_exit:, interactive: false)
  @emulator = emulator
  @title_enabled = title != false
  @title_text = title.is_a?(String) ? title : ""
  @lines = lines
  @on_exit = on_exit
  @interactive = interactive
  @status = :running
  @started_at = Block.clock
  @runtime = nil
  @focused = false
  @mutex = Mutex.new
end

Instance Attribute Details

#focusedObject

Returns the value of attribute focused.



20
21
22
# File 'lib/tty/command/window/block.rb', line 20

def focused
  @focused
end

#linesObject (readonly)

Returns the value of attribute lines.



19
20
21
# File 'lib/tty/command/window/block.rb', line 19

def lines
  @lines
end

#on_exitObject (readonly)

Returns the value of attribute on_exit.



19
20
21
# File 'lib/tty/command/window/block.rb', line 19

def on_exit
  @on_exit
end

#statusObject (readonly)

Returns the value of attribute status.



19
20
21
# File 'lib/tty/command/window/block.rb', line 19

def status
  @status
end

Class Method Details

.clockObject



41
42
43
# File 'lib/tty/command/window/block.rb', line 41

def self.clock
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

Instance Method Details

#collapsed?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/tty/command/window/block.rb', line 79

def collapsed?
  done? && @on_exit == :collapse
end

#done?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/tty/command/window/block.rb', line 63

def done?
  !running?
end

#dump_on_finalize?Boolean

Whether finalization should replace this block with its full history.

Returns:

  • (Boolean)


84
85
86
# File 'lib/tty/command/window/block.rb', line 84

def dump_on_finalize?
  failure? && @on_exit == :dump_on_failure
end

#dump_text(pastel) ⇒ Object

Plain title line plus history, used when dumping on failure.



116
117
118
# File 'lib/tty/command/window/block.rb', line 116

def dump_text(pastel)
  "#{status_glyph(pastel)} #{@title_text} #{pastel.dim("#{elapsed_text}")}\n#{full_text}"
end

#failure?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/tty/command/window/block.rb', line 71

def failure?
  @status == :failure
end

#feed(data) ⇒ Object

Feed raw child output into the emulator.



46
47
48
# File 'lib/tty/command/window/block.rb', line 46

def feed(data)
  @mutex.synchronize { @emulator.feed(data) }
end

#finish(success, runtime) ⇒ Object

Mark the block finished.

Parameters:

  • success (Boolean)
  • runtime (Float)

    seconds



54
55
56
57
# File 'lib/tty/command/window/block.rb', line 54

def finish(success, runtime)
  @runtime = runtime
  @status = success ? :success : :failure
end

#full_textObject

Full plain-text history (scrollback + visible screen).



111
112
113
# File 'lib/tty/command/window/block.rb', line 111

def full_text
  @mutex.synchronize { @emulator.full_text }
end

#heightObject

Rendered height in terminal lines given the current state.



89
90
91
92
93
# File 'lib/tty/command/window/block.rb', line 89

def height
  return 1 if collapsed?

  (@title_enabled ? 1 : 0) + @lines
end

#interactive?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/tty/command/window/block.rb', line 75

def interactive?
  @interactive
end

#render(width:, pastel:, frame:) ⇒ Array<String>

Render the block to an array of ANSI strings, height elements.

Parameters:

  • width (Integer)

    terminal width

  • pastel (Pastel::Delegator)
  • frame (String)

    current spinner frame

Returns:

  • (Array<String>)


101
102
103
104
105
106
107
108
# File 'lib/tty/command/window/block.rb', line 101

def render(width:, pastel:, frame:)
  @mutex.synchronize do
    out = []
    out << title_line(width, pastel, frame) if @title_enabled || collapsed?
    out.concat(@emulator.render_lines) unless collapsed?
    out
  end
end

#resize(width) ⇒ Object

Resize the emulator to a new terminal width. PTY resize is owned by ChildSession.



122
123
124
# File 'lib/tty/command/window/block.rb', line 122

def resize(width)
  @mutex.synchronize { @emulator.resize(cols: width) }
end

#running?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/tty/command/window/block.rb', line 59

def running?
  @status == :running
end

#success?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/tty/command/window/block.rb', line 67

def success?
  @status == :success
end