Class: Renderer

Inherits:
Struct show all
Defined in:
lib/selecta.rb

Defined Under Namespace

Classes: Rendered

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#searchObject

Returns the value of attribute search

Returns:

  • (Object)

    the current value of search



488
489
490
# File 'lib/selecta.rb', line 488

def search
  @search
end

Class Method Details

.render!(search, screen) ⇒ Object



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/selecta.rb', line 489

def self.render!(search, screen)
  rendered = Renderer.new(search).render
  width = screen.width

  screen.with_cursor_hidden do
    rendered.choices.each_with_index do |choice, index|
      choice = choice.truncate_to_width(width)
      is_last_line = (index == rendered.choices.length - 1)
      choice += Text["\n"] unless is_last_line
      screen.write(choice)
    end

    # Move back up to the search line and redraw it, which will naturally
    # leave the cursor at the end of the query.
    screen.cursor_up(rendered.choices.length - 1)
    screen.write(rendered.search_line.truncate_to_width(width))
  end
end

Instance Method Details

#correct_match_count(matches) ⇒ Object



531
532
533
534
535
# File 'lib/selecta.rb', line 531

def correct_match_count(matches)
  limited = matches[0, search.config.visible_choices]
  padded = limited + [Text[""]] * (search.config.visible_choices - limited.length)
  padded
end

#match_count_labelObject



524
525
526
527
528
529
# File 'lib/selecta.rb', line 524

def match_count_label
  choice_count = search.original_matches.length
  max_label_width = choice_count.to_s.length
  match_count = search.all_matches.count
  match_count.to_s.rjust(max_label_width)
end

#renderObject



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/selecta.rb', line 508

def render
  search_line = Text["#{match_count_label} > " + search.query]

  matches = search.best_matches[0, search.config.visible_choices]
  matches = matches.each_with_index.map do |match, index|
    if index == search.index
      Text[:inverse] + match.to_text + Text[:reset]
    else
      match.to_text
    end
  end
  matches = correct_match_count(matches)
  lines = [search_line] + matches
  Rendered.new(lines, search_line)
end