Class: Fatty::OutputSession
- Inherits:
-
Session
- Object
- Session
- Fatty::OutputSession
show all
- Defined in:
- lib/fatty/session/output_session.rb
Defined Under Namespace
Classes: VisibleLine
Constant Summary
collapse
- STATE_VIEWPORT =
0
- STATE_LINES =
1
Instance Attribute Summary collapse
Attributes inherited from Session
#counter, #id, #keymap, #terminal
Instance Method Summary
collapse
Methods inherited from Session
#close, #handle_resize, #persist!, #renderer, #screen
Methods included from Actionable
included
Constructor Details
#initialize(id: nil, keymap: nil, history: nil) ⇒ OutputSession
Returns a new instance of OutputSession.
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/fatty/session/output_session.rb', line 19
def initialize(id: nil, keymap: nil, history: nil)
super(keymap: keymap)
@output = Fatty::OutputBuffer.new(max_lines: 500_000)
@viewport = Fatty::Viewport.new(height: 10)
@history = history || Fatty::History.for_path(:default)
@line_numbers = false
@narrow_query = nil
@visible_lines = nil
mode = Fatty::Config.config.dig(:output, :mode)&.to_sym || :paging
@default_output_mode = mode
@pager = Fatty::Pager.new(output: @output, viewport: @viewport, mode: mode, lines: -> { visible_lines })
@pager_field = Fatty::InputField.new(prompt: -> { })
end
|
Instance Attribute Details
#output ⇒ Object
Returns the value of attribute output.
17
18
19
|
# File 'lib/fatty/session/output_session.rb', line 17
def output
@output
end
|
Returns the value of attribute pager.
17
18
19
|
# File 'lib/fatty/session/output_session.rb', line 17
def
@pager
end
|
Returns the value of attribute pager_field.
17
18
19
|
# File 'lib/fatty/session/output_session.rb', line 17
def
@pager_field
end
|
#viewport ⇒ Object
Returns the value of attribute viewport.
17
18
19
|
# File 'lib/fatty/session/output_session.rb', line 17
def viewport
@viewport
end
|
Instance Method Details
#apply_prompt_result(payload) ⇒ Object
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
# File 'lib/fatty/session/output_session.rb', line 291
def apply_prompt_result(payload)
return [] unless payload[:kind].to_sym == :narrow_output
query = payload.fetch(:text, "").to_s
if query.empty?
clear_narrowing!
else
@narrow_query = query
invalidate_visible_lines!
viewport.top = 0
.clamp!
end
[]
end
|
#clear_narrowing! ⇒ Object
306
307
308
309
310
311
|
# File 'lib/fatty/session/output_session.rb', line 306
def clear_narrowing!
@narrow_query = nil
invalidate_visible_lines!
viewport.top = 0
.clamp!
end
|
#highlights ⇒ Object
345
346
347
|
# File 'lib/fatty/session/output_session.rb', line 345
def highlights
.search_visible_highlights(viewport: viewport)
end
|
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
# File 'lib/fatty/session/output_session.rb', line 317
def incrementally_scrollable_from?(prev_state, curr_state)
prev_viewport = prev_state[STATE_VIEWPORT]
curr_viewport = curr_state[STATE_VIEWPORT]
prev_lines = prev_state[STATE_LINES]
curr_lines = curr_state[STATE_LINES]
delta = curr_viewport[0] - prev_viewport[0]
overlap = prev_lines.length - delta
delta.positive? &&
delta < curr_viewport[1] &&
overlap.positive? &&
prev_lines.drop(delta) == curr_lines.take(overlap)
end
|
#init(terminal:) ⇒ Object
37
38
39
|
# File 'lib/fatty/session/output_session.rb', line 37
def init(terminal:)
super
end
|
#line_numbers? ⇒ Boolean
313
314
315
|
# File 'lib/fatty/session/output_session.rb', line 313
def line_numbers?
@line_numbers
end
|
#narrow_query ⇒ Object
283
284
285
|
# File 'lib/fatty/session/output_session.rb', line 283
def narrow_query
@narrow_query
end
|
#narrowed? ⇒ Boolean
287
288
289
|
# File 'lib/fatty/session/output_session.rb', line 287
def narrowed?
!@narrow_query.nil?
end
|
True when the pager is currently holding the screen (i.e., paging mode is
active and the output is paused).
341
342
343
|
# File 'lib/fatty/session/output_session.rb', line 341
def
@pager.active?
end
|
332
333
334
335
336
337
|
# File 'lib/fatty/session/output_session.rb', line 332
def scroll_delta_from(prev_state, curr_state)
prev_viewport = prev_state[STATE_VIEWPORT]
curr_viewport = curr_state[STATE_VIEWPORT]
curr_viewport[0] - prev_viewport[0]
end
|
#state(viewport: @viewport) ⇒ Object
242
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/fatty/session/output_session.rb', line 242
def state(viewport: @viewport)
[
viewport.state,
viewport.slice(visible_lines),
renderer.screen.output_rect.rows,
renderer.screen.output_rect.cols,
highlights,
renderer.theme_version,
line_numbers?,
]
end
|
#tick ⇒ Object
144
145
146
147
148
149
150
151
|
# File 'lib/fatty/session/output_session.rb', line 144
def tick
dirty = false
if .autoscroll?
step = [(viewport.height * 3) / 4, 1].max
dirty ||= .autoscroll_step?(max_lines: step)
end
dirty
end
|
#update(command) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/fatty/session/output_session.rb', line 41
def update(command)
log_update(command)
payload = command.payload
commands =
case command.action
when :key
ev = payload.fetch(:event)
action, args = resolve_action(ev)
if action
apply_action(action, args, event: ev)
else
[]
end
when :append
case payload[:mode]
when :scrolling
.toggle_paging_mode if .mode == :paging
when :paging
.toggle_paging_mode if .mode == :scrolling
end
before = output.lines.length
Fatty.debug(
"output append role=#{payload[:role].inspect} text=#{payload[:text].inspect}",
tag: :session,
)
append_output(
payload.fetch(:text, ""),
follow: payload.fetch(:follow, true),
role: payload[:role],
)
reveal_appended_block(before) if payload[:scroll]
[]
when :set_mode
case payload.fetch(:mode).to_sym
when :scrolling
.set_to_scrolling
when :paging
.set_to_paging
end
[]
when :clear
reset_output!
[]
when :resize
resize_output!
[]
when :begin_command
reset_for_command!
.begin_command!(anchor: output.lines.length)
[]
when :finish_command
.finish_command!
[]
when :quit_paging
.quit
[Command.terminal(:refresh_layout)]
when :pager_search_set
(payload)
when :pager_search_step
(payload)
when :pager_search_cancel
.search_cancel!
[]
when :pager_search_commit
.search_commit!
[]
when :pager_isearch_update
(payload)
when :pager_isearch_step
(payload)
when :pager_isearch_cancel
.isearch_cancel!
[Command.session(:isearch, :isearch_set_failed, failed: false)]
when :pager_isearch_commit
(payload)
when :prompt_result
apply_prompt_result(payload)
when :prompt_cancelled
[]
else
[]
end
Array(commands)
end
|
#view ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/fatty/session/output_session.rb', line 126
def view
Fatty.debug(
"OutputSession#view id=#{id} pager_active=#{} output_rect=#{renderer.screen.output_rect.inspect}",
tag: :render,
)
if
renderer.hide_cursor
renderer.render_output(self, viewport: (renderer.screen))
renderer.(
,
row: renderer.screen.output_rect.rows - 1,
role: :pager_status,
)
else
renderer.render_output(self)
end
end
|
#visible_lines ⇒ Object
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
# File 'lib/fatty/session/output_session.rb', line 254
def visible_lines
@visible_lines ||=
if narrowed?
terms = narrow_query.split
output.lines.each_with_index.filter_map do |text, index|
visible_text = visible_output_text(text)
next unless terms.all? { |term| visible_text.include?(term) }
VisibleLine.new(
number: index + 1,
text: text,
fragments: output.fragments_for(index),
)
end
else
output.lines.each_with_index.map do |text, index|
VisibleLine.new(
number: index + 1,
text: text,
fragments: output.fragments_for(index),
)
end
end
end
|
#visible_output_text(text) ⇒ Object
279
280
281
|
# File 'lib/fatty/session/output_session.rb', line 279
def visible_output_text(text)
Fatty::Ansi.segment(text.to_s).map(&:first).join
end
|