From d891c2026e08080419f9741e1ad9440c188c479c Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Tue, 25 Jul 2017 14:48:37 +0000 Subject: [PATCH] Organize code --- lib/widgets/base.rb | 15 ++++++++------- lib/widgets/chat.rb | 2 ++ lib/widgets/chat/history.rb | 2 ++ lib/widgets/chat/info.rb | 2 ++ lib/widgets/container.rb | 2 ++ lib/widgets/logo.rb | 2 ++ lib/widgets/main.rb | 14 ++++++++------ lib/widgets/menu.rb | 2 ++ lib/widgets/sidebar.rb | 14 ++++++++------ lib/widgets/text.rb | 2 ++ 10 files changed, 38 insertions(+), 19 deletions(-) diff --git a/lib/widgets/base.rb b/lib/widgets/base.rb index cf80ad1..81c50ad 100644 --- a/lib/widgets/base.rb +++ b/lib/widgets/base.rb @@ -2,18 +2,11 @@ module Widgets class Base - attr_reader :parent - attr_reader :props - def initialize(parent) @parent = parent @props = {}.freeze end - def window - @window ||= parent&.subwin(props[:x], props[:y], props[:width], props[:height]) || Curses.stdscr - end - def props=(value) raise TypeError, "expected props to be a #{Hash}" unless value.is_a? Hash raise ArgumentError, 'expected props to be frozen' unless value.frozen? @@ -29,6 +22,14 @@ module Widgets window.refresh end + private + + attr_reader :parent, :props + + def window + @window ||= parent&.subwin(props[:x], props[:y], props[:width], props[:height]) || Curses.stdscr + end + def draw raise NotImplementedError, "#{self.class}#draw" end diff --git a/lib/widgets/chat.rb b/lib/widgets/chat.rb index 6d7ebc6..43f6ccd 100644 --- a/lib/widgets/chat.rb +++ b/lib/widgets/chat.rb @@ -17,6 +17,8 @@ module Widgets @message.props = props[:new_message] end + private + def focus case props[:focus] when :info then @info diff --git a/lib/widgets/chat/history.rb b/lib/widgets/chat/history.rb index 65acb5c..79ba470 100644 --- a/lib/widgets/chat/history.rb +++ b/lib/widgets/chat/history.rb @@ -3,6 +3,8 @@ module Widgets class Chat < VPanel class History < Base + private + def draw offset = 0 diff --git a/lib/widgets/chat/info.rb b/lib/widgets/chat/info.rb index 2edd9d8..b91cd4e 100644 --- a/lib/widgets/chat/info.rb +++ b/lib/widgets/chat/info.rb @@ -3,6 +3,8 @@ module Widgets class Chat < VPanel class Info < Base + private + def draw setpos 0, 0 Style.default.online_mark window do diff --git a/lib/widgets/container.rb b/lib/widgets/container.rb index b089022..bf1023e 100644 --- a/lib/widgets/container.rb +++ b/lib/widgets/container.rb @@ -6,6 +6,8 @@ module Widgets window.subwin height, width, y, x end + private + def draw children.each(&:render) end diff --git a/lib/widgets/logo.rb b/lib/widgets/logo.rb index d1e7301..f4f2f6b 100644 --- a/lib/widgets/logo.rb +++ b/lib/widgets/logo.rb @@ -16,6 +16,8 @@ module Widgets WIDTH = LOGO.first.length HEIGHT = LOGO.length + private + def draw Style.default.logo window do LOGO.each_with_index do |s, index| diff --git a/lib/widgets/main.rb b/lib/widgets/main.rb index 53d91e1..a43cd6c 100644 --- a/lib/widgets/main.rb +++ b/lib/widgets/main.rb @@ -9,6 +9,14 @@ module Widgets @chat = Widgets::Chat.new self end + def props=(_value) + super + @sidebar.props = props[:sidebar] + @chat.props = props[:chat] + end + + private + def focus case props[:focus] when :sidebar then @sidebar @@ -16,12 +24,6 @@ module Widgets end end - def props=(_value) - super - @sidebar.props = props[:sidebar] - @chat.props = props[:chat] - end - def children [@sidebar, @chat] end diff --git a/lib/widgets/menu.rb b/lib/widgets/menu.rb index 7dc136f..6b05b81 100644 --- a/lib/widgets/menu.rb +++ b/lib/widgets/menu.rb @@ -2,6 +2,8 @@ module Widgets class Menu < Base + private + def draw props[:items][props[:top]...(props[:top] + props[:height])].each_with_index.each do |item, offset| index = props[:top] + offset diff --git a/lib/widgets/sidebar.rb b/lib/widgets/sidebar.rb index d408891..c61d882 100644 --- a/lib/widgets/sidebar.rb +++ b/lib/widgets/sidebar.rb @@ -9,6 +9,14 @@ module Widgets @menu = Menu.new self end + def props=(_value) + super + @logo.props = props[:logo] + @menu.props = props[:menu] + end + + private + def focus case props[:focus] when :menu @@ -16,12 +24,6 @@ module Widgets end end - def props=(_value) - super - @logo.props = props[:logo] - @menu.props = props[:menu] - end - def children [@logo, @menu] end diff --git a/lib/widgets/text.rb b/lib/widgets/text.rb index 26688c1..30deffb 100644 --- a/lib/widgets/text.rb +++ b/lib/widgets/text.rb @@ -2,6 +2,8 @@ module Widgets class Text < Base + private + def draw total = props[:width] - 1 start = [0, props[:cursor_pos] - total].max