Class: Fatty::Pager
Constant Summary collapse
- SCROLL_WHEEL_LINES =
3
Instance Attribute Summary collapse
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Instance Method Summary collapse
- #act_on(action, *args, env:, **kwargs) ⇒ Object
- #at_bottom? ⇒ Boolean
- #at_top? ⇒ Boolean
- #autoscroll? ⇒ Boolean
- #autoscroll_step?(max_lines: 200) ⇒ Boolean
- #begin_command!(anchor:) ⇒ Object
- #clamp! ⇒ Object
- #clamp_to_page! ⇒ Object
- #clear_search! ⇒ Object
- #first_term_match(text, from:) ⇒ Object
-
#initialize(output:, viewport:, mode: :paging) ⇒ Pager
constructor
A new instance of Pager.
- #isearch_cancel! ⇒ Object
- #isearch_commit!(pattern:, direction:) ⇒ Object
- #isearch_step!(direction:) ⇒ Object
- #isearch_update!(pattern:, direction:) ⇒ Object
- #last_term_match_before(text, limit:) ⇒ Object
- #max_page_top ⇒ Object
- #merge_highlight_ranges(ranges) ⇒ Object
- #nav_arrow ⇒ Object
-
#on_append(ntrim:) ⇒ Object
Called by OutputSession after new text is appended.
- #page_bottom! ⇒ Object
-
#page_height ⇒ Object
The pager sometimes reserves a row for a status/minibuffer line.
- #page_top! ⇒ Object
- #paused? ⇒ Boolean
- #plain_search? ⇒ Boolean
- #preserve_after_resize!(was_at_bottom:) ⇒ Object
- #reserve_prompt_row? ⇒ Boolean
- #reset!(total_lines: 0, mode: :paging) ⇒ Object
-
#search_active? ⇒ Boolean
--- Search -----------------------------------------------------------.
- #search_label ⇒ Object
-
#search_last_match ⇒ Object
Exposes the last match for render-layer highlighting.
- #search_pattern ⇒ Object
-
#search_repeat_next! ⇒ Object
Vim/less semantics: - n repeats in the original direction (set by / ? C-s C-r) - N repeats in the opposite direction.
- #search_repeat_prev! ⇒ Object
-
#search_set!(pattern:, regex:, direction:) ⇒ Object
Sets the search pattern and moves the viewport to the next match.
-
#search_step!(direction:, initial: false, update_origin: true) ⇒ Object
Steps to the next match in +direction+.
-
#search_visible_highlights(viewport:) ⇒ Object
Returns highlight ranges for all matches within the given viewport.
- #term_regexps ⇒ Object
Methods included from Actionable
Constructor Details
#initialize(output:, viewport:, mode: :paging) ⇒ Pager
Returns a new instance of Pager.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/fatty/pager.rb', line 12 def initialize(output:, viewport:, mode: :paging) @output = output @viewport = # @mode can be :paging, or :scrolling # - paging: the viewport displays one page worth of output at a time, # - scrolling: the viewport displays output continuously as it is produced. @mode = mode @paused = false @autoscroll = false @last_nav_dir = nil # Search state (used by SearchSession, but owned here so paging repeats and # highlighting survive after the SearchSession closes). @search = { pattern: nil, regex: false, re: nil, original_direction: nil, last_direction: nil, last: nil, # { line: Integer, from: Integer, to: Integer } last_view_top: nil, pending_wrap: nil, # :forward/:backward } end |
Instance Attribute Details
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
10 11 12 |
# File 'lib/fatty/pager.rb', line 10 def mode @mode end |
Instance Method Details
#act_on(action, *args, env:, **kwargs) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/fatty/pager.rb', line 103 def act_on(action, *args, env:, **kwargs) raise Fatty::ActionError, "Unknown action: #{action}" unless action if Fatty::Actions.registered?(action) Fatty::Actions.call(action, env, *args, **kwargs) elsif respond_to?(action) public_send(action, *args, **kwargs) else raise Fatty::ActionError, "Unknown action: #{action}" end end |
#at_bottom? ⇒ Boolean
237 238 239 |
# File 'lib/fatty/pager.rb', line 237 def at_bottom? @viewport.top >= max_page_top end |
#at_top? ⇒ Boolean
233 234 235 |
# File 'lib/fatty/pager.rb', line 233 def at_top? @viewport.at_top? end |
#autoscroll? ⇒ Boolean
44 45 46 |
# File 'lib/fatty/pager.rb', line 44 def autoscroll? @autoscroll end |
#autoscroll_step?(max_lines: 200) ⇒ Boolean
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/fatty/pager.rb', line 285 def autoscroll_step?(max_lines: 200) lines = @output.lines total = lines.length moved = false if total > 0 if @viewport.at_bottom?(total) @autoscroll = false else remaining = @viewport.max_top(total) - @viewport.top n = [remaining, max_lines].min before = @viewport.top @viewport.scroll_down(lines, n) moved = @viewport.top != before if @viewport.at_bottom?(total) @autoscroll = false end end end moved end |
#begin_command!(anchor:) ⇒ Object
271 272 273 274 275 276 |
# File 'lib/fatty/pager.rb', line 271 def begin_command!(anchor:) @mode = :paging @paused = false @anchor = anchor @autoscroll = false end |
#clamp! ⇒ Object
259 260 261 262 263 264 265 |
# File 'lib/fatty/pager.rb', line 259 def clamp! if paused? clamp_to_page! else @viewport.clamp!(@output.lines) end end |
#clamp_to_page! ⇒ Object
267 268 269 |
# File 'lib/fatty/pager.rb', line 267 def clamp_to_page! @viewport.top = @viewport.top.clamp(0, max_page_top) end |
#clear_search! ⇒ Object
343 344 345 346 347 348 349 350 |
# File 'lib/fatty/pager.rb', line 343 def clear_search! @search[:pattern] = nil @search[:regex] = false @search[:re] = nil @search[:last] = nil @search[:original_direction] = nil @search[:pending_wrap] = nil end |
#first_term_match(text, from:) ⇒ Object
530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
# File 'lib/fatty/pager.rb', line 530 def first_term_match(text, from:) best = nil term_regexps.each do |term_re| m = term_re.match(text, from) next unless m if best.nil? || m.begin(0) < best.begin(0) best = m end end best end |
#isearch_cancel! ⇒ Object
606 607 608 609 610 611 612 613 |
# File 'lib/fatty/pager.rb', line 606 def isearch_cancel! if @isearch_snapshot @viewport.top = @isearch_snapshot[:viewport_top] @search = @isearch_snapshot[:search] @isearch_snapshot = nil end nil end |
#isearch_commit!(pattern:, direction:) ⇒ Object
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 |
# File 'lib/fatty/pager.rb', line 615 def isearch_commit!(pattern:, direction:) ensure_isearch_snapshot! pattern = pattern.to_s if pattern.strip.empty? isearch_cancel! return { status: :not_found } end result = isearch_update!(pattern: pattern, direction: direction) if result[:status] == :moved @search[:original_direction] = direction.to_sym @search[:pending_wrap] = nil end @isearch_snapshot = nil result end |
#isearch_step!(direction:) ⇒ Object
601 602 603 604 |
# File 'lib/fatty/pager.rb', line 601 def isearch_step!(direction:) ensure_isearch_snapshot! search_step!(direction: direction, initial: false, update_origin: false) end |
#isearch_update!(pattern:, direction:) ⇒ Object
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
# File 'lib/fatty/pager.rb', line 573 def isearch_update!(pattern:, direction:) ensure_isearch_snapshot! pattern = pattern.to_s result = if pattern.strip.empty? restore_isearch_anchor! @search[:re] = nil @search[:pattern] = nil @search[:last] = nil @search[:pending_wrap] = nil { status: :not_found } else re = compile_search_regexp(pattern, regex: false) @search[:pattern] = pattern @search[:regex] = false @search[:re] = re @search[:last_direction] = direction.to_sym initial = @search[:last].nil? search_step!(direction: direction, initial: initial, update_origin: false) end result rescue RegexpError => e { status: :not_found, message: "Invalid regexp: #{e.}" } end |
#last_term_match_before(text, limit:) ⇒ Object
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
# File 'lib/fatty/pager.rb', line 545 def last_term_match_before(text, limit:) best = nil term_regexps.each do |term_re| text.to_enum(:scan, term_re).each do m = Regexp.last_match break if m.end(0) > limit if best.nil? || m.begin(0) >= best.begin(0) best = m end end end best end |
#max_page_top ⇒ Object
255 256 257 |
# File 'lib/fatty/pager.rb', line 255 def max_page_top [@output.lines.size - page_height, 0].max end |
#merge_highlight_ranges(ranges) ⇒ Object
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/fatty/pager.rb', line 502 def merge_highlight_ranges(ranges) return [] if ranges.empty? merged = [ranges.first.dup] ranges.drop(1).each do |from, to, role| prev = merged[-1] _, prev_to, prev_role = prev if from <= prev_to prev[1] = [prev_to, to].max prev[2] = :primary if prev_role == :primary || role == :primary else merged << [from, to, role] end end merged end |
#nav_arrow ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/fatty/pager.rb', line 241 def nav_arrow if at_top? "⇓" elsif at_bottom? "⇑" elsif @last_nav_dir == :up "⇑" elsif @last_nav_dir == :down "⇓" else "" end end |
#on_append(ntrim:) ⇒ Object
Called by OutputSession after new text is appended.
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 |
# File 'lib/fatty/pager.rb', line 58 def on_append(ntrim:) @viewport.adjust_for_trim(ntrim) lines = @output.lines total = lines.size case @mode when :scrolling # In scrolling mode, the viewport is allowed to move continuously. # Autoscroll animation (after switching from paging -> scrolling) is # driven by #autoscroll_step? in ShellSession#tick, not by incremental # scroll deltas during append. @viewport.clamp!(lines) when :paging if @anchor produced = total - @anchor if produced <= 0 return end # While producing the first page, keep the viewport pinned to the anchor. @viewport.top = @anchor clamp_to_page! if produced >= page_height @paused = true clamp_to_page! end return end if @paused clamp_to_page! return end # No anchor and not yet paused: once the output exceeds a single page, # pause and show the first page. if total > page_height @paused = true @viewport.page_top end # Otherwise user is not at bottom; don't force movement. @viewport.clamp!(lines) end end |
#page_bottom! ⇒ Object
631 632 633 |
# File 'lib/fatty/pager.rb', line 631 def page_bottom! @viewport.top = max_page_top end |
#page_height ⇒ Object
The pager sometimes reserves a row for a status/minibuffer line. When it does, paging calculations must use a reduced page height so that the pause threshold and page-step size match what is actually rendered.
51 52 53 54 55 |
# File 'lib/fatty/pager.rb', line 51 def page_height h = @viewport.height h -= 1 if reserve_prompt_row? [h, 1].max end |
#page_top! ⇒ Object
635 636 637 |
# File 'lib/fatty/pager.rb', line 635 def page_top! @viewport.top = 0 end |
#paused? ⇒ Boolean
36 37 38 |
# File 'lib/fatty/pager.rb', line 36 def paused? @mode == :paging && @paused end |
#plain_search? ⇒ Boolean
522 523 524 |
# File 'lib/fatty/pager.rb', line 522 def plain_search? @search[:re] && !@search[:regex] end |
#preserve_after_resize!(was_at_bottom:) ⇒ Object
639 640 641 642 643 644 645 |
# File 'lib/fatty/pager.rb', line 639 def preserve_after_resize!(was_at_bottom:) if @mode == :paging && @output.lines.any? && was_at_bottom page_bottom! else clamp! end end |
#reserve_prompt_row? ⇒ Boolean
40 41 42 |
# File 'lib/fatty/pager.rb', line 40 def reserve_prompt_row? paused? end |
#reset!(total_lines: 0, mode: :paging) ⇒ Object
278 279 280 281 282 283 |
# File 'lib/fatty/pager.rb', line 278 def reset!(total_lines: 0, mode: :paging) @mode = mode @paused = false @autoscroll = false @anchor = nil end |
#search_active? ⇒ Boolean
--- Search -----------------------------------------------------------
309 310 311 |
# File 'lib/fatty/pager.rb', line 309 def search_active? !@search[:re].nil? end |
#search_label ⇒ Object
562 563 564 565 566 567 568 569 570 571 |
# File 'lib/fatty/pager.rb', line 562 def search_label return unless @search[:re] # Keep showing the direction of the most recent movement (nice UX), # while repeat semantics depend on original_direction. arrow = (@search[:last_direction] == :backward ? "↑" : "↓") prefix = @search[:regex] ? "re:" : "" pat = @search[:pattern].to_s "#{arrow} #{prefix}#{pat}" end |
#search_last_match ⇒ Object
Exposes the last match for render-layer highlighting. (We only highlight the last match for now; later we can highlight all visible matches.)
417 418 419 |
# File 'lib/fatty/pager.rb', line 417 def search_last_match @search[:last] end |
#search_pattern ⇒ Object
421 422 423 |
# File 'lib/fatty/pager.rb', line 421 def search_pattern @search[:pattern].to_s end |
#search_repeat_next! ⇒ Object
Vim/less semantics:
- n repeats in the original direction (set by / ? C-s C-r)
- N repeats in the opposite direction
403 404 405 406 |
# File 'lib/fatty/pager.rb', line 403 def search_repeat_next! dir = (@search[:original_direction] || :forward).to_sym search_step!(direction: dir, update_origin: false) end |
#search_repeat_prev! ⇒ Object
408 409 410 411 412 |
# File 'lib/fatty/pager.rb', line 408 def search_repeat_prev! origin = (@search[:original_direction] || :forward).to_sym dir = (origin == :forward ? :backward : :forward) search_step!(direction: dir, update_origin: false) end |
#search_set!(pattern:, regex:, direction:) ⇒ Object
Sets the search pattern and moves the viewport to the next match. Returns a result hash: { status: :moved } { status: :not_found }
The initial search includes the currently visible page:
- forward starts at viewport.top
- backward starts at viewport.bottom_index(total)
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/fatty/pager.rb', line 321 def search_set!(pattern:, regex:, direction:) pattern = pattern.to_s if pattern.strip.empty? clear_search! return { status: :not_found } end re = compile_search_regexp(pattern, regex: regex) @search[:pattern] = pattern @search[:regex] = !!regex @search[:re] = re @search[:last_direction] = direction.to_sym @search[:original_direction] = direction.to_sym @search[:last] = nil @search[:pending_wrap] = nil search_step!(direction: direction, initial: true) rescue RegexpError => e clear_search! { status: :not_found, message: "Invalid regexp: #{e.}" } end |
#search_step!(direction:, initial: false, update_origin: true) ⇒ Object
Steps to the next match in +direction+. If no match exists without wrapping, returns :boundary and requires a second step in the same direction to wrap. NOTE: Regex search is strictly line-local. Matches never span line boundaries even if '.' would normally match '\n'. This preserves pager navigation, viewport anchoring, and renderer simplicity.
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/fatty/pager.rb', line 360 def search_step!(direction:, initial: false, update_origin: true) direction = direction.to_sym re = @search[:re] @search[:last_direction] = direction.to_sym if update_origin @search[:original_direction] = direction.to_sym end return { status: :not_found } unless re lines = @output.lines total = lines.length return { status: :not_found } if total.zero? start = search_start_position(direction: direction, total: total, initial: initial) found = find_next_match(lines, re, start, direction: direction, wrap: false) if found apply_search_match(found) @search[:pending_wrap] = nil return { status: :moved } end if @search[:pending_wrap] == direction # Second invocation in the same direction: wrap. wrap_start = wrap_start_position(direction: direction, total: total) found = find_next_match(lines, re, wrap_start, direction: direction, wrap: true) @search[:pending_wrap] = nil if found apply_search_match(found) return { status: :moved, wrapped: true } end return { status: :not_found } end @search[:pending_wrap] = direction { status: :boundary, message: (direction: direction) } end |
#search_visible_highlights(viewport:) ⇒ Object
Returns highlight ranges for all matches within the given viewport.
Format: { abs_line_index => [[from, to, :secondary], [from, to, :secondary], ..., [from, to, :primary]], ... }
Ranges are in visible text coordinates (ANSI already stripped), matching the renderer’s slice planner / highlighting behavior.
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
# File 'lib/fatty/pager.rb', line 436 def search_visible_highlights(viewport:) return unless @search[:re] lines = @output.lines total = lines.length return if total.zero? top = .top.to_i bottom = .bottom_index(total) current = @search[:last] out = {} if @search[:regex] (top..bottom).each do |i| text = visible_text(lines[i]) next if text.nil? || text.empty? ranges = [] text.to_enum(:scan, @search[:re]).each do m = Regexp.last_match ranges << [m.begin(0), m.end(0), :secondary] end next if ranges.empty? if current && current[:line].to_i == i cf = current[:from].to_i ct = current[:to].to_i ranges.reject! { |a, b, _role| a == cf && b == ct } ranges << [cf, ct, :primary] end ranges.sort_by!(&:first) out[i] = merge_highlight_ranges(ranges) end else term_res = Fatty::Search.compile_term_regexps(@search[:pattern]) (top..bottom).each do |i| text = visible_text(lines[i]) next if text.nil? || text.empty? ranges = [] term_res.each do |term_re| text.to_enum(:scan, term_re).each do m = Regexp.last_match ranges << [m.begin(0), m.end(0), :secondary] end end next if ranges.empty? if current && current[:line].to_i == i cf = current[:from].to_i ct = current[:to].to_i ranges.reject! { |a, b, _role| a == cf && b == ct } ranges << [cf, ct, :primary] end ranges.sort_by!(&:first) out[i] = merge_highlight_ranges(ranges) end end out.empty? ? nil : out end |
#term_regexps ⇒ Object
526 527 528 |
# File 'lib/fatty/pager.rb', line 526 def term_regexps Fatty::Search.compile_term_regexps(@search[:pattern]) end |