Class: Fatty::OutputSession

Inherits:
Session
  • Object
show all
Defined in:
lib/fatty/session/output_session.rb

Direct Known Subclasses

ShellSession

Instance Attribute Summary collapse

Attributes inherited from Session

#counter, #keymap, #terminal, #views

Instance Method Summary collapse

Methods inherited from Session

#add_view, #close, #handle_action, #handle_resize, #init, #inspect, #keymap_contexts, #persist!, #resolve_action, #tick, #update, #view

Methods included from Actionable

included

Constructor Details

#initialize(keymap: nil, views: []) ⇒ OutputSession

Returns a new instance of OutputSession.



7
8
9
10
11
12
13
14
15
# File 'lib/fatty/session/output_session.rb', line 7

def initialize(keymap: nil, views: [])
  super(keymap: keymap, views: views)
  @output   = Fatty::OutputBuffer.new(max_lines: 500_000)
  @viewport = Fatty::Viewport.new(height: 10)
  mode = Fatty::Config.config.dig(:output, :mode)&.to_sym || :paging
  @default_output_mode = mode
  @pager = Fatty::Pager.new(output: @output, viewport: @viewport, mode: mode)
  @pager_field = Fatty::InputField.new(prompt: -> { pager_status_prompt })
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



5
6
7
# File 'lib/fatty/session/output_session.rb', line 5

def output
  @output
end

#pagerObject (readonly)

Returns the value of attribute pager.



5
6
7
# File 'lib/fatty/session/output_session.rb', line 5

def pager
  @pager
end

#pager_fieldObject (readonly)

Returns the value of attribute pager_field.



5
6
7
# File 'lib/fatty/session/output_session.rb', line 5

def pager_field
  @pager_field
end

#viewportObject (readonly)

Returns the value of attribute viewport.



5
6
7
# File 'lib/fatty/session/output_session.rb', line 5

def viewport
  @viewport
end

Instance Method Details

#append_output(text, follow: true) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fatty/session/output_session.rb', line 17

def append_output(text, follow: true)
  ntrim = @output.append(text.to_s)
  @pager.on_append(ntrim: ntrim)

  # When callers request follow behavior (scrolling mode),
  # keep the viewport at the bottom unless paging is actively paused.
  if follow && !@pager.paused?
    case @pager.mode
    when :scrolling
      @viewport.page_bottom(@output.lines)
    end
  end
  ntrim
end

#follow_output!Object



90
91
92
93
# File 'lib/fatty/session/output_session.rb', line 90

def follow_output!
  pager.end_of_output
  []
end

#page_output_from_bottom!Object



100
101
102
103
# File 'lib/fatty/session/output_session.rb', line 100

def page_output_from_bottom!
  pager.page_bottom
  []
end

#page_output_from_top!Object



95
96
97
98
# File 'lib/fatty/session/output_session.rb', line 95

def page_output_from_top!
  pager.page_top
  []
end

#pager_active?Boolean

True when the pager is currently holding the screen (i.e., paging mode is active and the output is paused).

Returns:

  • (Boolean)


51
52
53
# File 'lib/fatty/session/output_session.rb', line 51

def pager_active?
  @pager.reserve_prompt_row?
end

#pager_status_promptObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fatty/session/output_session.rb', line 66

def pager_status_prompt
  total = @output.lines.length
  visible_h = pager_active? ? pager.page_height : @viewport.height
  bottom = [@viewport.top + visible_h, total].min
  pct =
    if total.positive?
      ((bottom * 100.0) / total).round
    end

  search = pager.search_label
  search = "  [#{search}]" if search && !search.empty?
  if pct
    "  #{pager.nav_arrow} --More--  #{bottom}/#{total} (#{pct}%)#{search} "
  else
    "  #{pager.nav_arrow} --More--  #{bottom}#{search} "
  end
end

#pager_viewportObject

When the pager is active, the last output row is reserved for the pager InputField. This returns the viewport to use for rendering the output content itself.



58
59
60
61
62
63
64
# File 'lib/fatty/session/output_session.rb', line 58

def pager_viewport
  if pager_active? && @viewport.height > 1
    Fatty::Viewport.new(top: @viewport.top, height: @viewport.height - 1)
  else
    @viewport
  end
end

#reset_for_command!Object



43
44
45
46
47
# File 'lib/fatty/session/output_session.rb', line 43

def reset_for_command!
  reset_output!
  mode = @default_output_mode # Fatty::Config.config.dig(:output, :mode)&.to_sym || :paging
  @pager.reset!(mode: mode)
end

#reset_output!Object



32
33
34
35
# File 'lib/fatty/session/output_session.rb', line 32

def reset_output!
  @output.lines.clear
  @viewport.reset
end

#reset_pager!Object



84
85
86
87
88
# File 'lib/fatty/session/output_session.rb', line 84

def reset_pager!
  # Start the next command from the bottom of existing scrollback.
  @viewport.page_bottom(@output.lines)
  @pager.reset!(total_lines: @output.lines.length, mode: @default_output_mode)
end

#resize_output!Object



37
38
39
40
41
# File 'lib/fatty/session/output_session.rb', line 37

def resize_output!
  was_at_bottom = pager.at_bottom?
  @viewport.height = terminal.screen.output_rect.rows
  pager.preserve_after_resize!(was_at_bottom: was_at_bottom)
end