2017-07-21 13:40:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Widgets
|
2017-07-27 19:47:48 -04:00
|
|
|
class Text < Curses::React::Component
|
2017-07-25 12:36:19 -04:00
|
|
|
def trigger(event)
|
|
|
|
case event
|
2017-07-26 04:07:36 -04:00
|
|
|
when Events::Text::Enter
|
|
|
|
props[:on_enter].call
|
|
|
|
|
2017-07-25 12:36:19 -04:00
|
|
|
when Events::Text::Putc
|
|
|
|
props[:on_putc].call event.char
|
2017-07-25 13:36:05 -04:00
|
|
|
|
|
|
|
when Events::Text::Left
|
|
|
|
props[:on_left].call
|
|
|
|
when Events::Text::Right
|
|
|
|
props[:on_right].call
|
|
|
|
when Events::Text::Home
|
|
|
|
props[:on_home].call
|
|
|
|
when Events::Text::End
|
|
|
|
props[:on_end].call
|
2017-07-25 13:41:55 -04:00
|
|
|
|
|
|
|
when Events::Text::Backspace
|
|
|
|
props[:on_backspace].call
|
|
|
|
when Events::Text::Delete
|
|
|
|
props[:on_delete].call
|
2017-07-25 12:36:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-25 10:48:37 -04:00
|
|
|
private
|
|
|
|
|
2017-07-27 20:02:55 -04:00
|
|
|
def render
|
2017-07-25 10:36:28 -04:00
|
|
|
total = props[:width] - 1
|
|
|
|
start = [0, props[:cursor_pos] - total].max
|
2017-07-21 13:40:34 -04:00
|
|
|
|
2017-07-25 10:36:28 -04:00
|
|
|
cut = props[:text][start...start + total]
|
2017-07-21 13:40:34 -04:00
|
|
|
|
2017-07-22 10:31:38 -04:00
|
|
|
setpos 0, 0
|
2017-07-21 13:40:34 -04:00
|
|
|
|
2017-07-25 10:36:28 -04:00
|
|
|
before_cursor = cut[0...props[:cursor_pos]]
|
|
|
|
under_cursor = cut[props[:cursor_pos]] || ' '
|
|
|
|
after_cursor = cut[(1 + props[:cursor_pos])..-1] || ''
|
2017-07-21 13:40:34 -04:00
|
|
|
|
2017-07-24 05:52:42 -04:00
|
|
|
Style.default.editing_text window do
|
2017-07-22 14:46:50 -04:00
|
|
|
addstr before_cursor
|
2017-07-22 05:46:15 -04:00
|
|
|
end
|
2017-07-21 13:40:34 -04:00
|
|
|
|
2017-07-25 10:36:28 -04:00
|
|
|
Style.default.public_send props[:focused] ? :cursor : :editing_text, window do
|
2017-07-22 14:46:50 -04:00
|
|
|
addstr under_cursor
|
2017-07-22 05:46:15 -04:00
|
|
|
end
|
2017-07-21 13:40:34 -04:00
|
|
|
|
2017-07-24 05:52:42 -04:00
|
|
|
Style.default.editing_text window do
|
2017-07-22 14:46:50 -04:00
|
|
|
addstr after_cursor
|
2017-07-22 05:46:15 -04:00
|
|
|
end
|
2017-07-21 13:40:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|