Archived
1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
cli-old/lib/main.rb

176 lines
2.9 KiB
Ruby
Raw Normal View History

2017-07-21 02:48:04 -04:00
# frozen_string_literal: true
require 'thread'
require 'curses'
class Main
def self.inherited(_base)
raise "#{self} is final"
end
def self.mutex
(@mutex ||= Mutex.new).tap { freeze }
end
def initialize
raise "#{self.class} is singleton" unless self.class.mutex.try_lock
call
end
private
def call
before_loop
loop do
before_iteration
sleep
after_iteration
end
after_loop
end
def sleep
super 0.01
end
def before_loop
Curses.init_screen
Curses.start_color
Curses.noecho # do no echo input
Curses.curs_set 0 # invisible cursor
Curses.timeout = 0 # non-blocking input
Curses.stdscr.keypad = true
Curses.init_pair 1, Curses::COLOR_WHITE, Curses::COLOR_BLACK
Curses.init_pair 2, Curses::COLOR_BLACK, Curses::COLOR_WHITE
2017-07-21 08:54:09 -04:00
Curses.init_pair 3, Curses::COLOR_BLUE, Curses::COLOR_BLACK
2017-07-21 07:41:05 -04:00
initials
2017-07-21 02:48:04 -04:00
end
def after_loop
Curses.close_screen
end
2017-07-21 07:41:05 -04:00
def before_iteration
render
end
2017-07-21 02:48:04 -04:00
def after_iteration
loop do
event = Curses.getch
break if event.nil?
handle event
end
end
2017-07-21 07:54:07 -04:00
def initials
2017-07-21 08:54:09 -04:00
@search = Search.new 0, 0, Curses.stdscr.maxx, 1
@list = List.new(
0, 1,
Curses.stdscr.maxx, Curses.stdscr.maxy - 1,
1.upto(Curses.stdscr.maxy - 1 + 10).map do
['Qwe'].*(3 * (1 + rand(10))).join(' ')
end
)
2017-07-21 07:54:07 -04:00
end
2017-07-21 07:41:05 -04:00
2017-07-21 08:03:53 -04:00
def handle(event)
case event
2017-07-21 08:57:38 -04:00
when /[a-zA-Z0-9 _-]/
@search.append event
2017-07-21 08:59:16 -04:00
when Curses::Key::BACKSPACE
@search.backspace
2017-07-21 08:03:53 -04:00
when Curses::Key::UP
@list.up
2017-07-21 08:03:53 -04:00
when Curses::Key::DOWN
@list.down
2017-07-21 08:25:17 -04:00
end
2017-07-21 08:03:53 -04:00
end
2017-07-21 07:41:05 -04:00
2017-07-21 07:54:07 -04:00
def render
Curses.clear
2017-07-21 08:54:09 -04:00
@search.render
@list.render
Curses.refresh
end
end
2017-07-21 08:18:29 -04:00
2017-07-21 08:54:09 -04:00
class Search
attr_reader :x, :y, :width, :height, :text
def initialize(x, y, width, height)
@x = x
@y = y
@width = width
@height = height
2017-07-21 08:57:38 -04:00
@text = ''
2017-07-21 08:54:09 -04:00
end
def render
Curses.attron Curses.color_pair 3
Curses.setpos x, y
Curses.addstr text
end
2017-07-21 08:57:38 -04:00
def append(c)
@text += c
end
2017-07-21 08:59:16 -04:00
def backspace
@text = text[0...-1]
end
2017-07-21 08:54:09 -04:00
end
class List
2017-07-21 08:40:45 -04:00
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
2017-07-21 08:40:45 -04:00
items[top...(top + height)].each_with_index.each do |item, offset|
index = top + offset
2017-07-21 08:20:31 -04:00
2017-07-21 08:40:45 -04:00
if index == active
2017-07-21 07:54:07 -04:00
Curses.attron Curses.color_pair 2
else
Curses.attron Curses.color_pair 1
end
2017-07-21 08:40:45 -04:00
Curses.setpos y + offset, x
Curses.addstr "#{index}: #{item}".ljust width
2017-07-21 07:54:07 -04:00
end
end
2017-07-21 07:54:07 -04:00
def up
@active -= 1
2017-07-21 08:40:45 -04:00
@active = items.count - 1 if active.negative?
update
end
def down
@active += 1
2017-07-21 08:40:45 -04:00
@active = 0 if active >= items.count
update
end
def update
2017-07-21 08:40:45 -04:00
if active < top
@top = active
elsif active >= top + height
@top = active - height + 1
end
2017-07-21 07:54:07 -04:00
end
2017-07-21 02:48:04 -04:00
end