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.
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/fatty/session/output_session.rb', line 15
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.
13
14
15
|
# File 'lib/fatty/session/output_session.rb', line 13
def output
@output
end
|
Returns the value of attribute pager.
13
14
15
|
# File 'lib/fatty/session/output_session.rb', line 13
def
@pager
end
|
Returns the value of attribute pager_field.
13
14
15
|
# File 'lib/fatty/session/output_session.rb', line 13
def
@pager_field
end
|
#viewport ⇒ Object
Returns the value of attribute viewport.
13
14
15
|
# File 'lib/fatty/session/output_session.rb', line 13
def viewport
@viewport
end
|
Instance Method Details
#apply_prompt_result(payload) ⇒ Object
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
# File 'lib/fatty/session/output_session.rb', line 275
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
290
291
292
293
294
295
|
# File 'lib/fatty/session/output_session.rb', line 290
def clear_narrowing!
@narrow_query = nil
invalidate_visible_lines!
viewport.top = 0
.clamp!
end
|
#highlights ⇒ Object
329
330
331
|
# File 'lib/fatty/session/output_session.rb', line 329
def highlights
.search_visible_highlights(viewport: viewport)
end
|
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
# File 'lib/fatty/session/output_session.rb', line 301
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
33
34
35
|
# File 'lib/fatty/session/output_session.rb', line 33
def init(terminal:)
super
end
|
#line_numbers? ⇒ Boolean
297
298
299
|
# File 'lib/fatty/session/output_session.rb', line 297
def line_numbers?
@line_numbers
end
|
#narrow_query ⇒ Object
267
268
269
|
# File 'lib/fatty/session/output_session.rb', line 267
def narrow_query
@narrow_query
end
|
#narrowed? ⇒ Boolean
271
272
273
|
# File 'lib/fatty/session/output_session.rb', line 271
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).
325
326
327
|
# File 'lib/fatty/session/output_session.rb', line 325
def
@pager.active?
end
|
316
317
318
319
320
321
|
# File 'lib/fatty/session/output_session.rb', line 316
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
233
234
235
236
237
238
239
240
241
242
243
|
# File 'lib/fatty/session/output_session.rb', line 233
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
135
136
137
138
139
140
141
142
|
# File 'lib/fatty/session/output_session.rb', line 135
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
37
38
39
40
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
|
# File 'lib/fatty/session/output_session.rb', line 37
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
append_output(
payload.fetch(:text, ""),
follow: payload.fetch(:follow, true),
)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/fatty/session/output_session.rb', line 117
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
# File 'lib/fatty/session/output_session.rb', line 245
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)
end
else
output.lines.each_with_index.map do |text, index|
VisibleLine.new(number: index + 1, text: text)
end
end
end
|
#visible_output_text(text) ⇒ Object
263
264
265
|
# File 'lib/fatty/session/output_session.rb', line 263
def visible_output_text(text)
Fatty::Ansi.segment(text.to_s).map(&:first).join
end
|