class Scryglass::ViewPanel

Attributes

current_view_coords[RW]
scry_session[RW]
x_boundaries[RW]
y_boundaries[RW]

Public Class Methods

new(scry_session:) click to toggle source
# File lib/scryglass/view_panel.rb, line 10
def initialize(scry_session:)
  self.scry_session = scry_session
  self.current_view_coords = { x: 0, y: 0 }

  recalculate_boundaries
end

Public Instance Methods

ensure_correct_view_coords() click to toggle source
# File lib/scryglass/view_panel.rb, line 17
def ensure_correct_view_coords
  _screen_height, screen_width = $stdout.winsize
  top_boundary    = y_boundaries.min
  bottom_boundary = y_boundaries.max
  left_boundary   = x_boundaries.min
  right_boundary  = x_boundaries.max

  ## Snap View, Vertical
  screen_bottom_index = (body_screen_height - 1)
  top_edge_of_view = current_view_coords[:y]
  bottom_edge_of_view = current_view_coords[:y] + screen_bottom_index
  if bottom_edge_of_view > bottom_boundary
    current_view_coords[:y] = [bottom_boundary - screen_bottom_index, 0].max
  elsif top_edge_of_view < top_boundary
    current_view_coords[:y] = top_boundary
  end

  ## Snap View, Horizontal
  screen_right_edge_index = (screen_width - 1)
  left_edge_of_view = current_view_coords[:x]
  right_edge_of_view = current_view_coords[:x] + screen_right_edge_index
  if !x_boundaries.include?(left_edge_of_view)
    current_view_coords[:x] = left_boundary
  elsif !x_boundaries.include?(right_edge_of_view)
    current_view_coords[:x] = right_boundary - screen_right_edge_index
  end
end
move_view_down(distance) click to toggle source
# File lib/scryglass/view_panel.rb, line 49
def move_view_down(distance)
  current_view_coords[:y] += distance
end
move_view_left(distance) click to toggle source
# File lib/scryglass/view_panel.rb, line 53
def move_view_left(distance)
  current_view_coords[:x] -= distance
end
move_view_right(distance) click to toggle source
# File lib/scryglass/view_panel.rb, line 57
def move_view_right(distance)
  current_view_coords[:x] += distance
end
move_view_up(distance) click to toggle source
# File lib/scryglass/view_panel.rb, line 45
def move_view_up(distance)
  current_view_coords[:y] -= distance
end
recalculate_boundaries() click to toggle source
# File lib/scryglass/view_panel.rb, line 65
def recalculate_boundaries
  recalculate_y_boundaries
  recalculate_x_boundaries
end
screen_string() click to toggle source
# File lib/scryglass/view_panel.rb, line 61
def screen_string
  Hexes.opacify_screen_string(visible_header_string + "\n" + visible_body_string)
end
visible_body_string() click to toggle source
# File lib/scryglass/view_panel.rb, line 74
def visible_body_string
  _screen_height, screen_width = $stdout.winsize
  screen_bottom_index = (body_screen_height - 1)
  screen_right_edge_index = (screen_width - 1)

  body_array = visible_body_slice(uncut_body_string)

  marked_body_array =
    body_array.map do |line|
      last_visible_char_of_line =
        line.ansiless_pick(screen_right_edge_index)

      if last_visible_char_of_line
        line.ansiless_set!(screen_right_edge_index, '·')
      end

      line
    end

  bottom_edge_line = marked_body_array[screen_bottom_index]

  if bottom_edge_line
    bottom_edge_line_has_content =
      !bottom_edge_line.ansiless.tr(' ·', '').empty?

    if bottom_edge_line_has_content
      bottom_edge_line_dot_preview =
        bottom_edge_line.ansiless.gsub(/[^\s]/, '·')
    end

    marked_body_array[screen_bottom_index] =
      bottom_edge_line_dot_preview ||
      bottom_edge_line_default_truncation_dots
  end

  marked_body_array.join("\n")
end
visible_header_string() click to toggle source
# File lib/scryglass/view_panel.rb, line 70
def visible_header_string
  visible_header_slice(uncut_header_string)
end

Private Instance Methods

body_screen_height() click to toggle source
# File lib/scryglass/view_panel.rb, line 126
def body_screen_height
  screen_height, _screen_width = $stdout.winsize
  # It would be more efficient, but technically overall less accurate, to
  #   avoid recalculating visible_header_string here (and everywhere that calls this)
  screen_height - visible_header_string.split("\n").count
end
bottom_edge_line_default_truncation_dots() click to toggle source
# File lib/scryglass/view_panel.rb, line 114
def bottom_edge_line_default_truncation_dots
  _screen_height, screen_width = $stdout.winsize
  dots = '· ··  ···   ··········   ···  ·· ·'
  pad_length = (screen_width - dots.length) / 2

  (' ' * pad_length) + dots
end
visible_header_slice(uncut_header_string) click to toggle source
# File lib/scryglass/view_panel.rb, line 122
def visible_header_slice(uncut_header_string)
  Hexes.simple_screen_slice(uncut_header_string)
end