Add method Main#state
This commit is contained in:
parent
803c2a30f3
commit
4f1e6c4ed1
1 changed files with 42 additions and 40 deletions
82
lib/main.rb
82
lib/main.rb
|
@ -7,45 +7,6 @@ require 'faker'
|
|||
require 'screen'
|
||||
|
||||
class Main
|
||||
INITIAL_STATE = {
|
||||
focus: :sidebar,
|
||||
sidebar: {
|
||||
focus: :menu,
|
||||
menu: {
|
||||
focused: true,
|
||||
active: 0,
|
||||
top: 0,
|
||||
items: 1.upto(Curses.stdscr.maxy).map do
|
||||
{
|
||||
name: Faker::Name.name,
|
||||
online: [false, true].sample,
|
||||
}
|
||||
end,
|
||||
}.freeze,
|
||||
}.freeze,
|
||||
chat: {
|
||||
focus: :new_message,
|
||||
info: {
|
||||
name: Faker::Name.name,
|
||||
public_key: SecureRandom.hex(32),
|
||||
}.freeze,
|
||||
new_message: {
|
||||
text: '',
|
||||
cursor_pos: 0,
|
||||
}.freeze,
|
||||
history: {
|
||||
messages: 1.upto(100).map do
|
||||
{
|
||||
out: rand <= 0.2,
|
||||
time: Faker::Time.forward,
|
||||
name: Faker::Name.name,
|
||||
text: Faker::Lorem.sentence,
|
||||
}
|
||||
end,
|
||||
}.freeze,
|
||||
}.freeze,
|
||||
}.freeze
|
||||
|
||||
def self.inherited(_base)
|
||||
raise "#{self} is final"
|
||||
end
|
||||
|
@ -85,11 +46,52 @@ private
|
|||
end
|
||||
|
||||
def before_iteration
|
||||
@screen.props = INITIAL_STATE
|
||||
@screen.props = state
|
||||
@screen.render
|
||||
end
|
||||
|
||||
def after_iteration
|
||||
@screen.poll
|
||||
end
|
||||
|
||||
def state
|
||||
@state ||= {
|
||||
focus: :sidebar,
|
||||
sidebar: {
|
||||
focus: :menu,
|
||||
menu: {
|
||||
focused: true,
|
||||
active: 0,
|
||||
top: 0,
|
||||
items: 1.upto(Curses.stdscr.maxy).map do
|
||||
{
|
||||
name: Faker::Name.name,
|
||||
online: [false, true].sample,
|
||||
}
|
||||
end,
|
||||
}.freeze,
|
||||
}.freeze,
|
||||
chat: {
|
||||
focus: :new_message,
|
||||
info: {
|
||||
name: Faker::Name.name,
|
||||
public_key: SecureRandom.hex(32),
|
||||
}.freeze,
|
||||
new_message: {
|
||||
text: '',
|
||||
cursor_pos: 0,
|
||||
}.freeze,
|
||||
history: {
|
||||
messages: 1.upto(100).map do
|
||||
{
|
||||
out: rand <= 0.2,
|
||||
time: Faker::Time.forward,
|
||||
name: Faker::Name.name,
|
||||
text: Faker::Lorem.sentence,
|
||||
}
|
||||
end,
|
||||
}.freeze,
|
||||
}.freeze,
|
||||
}.freeze
|
||||
end
|
||||
end
|
||||
|
|
Reference in a new issue