Class: Keisanjaku::TerminalViewport
- Inherits:
-
Object
- Object
- Keisanjaku::TerminalViewport
- Defined in:
- lib/keisanjaku/terminal_viewport.rb
Constant Summary collapse
- POLL_INTERVAL =
0.25
Instance Attribute Summary collapse
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #current_width ⇒ Object
- #fixed_width? ⇒ Boolean
-
#initialize(width: nil, provider: nil, poll_interval: POLL_INTERVAL, platform: nil, right_margin: 0, clamp_fixed_width: false) ⇒ TerminalViewport
constructor
A new instance of TerminalViewport.
- #install ⇒ Object
- #refresh ⇒ Object
- #refresh_needed? ⇒ Boolean
- #restore ⇒ Object
Constructor Details
#initialize(width: nil, provider: nil, poll_interval: POLL_INTERVAL, platform: nil, right_margin: 0, clamp_fixed_width: false) ⇒ TerminalViewport
Returns a new instance of TerminalViewport.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/keisanjaku/terminal_viewport.rb', line 11 def initialize(width: nil, provider: nil, poll_interval: POLL_INTERVAL, platform: nil, right_margin: 0, clamp_fixed_width: false) @requested_width = width @fixed = !width.nil? @provider = provider || -> { IO.console&.winsize&.last || Renderer::DEFAULT_WIDTH } @poll_interval = poll_interval @platform = platform || RbConfig::CONFIG["host_os"] @right_margin = [right_margin.to_i, 0].max @clamp_fixed_width = clamp_fixed_width @dirty = false @last_poll = Time.now @previous_winch_handler = nil @width = normalize_width(target_width) end |
Instance Attribute Details
#width ⇒ Object (readonly)
Returns the value of attribute width.
9 10 11 |
# File 'lib/keisanjaku/terminal_viewport.rb', line 9 def width @width end |
Instance Method Details
#current_width ⇒ Object
45 46 47 48 49 50 |
# File 'lib/keisanjaku/terminal_viewport.rb', line 45 def current_width return width if fixed_width? refresh if @dirty || poll_due? width end |
#fixed_width? ⇒ Boolean
70 71 72 |
# File 'lib/keisanjaku/terminal_viewport.rb', line 70 def fixed_width? @fixed && !@clamp_fixed_width end |
#install ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/keisanjaku/terminal_viewport.rb', line 25 def install return self if fixed_width? return self unless Signal.list.key?("WINCH") return self if windows? @previous_winch_handler = Signal.trap("WINCH") { @dirty = true } self rescue ArgumentError self end |
#refresh ⇒ Object
63 64 65 66 67 68 |
# File 'lib/keisanjaku/terminal_viewport.rb', line 63 def refresh @width = normalize_width(target_width) @dirty = false @last_poll = Time.now width end |
#refresh_needed? ⇒ Boolean
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/keisanjaku/terminal_viewport.rb', line 52 def refresh_needed? return false if fixed_width? signaled = @dirty return false unless signaled || poll_due? previous_width = @width refresh signaled || previous_width != @width end |
#restore ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/keisanjaku/terminal_viewport.rb', line 36 def restore return unless @previous_winch_handler Signal.trap("WINCH", @previous_winch_handler) @previous_winch_handler = nil rescue ArgumentError nil end |