From 3612081d64230eb517b4418cfe1bb8756b96213e Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Fri, 21 Jul 2017 18:17:40 +0000 Subject: [PATCH] Move class Widgets::List to Widgets::Peers:List --- lib/main.rb | 2 +- lib/widgets/list.rb | 81 -------------------------------------- lib/widgets/peers.rb | 2 +- lib/widgets/peers/list.rb | 83 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 83 deletions(-) delete mode 100644 lib/widgets/list.rb create mode 100644 lib/widgets/peers/list.rb diff --git a/lib/main.rb b/lib/main.rb index 5f23e59..b3d8e2b 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -15,7 +15,7 @@ require 'widgets/messenger' require 'widgets/menu' require 'widgets/peers' -require 'widgets/list' +require 'widgets/peers/list' require 'widgets/search' require 'widgets/chat' diff --git a/lib/widgets/list.rb b/lib/widgets/list.rb deleted file mode 100644 index 34b15e5..0000000 --- a/lib/widgets/list.rb +++ /dev/null @@ -1,81 +0,0 @@ -# frozen_string_literal: true - -module Widgets - class List - attr_reader :x, :y, :width, :height - attr_accessor :focused - - attr_reader :active, :top, :items - - def initialize(x, y, width, height) - @x = x - @y = y - - @width = width - @height = height - - @focused = false - - @active = 0 - @top = 0 - - @items = 1.upto(height - 1 + 10).map do - Faker::Name.name - end - end - - def render - items[top...(top + height)].each_with_index.each do |item, offset| - index = top + offset - - if index == active && focused - Curses.attron Curses.color_pair 2 - else - Curses.attron Curses.color_pair 1 - end - - Curses.setpos y + offset, x - - s = "#{index}: #{item}" - if s.length <= width - Curses.addstr s.ljust width - else - Curses.addstr "#{s[0...width - 3]}..." - end - end - end - - def trigger(event) - case event - when Events::Panel::Up - up - when Events::Panel::Down - down - end - end - - def up - @active -= 1 - update - end - - def down - @active += 1 - update - end - - def update - if active.negative? - @active = items.count - 1 - elsif active >= items.count - @active = 0 - end - - if active < top - @top = active - elsif active >= top + height - @top = active - height + 1 - end - end - end -end diff --git a/lib/widgets/peers.rb b/lib/widgets/peers.rb index e53965a..6276f61 100644 --- a/lib/widgets/peers.rb +++ b/lib/widgets/peers.rb @@ -8,7 +8,7 @@ module Widgets @focused = false @search = Widgets::Search.new x, y, width, 1 - @list = Widgets::List.new x, y + 1, width, height - 1 + @list = List.new x, y + 1, width, height - 1 end def render diff --git a/lib/widgets/peers/list.rb b/lib/widgets/peers/list.rb new file mode 100644 index 0000000..96ec1c2 --- /dev/null +++ b/lib/widgets/peers/list.rb @@ -0,0 +1,83 @@ +# frozen_string_literal: true + +module Widgets + class Peers + class List + attr_reader :x, :y, :width, :height + attr_accessor :focused + + attr_reader :active, :top, :items + + def initialize(x, y, width, height) + @x = x + @y = y + + @width = width + @height = height + + @focused = false + + @active = 0 + @top = 0 + + @items = 1.upto(height - 1 + 10).map do + Faker::Name.name + end + end + + def render + items[top...(top + height)].each_with_index.each do |item, offset| + index = top + offset + + if index == active && focused + Curses.attron Curses.color_pair 2 + else + Curses.attron Curses.color_pair 1 + end + + Curses.setpos y + offset, x + + s = "#{index}: #{item}" + if s.length <= width + Curses.addstr s.ljust width + else + Curses.addstr "#{s[0...width - 3]}..." + end + end + end + + def trigger(event) + case event + when Events::Panel::Up + up + when Events::Panel::Down + down + end + end + + def up + @active -= 1 + update + end + + def down + @active += 1 + update + end + + def update + if active.negative? + @active = items.count - 1 + elsif active >= items.count + @active = 0 + end + + if active < top + @top = active + elsif active >= top + height + @top = active - height + 1 + end + end + end + end +end