Add search bar cursor
This commit is contained in:
parent
23659fe732
commit
87627ab622
1 changed files with 28 additions and 3 deletions
31
lib/main.rb
31
lib/main.rb
|
@ -44,6 +44,7 @@ private
|
||||||
Curses.init_pair 1, Curses::COLOR_WHITE, Curses::COLOR_BLACK
|
Curses.init_pair 1, Curses::COLOR_WHITE, Curses::COLOR_BLACK
|
||||||
Curses.init_pair 2, Curses::COLOR_BLACK, Curses::COLOR_WHITE
|
Curses.init_pair 2, Curses::COLOR_BLACK, Curses::COLOR_WHITE
|
||||||
Curses.init_pair 3, Curses::COLOR_BLUE, Curses::COLOR_BLACK
|
Curses.init_pair 3, Curses::COLOR_BLUE, Curses::COLOR_BLACK
|
||||||
|
Curses.init_pair 4, Curses::COLOR_BLACK, Curses::COLOR_BLUE
|
||||||
|
|
||||||
initials
|
initials
|
||||||
end
|
end
|
||||||
|
@ -100,7 +101,7 @@ private
|
||||||
end
|
end
|
||||||
|
|
||||||
class Search
|
class Search
|
||||||
attr_reader :x, :y, :width, :height, :text
|
attr_reader :x, :y, :width, :height, :text, :cursor_pos
|
||||||
|
|
||||||
def initialize(x, y, width, height)
|
def initialize(x, y, width, height)
|
||||||
@x = x
|
@x = x
|
||||||
|
@ -108,20 +109,44 @@ class Search
|
||||||
@width = width
|
@width = width
|
||||||
@height = height
|
@height = height
|
||||||
@text = ''
|
@text = ''
|
||||||
|
@cursor_pos = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
def render
|
def render
|
||||||
Curses.attron Curses.color_pair 3
|
|
||||||
Curses.setpos x, y
|
Curses.setpos x, y
|
||||||
Curses.addstr text
|
|
||||||
|
before_cursor = text[0...cursor_pos]
|
||||||
|
under_cursor = text[cursor_pos]
|
||||||
|
after_cursor = text[cursor_pos..-1]
|
||||||
|
|
||||||
|
Curses.attron Curses.color_pair 3
|
||||||
|
Curses.addstr before_cursor
|
||||||
|
|
||||||
|
Curses.attron Curses.color_pair 4
|
||||||
|
Curses.addstr under_cursor || ' '
|
||||||
|
|
||||||
|
Curses.attron Curses.color_pair 3
|
||||||
|
Curses.addstr after_cursor
|
||||||
end
|
end
|
||||||
|
|
||||||
def append(c)
|
def append(c)
|
||||||
@text += c
|
@text += c
|
||||||
|
@cursor_pos += 1
|
||||||
|
update
|
||||||
end
|
end
|
||||||
|
|
||||||
def backspace
|
def backspace
|
||||||
@text = text[0...-1]
|
@text = text[0...-1]
|
||||||
|
@cursor_pos -= 1
|
||||||
|
update
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
if @cursor_pos.negative?
|
||||||
|
@cursor_pos = 0
|
||||||
|
elsif @cursor_pos > @text.length
|
||||||
|
@cursor_pos = @text.length
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Reference in a new issue