Archived
1
0
Fork 0

Move peers list to separate component

This commit is contained in:
Braiden Vasco 2017-07-21 14:43:08 +00:00
parent 1094635d45
commit f5c42e03a7
3 changed files with 75 additions and 24 deletions

View file

@ -3,8 +3,7 @@
require 'thread' require 'thread'
require 'curses' require 'curses'
require 'widgets/search' require 'widgets/peers'
require 'widgets/list'
class Main class Main
def self.inherited(_base) def self.inherited(_base)
@ -69,22 +68,13 @@ private
end end
def initials def initials
@search = Widgets::Search.new 0, 0, Curses.stdscr.maxx, 1 @peers = Widgets::Peers.new 0, 0, Curses.stdscr.maxx / 2, Curses.stdscr.maxy
@list = Widgets::List.new(
0, 1,
Curses.stdscr.maxx, Curses.stdscr.maxy - 1,
1.upto(Curses.stdscr.maxy - 1 + 10).map do
['Qwe'].*(3 * (1 + rand(15))).join(' ')
end
)
end end
def render def render
Curses.clear Curses.clear
@search.render @peers.render
@list.render
Curses.refresh Curses.refresh
end end
@ -92,23 +82,23 @@ private
def handle(event) def handle(event)
case event case event
when /[a-zA-Z0-9 _-]/ when /[a-zA-Z0-9 _-]/
@search.putc event @peers.putc event
when Curses::Key::LEFT when Curses::Key::LEFT
@search.left @peers.left
when Curses::Key::RIGHT when Curses::Key::RIGHT
@search.right @peers.right
when Curses::Key::HOME when Curses::Key::HOME
@search.home @peers.home
when Curses::Key.const_get(:END) when Curses::Key::END
@search.end @peers.endk
when Curses::Key::BACKSPACE when Curses::Key::BACKSPACE
@search.backspace @peers.backspace
when Curses::Key::DC when Curses::Key::DC
@search.delete @peers.delete
when Curses::Key::UP when Curses::Key::UP
@list.up @peers.up
when Curses::Key::DOWN when Curses::Key::DOWN
@list.down @peers.down
end end
end end
end end

61
lib/widgets/peers.rb Normal file
View file

@ -0,0 +1,61 @@
# frozen_string_literal: true
require 'widgets/search'
require 'widgets/list'
module Widgets
class Peers
def initialize(x, y, width, height)
@search = Widgets::Search.new x, y, width, 1
@list = Widgets::List.new(
x, y + 1,
width, height - 1,
1.upto(height - 1 + 10).map do
['Qwe'].*(3 * (1 + rand(15))).join(' ')
end
)
end
def render
@search.render
@list.render
end
def putc(event)
@search.putc event
end
def left
@search.left
end
def right
@search.right
end
def home
@search.home
end
def endk
@search.endk
end
def backspace
@search.backspace
end
def delete
@search.delete
end
def up
@list.up
end
def down
@list.down
end
end
end

View file

@ -55,7 +55,7 @@ module Widgets
@cursor_pos = 0 @cursor_pos = 0
end end
def end def endk
@cursor_pos = @text.length @cursor_pos = @text.length
end end