Skip to content

Instantly share code, notes, and snippets.

@Prigin
Created May 17, 2020 17:28
Show Gist options
  • Save Prigin/f770ec9715fc4b2d65a2d23e7bc8b17d to your computer and use it in GitHub Desktop.
Save Prigin/f770ec9715fc4b2d65a2d23e7bc8b17d to your computer and use it in GitHub Desktop.
class Log
def initialize(line = 0, col = 0, line_len = 10, size = 6)
@line = line
@col = col
@buff = [''] * size
@line_len = line_len
end
def push(str)
str = str.chomp
loop do
@buff.shift
@buff.push(str.slice!(0, @line_len).ljust(@line_len))
break if str == ''
end
@buff.each_index do |i|
print "\033[#{@line + i};#{@col}H#{@buff[i]}"
end
end
end
class Line
def initialize(line = 0, col = 0)
@line = line
@col = col
@size = 0
end
def push(str)
print "\033[#{@line};#{@col}H#{str.ljust(@size)}"
@size = str.size
end
end
class List
#attr_accessor :line, :col
def initialize(line = 0, col = 0)
@line = line
@col = col
@list = []
end
def push(obj)
flag = false
if obj.last == :delete
@list.delete(obj.first.chomp.ljust(13))
flag = true
elsif obj.last == :add
@list.push(obj.first.chomp.ljust(13))
end
refresh(flag)
end
def refresh(flag = false)
i = 0
@list.each do |u|
print "\e[#{@line + i};#{@col}H#{u}"
i += 1
end
print "\e[#{@line + i};#{@col}H#{' ' * 13}" if flag == true
end
end
require 'io/console'
require_relative 'scr_samples'
# screen configuration
class Screen
attr_reader :in
def initialize(units_hash)
@units_hash = units_hash
@in = Queue.new
@loop = Thread.new { loop { refresh } }
end
def refresh
hsh, str = @in.deq
@units_hash[hsh].push(str)
end
def stop
@loop.kill
print "\033[J"
end
end
# input handlig
class Input
def initialize(cmd_len: 30, cmd: :cmd, scr_queue: nil)
@cmd_len = cmd_len
@cmd = cmd
@scr_queue = scr_queue
end
# check command
def check_com(str)
str != '/close'
end
def key_read
buff = ''
tr = true
while tr
in_key = $stdin.noecho(&:getch)
if in_key == "\r"
tr = check_com(buff)
buff = ''
elsif in_key == "\b"
buff.chop!
elsif buff.size < @cmd_len
buff += in_key
end
@scr_queue << [@cmd, buff]
end
end
end
# TEST
# initializing
scr = Screen.new(cmd: Line.new(10, 0), clock: Line.new(0, 0))
in_keyboard = Input.new(scr_queue: scr.in)
# loops and input
Thread.new do # date and time
loop do
scr.in << [:clock, Time.new.to_s]
sleep 1
end
end
in_keyboard.key_read # could be run in thread
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment