Class: Changelogger::PreviewWindow

Inherits:
Object
  • Object
show all
Defined in:
lib/changelogger/preview_window.rb

Overview

Changelogger::PreviewWindow shows scrollable text in a framed window.

Instance Method Summary collapse

Constructor Details

#initialize(title: 'Preview', content: '', top: 1, left: 0, height: nil, width: nil) ⇒ PreviewWindow

Changelogger::PreviewWindow.new -> PreviewWindow

Parameters:

  • title (String) (defaults to: 'Preview')

    window title

  • content (String) (defaults to: '')

    initial text

  • top (Integer) (defaults to: 1)

    top row position

  • left (Integer) (defaults to: 0)

    left column position

  • height (Integer, nil) (defaults to: nil)

    window height or computed from screen

  • width (Integer, nil) (defaults to: nil)

    window width or computed from screen



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/changelogger/preview_window.rb', line 16

def initialize(title: 'Preview', content: '', top: 1, left: 0, height: nil, width: nil)
  @title = title
  screen_h = Curses.lines
  screen_w = Curses.cols

  @height = height || [screen_h - top, 3].max
  @width  = width  || [screen_w - left, 10].max
  @top    = top
  @left   = left

  @sub_height = @height - 2
  @sub_width  = @width  - 2
  @sub_top    = @top + 1
  @sub_left   = @left + 1

  @offset = 0
  @lines  = (content || '').split("\n")

  build_windows
  redraw
end

Instance Method Details

#runvoid

This method returns an undefined value.

Changelogger::PreviewWindow#run -> void

Enters the input loop. Returns when user closes the preview.



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
# File 'lib/changelogger/preview_window.rb', line 53

def run
  loop do
    case @sub.getch
    when Curses::Key::UP, 'k'
      @offset = [@offset - 1, 0].max
      redraw
    when Curses::Key::DOWN, 'j'
      max_off = [@lines.length - @sub_height, 0].max
      @offset = [@offset + 1, max_off].min
      redraw
    when Curses::Key::PPAGE
      @offset = [@offset - @sub_height, 0].max
      redraw
    when Curses::Key::NPAGE
      max_off = [@lines.length - @sub_height, 0].max
      @offset = [@offset + @sub_height, max_off].min
      redraw
    when 'g'
      @offset = 0
      redraw
    when 'G'
      @offset = [@lines.length - @sub_height, 0].max
      redraw
    when 'q', 27
      break
    end
  end
ensure
  destroy
end

#update_content(text) ⇒ void

This method returns an undefined value.

Changelogger::PreviewWindow#update_content -> void

Replace content and reset scroll to top.

Parameters:

  • text (String)

    new content



43
44
45
46
47
# File 'lib/changelogger/preview_window.rb', line 43

def update_content(text)
  @lines  = (text || '').split("\n")
  @offset = 0
  redraw
end