Move peers list to separate component
This commit is contained in:
parent
1094635d45
commit
f5c42e03a7
3 changed files with 75 additions and 24 deletions
36
lib/main.rb
36
lib/main.rb
|
@ -3,8 +3,7 @@
|
|||
require 'thread'
|
||||
require 'curses'
|
||||
|
||||
require 'widgets/search'
|
||||
require 'widgets/list'
|
||||
require 'widgets/peers'
|
||||
|
||||
class Main
|
||||
def self.inherited(_base)
|
||||
|
@ -69,22 +68,13 @@ private
|
|||
end
|
||||
|
||||
def initials
|
||||
@search = Widgets::Search.new 0, 0, Curses.stdscr.maxx, 1
|
||||
|
||||
@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
|
||||
)
|
||||
@peers = Widgets::Peers.new 0, 0, Curses.stdscr.maxx / 2, Curses.stdscr.maxy
|
||||
end
|
||||
|
||||
def render
|
||||
Curses.clear
|
||||
|
||||
@search.render
|
||||
@list.render
|
||||
@peers.render
|
||||
|
||||
Curses.refresh
|
||||
end
|
||||
|
@ -92,23 +82,23 @@ private
|
|||
def handle(event)
|
||||
case event
|
||||
when /[a-zA-Z0-9 _-]/
|
||||
@search.putc event
|
||||
@peers.putc event
|
||||
when Curses::Key::LEFT
|
||||
@search.left
|
||||
@peers.left
|
||||
when Curses::Key::RIGHT
|
||||
@search.right
|
||||
@peers.right
|
||||
when Curses::Key::HOME
|
||||
@search.home
|
||||
when Curses::Key.const_get(:END)
|
||||
@search.end
|
||||
@peers.home
|
||||
when Curses::Key::END
|
||||
@peers.endk
|
||||
when Curses::Key::BACKSPACE
|
||||
@search.backspace
|
||||
@peers.backspace
|
||||
when Curses::Key::DC
|
||||
@search.delete
|
||||
@peers.delete
|
||||
when Curses::Key::UP
|
||||
@list.up
|
||||
@peers.up
|
||||
when Curses::Key::DOWN
|
||||
@list.down
|
||||
@peers.down
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
61
lib/widgets/peers.rb
Normal file
61
lib/widgets/peers.rb
Normal 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
|
|
@ -55,7 +55,7 @@ module Widgets
|
|||
@cursor_pos = 0
|
||||
end
|
||||
|
||||
def end
|
||||
def endk
|
||||
@cursor_pos = @text.length
|
||||
end
|
||||
|
||||
|
|
Reference in a new issue