Class: Binocs::TUI::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/binocs/tui/window.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(height:, width:, top:, left:) ⇒ Window

Returns a new instance of Window.



8
9
10
11
12
13
14
# File 'lib/binocs/tui/window.rb', line 8

def initialize(height:, width:, top:, left:)
  @height = height
  @width = width
  @top = top
  @left = left
  @win = Curses::Window.new(height, width, top, left)
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



6
7
8
# File 'lib/binocs/tui/window.rb', line 6

def height
  @height
end

#leftObject (readonly)

Returns the value of attribute left.



6
7
8
# File 'lib/binocs/tui/window.rb', line 6

def left
  @left
end

#topObject (readonly)

Returns the value of attribute top.



6
7
8
# File 'lib/binocs/tui/window.rb', line 6

def top
  @top
end

#widthObject (readonly)

Returns the value of attribute width.



6
7
8
# File 'lib/binocs/tui/window.rb', line 6

def width
  @width
end

#winObject (readonly)

Returns the value of attribute win.



6
7
8
# File 'lib/binocs/tui/window.rb', line 6

def win
  @win
end

Instance Method Details

#clearObject



24
25
26
# File 'lib/binocs/tui/window.rb', line 24

def clear
  @win.erase  # Use erase instead of clear to reduce flicker
end

#closeObject



28
29
30
# File 'lib/binocs/tui/window.rb', line 28

def close
  @win.close
end

#copy_to_clipboard(text) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/binocs/tui/window.rb', line 97

def copy_to_clipboard(text)
  if RbConfig::CONFIG['host_os'] =~ /darwin/
    IO.popen('pbcopy', 'w') { |io| io.write(text) }
    true
  elsif system('which xclip > /dev/null 2>&1')
    IO.popen('xclip -selection clipboard', 'w') { |io| io.write(text) }
    true
  elsif system('which xsel > /dev/null 2>&1')
    IO.popen('xsel --clipboard --input', 'w') { |io| io.write(text) }
    true
  elsif system('which wl-copy > /dev/null 2>&1')
    IO.popen('wl-copy', 'w') { |io| io.write(text) }
    true
  else
    false
  end
rescue
  false
end

#draw_box(title = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
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
# File 'lib/binocs/tui/window.rb', line 41

def draw_box(title = nil)
  @win.attron(Curses.color_pair(Colors::BORDER)) do
    # Draw corners
    @win.setpos(0, 0)
    @win.addstr('')
    @win.setpos(0, @width - 1)
    @win.addstr('')
    @win.setpos(@height - 1, 0)
    @win.addstr('')
    @win.setpos(@height - 1, @width - 1)
    @win.addstr('')

    # Draw horizontal lines
    (1...@width - 1).each do |x|
      @win.setpos(0, x)
      @win.addstr('')
      @win.setpos(@height - 1, x)
      @win.addstr('')
    end

    # Draw vertical lines
    (1...@height - 1).each do |y|
      @win.setpos(y, 0)
      @win.addstr('')
      @win.setpos(y, @width - 1)
      @win.addstr('')
    end
  end

  # Draw title if provided
  if title
    @win.attron(Curses.color_pair(Colors::TITLE) | Curses::A_BOLD) do
      title_text = " #{title} "
      @win.setpos(0, 2)
      @win.addstr(title_text)
    end
  end
end

#noutrefreshObject



20
21
22
# File 'lib/binocs/tui/window.rb', line 20

def noutrefresh
  @win.noutrefresh
end

#refreshObject



16
17
18
# File 'lib/binocs/tui/window.rb', line 16

def refresh
  @win.refresh
end

#resize(height, width, top, left) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/binocs/tui/window.rb', line 32

def resize(height, width, top, left)
  @win.close
  @height = height
  @width = width
  @top = top
  @left = left
  @win = Curses::Window.new(height, width, top, left)
end

#write(y, x, text, color_pair = Colors::NORMAL, attrs = 0) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/binocs/tui/window.rb', line 80

def write(y, x, text, color_pair = Colors::NORMAL, attrs = 0)
  return if y < 0 || y >= @height || x < 0

  @win.attron(Curses.color_pair(color_pair) | attrs) do
    @win.setpos(y, x)
    # Truncate text if it would overflow
    max_len = @width - x
    truncated = text.to_s[0, max_len]
    @win.addstr(truncated)
  end
end

#write_centered(y, text, color_pair = Colors::NORMAL, attrs = 0) ⇒ Object



92
93
94
95
# File 'lib/binocs/tui/window.rb', line 92

def write_centered(y, text, color_pair = Colors::NORMAL, attrs = 0)
  x = [(@width - text.length) / 2, 0].max
  write(y, x, text, color_pair, attrs)
end