Archived
1
0
Fork 0

Move widgets to separate files

This commit is contained in:
Braiden Vasco 2017-07-21 14:00:01 +00:00
parent c211b59660
commit 137dff5d39
3 changed files with 145 additions and 136 deletions

View file

@ -3,6 +3,9 @@
require 'thread'
require 'curses'
require 'widgets/search'
require 'widgets/list'
class Main
def self.inherited(_base)
raise "#{self} is final"
@ -66,9 +69,9 @@ private
end
def initials
@search = Search.new 1, 1, Curses.stdscr.maxx - 2, 1
@search = Widgets::Search.new 1, 1, Curses.stdscr.maxx - 2, 1
@list = List.new(
@list = Widgets::List.new(
1, 2,
Curses.stdscr.maxx - 2, Curses.stdscr.maxy - 3,
1.upto(Curses.stdscr.maxy - 1 + 10).map do
@ -109,137 +112,3 @@ private
Curses.refresh
end
end
class Search
attr_reader :x, :y, :width, :height, :text, :cursor_pos
def initialize(x, y, width, height)
@x = x
@y = y
@width = width
@height = height
@text = ''
@cursor_pos = 0
end
def render
total = width - 1
start = [0, cursor_pos - total].max
cut = text[start...start + total]
Curses.setpos x, y
before_cursor = cut[0...cursor_pos]
under_cursor = cut[cursor_pos] || ' '
after_cursor = cut[(1 + cursor_pos)..-1] || ''
Curses.attron Curses.color_pair 3
Curses.addstr before_cursor
Curses.attron Curses.color_pair 4
Curses.addstr under_cursor
Curses.attron Curses.color_pair 3
Curses.addstr after_cursor
end
def putc(c)
@text = "#{text[0...cursor_pos]}#{c}#{text[cursor_pos..-1]}"
@cursor_pos += 1
update
end
def left
@cursor_pos -= 1
update
end
def right
@cursor_pos += 1
update
end
def home
@cursor_pos = 0
end
def end
@cursor_pos = @text.length
end
def backspace
return unless cursor_pos.positive?
@text = "#{text[0...(cursor_pos - 1)]}#{text[cursor_pos..-1]}"
@cursor_pos -= 1
update
end
def delete
return if cursor_pos > text.length
@text = "#{text[0...cursor_pos]}#{text[(cursor_pos + 1)..-1]}"
end
def update
if @cursor_pos.negative?
@cursor_pos = 0
elsif @cursor_pos > @text.length
@cursor_pos = @text.length
end
end
end
class List
attr_reader :x, :y, :width, :height, :active, :top, :items
def initialize(x, y, width, height, items)
@x = x
@y = y
@width = width
@height = height
@active = 0
@top = 0
@items = Array(items)
end
def render
items[top...(top + height)].each_with_index.each do |item, offset|
index = top + offset
if index == active
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 up
@active -= 1
@active = items.count - 1 if active.negative?
update
end
def down
@active += 1
@active = 0 if active >= items.count
update
end
def update
if active < top
@top = active
elsif active >= top + height
@top = active - height + 1
end
end
end

58
lib/widgets/list.rb Normal file
View file

@ -0,0 +1,58 @@
# frozen_string_literal: true
module Widgets
class List
attr_reader :x, :y, :width, :height, :active, :top, :items
def initialize(x, y, width, height, items)
@x = x
@y = y
@width = width
@height = height
@active = 0
@top = 0
@items = Array(items)
end
def render
items[top...(top + height)].each_with_index.each do |item, offset|
index = top + offset
if index == active
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 up
@active -= 1
@active = items.count - 1 if active.negative?
update
end
def down
@active += 1
@active = 0 if active >= items.count
update
end
def update
if active < top
@top = active
elsif active >= top + height
@top = active - height + 1
end
end
end
end

82
lib/widgets/search.rb Normal file
View file

@ -0,0 +1,82 @@
# frozen_string_literal: true
module Widgets
class Search
attr_reader :x, :y, :width, :height, :text, :cursor_pos
def initialize(x, y, width, height)
@x = x
@y = y
@width = width
@height = height
@text = ''
@cursor_pos = 0
end
def render
total = width - 1
start = [0, cursor_pos - total].max
cut = text[start...start + total]
Curses.setpos x, y
before_cursor = cut[0...cursor_pos]
under_cursor = cut[cursor_pos] || ' '
after_cursor = cut[(1 + cursor_pos)..-1] || ''
Curses.attron Curses.color_pair 3
Curses.addstr before_cursor
Curses.attron Curses.color_pair 4
Curses.addstr under_cursor
Curses.attron Curses.color_pair 3
Curses.addstr after_cursor
end
def putc(c)
@text = "#{text[0...cursor_pos]}#{c}#{text[cursor_pos..-1]}"
@cursor_pos += 1
update
end
def left
@cursor_pos -= 1
update
end
def right
@cursor_pos += 1
update
end
def home
@cursor_pos = 0
end
def end
@cursor_pos = @text.length
end
def backspace
return unless cursor_pos.positive?
@text = "#{text[0...(cursor_pos - 1)]}#{text[cursor_pos..-1]}"
@cursor_pos -= 1
update
end
def delete
return if cursor_pos > text.length
@text = "#{text[0...cursor_pos]}#{text[(cursor_pos + 1)..-1]}"
end
def update
if @cursor_pos.negative?
@cursor_pos = 0
elsif @cursor_pos > @text.length
@cursor_pos = @text.length
end
end
end
end