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

184 lines
4.3 KiB
Ruby
Raw Normal View History

2017-07-21 02:48:04 -04:00
# frozen_string_literal: true
2017-07-25 19:06:37 -04:00
require 'tox'
2017-07-31 00:41:12 -04:00
require 'singleton'
2017-07-21 02:48:04 -04:00
2017-08-11 22:32:29 -04:00
require 'obredux'
require 'obredux/thunk'
2017-07-27 19:18:28 -04:00
require 'helpers'
2017-07-27 20:33:32 -04:00
require 'actions'
require 'reducer'
2017-07-24 08:48:22 -04:00
require 'screen'
2017-07-21 10:00:01 -04:00
2017-07-21 02:48:04 -04:00
class Main
2017-07-31 00:41:12 -04:00
include Singleton
2017-07-21 02:48:04 -04:00
2017-07-31 00:41:12 -04:00
SAVEDATA_FILENAME = File.expand_path '../savedata', __dir__
2017-07-21 02:48:04 -04:00
def initialize
call
end
private
2017-07-27 21:47:18 -04:00
def store
2017-08-11 21:58:10 -04:00
@store ||= Obredux::Store.new Reducer, [Obredux::Thunk.new]
2017-07-27 21:55:30 -04:00
end
def state
store.state.merge(
on_window_left: method(:on_window_left),
on_window_right: method(:on_window_right),
on_menu_up: method(:on_menu_up),
on_menu_down: method(:on_menu_down),
on_new_message_enter: method(:on_new_message_enter),
on_new_message_putc: method(:on_new_message_putc),
on_new_message_left: method(:on_new_message_left),
on_new_message_right: method(:on_new_message_right),
on_new_message_home: method(:on_new_message_home),
on_new_message_end: method(:on_new_message_end),
on_new_message_backspace: method(:on_new_message_backspace),
on_new_message_delete: method(:on_new_message_delete),
).freeze
2017-07-27 21:47:18 -04:00
end
2017-07-21 02:48:04 -04:00
def call
2017-07-27 21:55:30 -04:00
@screen = Screen.new
Style.default = Style.new
Reducer.screen_width = Curses.stdscr.maxx
Reducer.screen_height = Curses.stdscr.maxy
2017-07-25 19:13:06 -04:00
tox_options = Tox::Options.new
tox_options.savedata = File.binread SAVEDATA_FILENAME if File.exist? SAVEDATA_FILENAME
@tox_client = Tox::Client.new tox_options
2017-07-25 19:06:37 -04:00
2017-07-25 19:26:38 -04:00
on_friends_load @tox_client.friends
@tox_client.on_iteration(&method(:on_iteration))
2017-07-25 22:30:40 -04:00
@tox_client.on_friend_request(&method(:on_friend_request))
2017-07-25 21:17:18 -04:00
@tox_client.on_friend_message(&method(:on_friend_message))
@tox_client.on_friend_name_change(&method(:on_friend_name_change))
@tox_client.on_friend_status_message_change(&method(:on_friend_status_message_change))
@tox_client.on_friend_status_change(&method(:on_friend_status_change))
2017-07-25 21:17:18 -04:00
2017-07-25 22:13:11 -04:00
@tox_client.run
ensure
2017-07-25 19:13:06 -04:00
@screen&.close
File.binwrite SAVEDATA_FILENAME, @tox_client.savedata if @tox_client
2017-07-21 02:48:04 -04:00
end
2017-07-25 22:35:43 -04:00
#################
# Tox callbacks #
#################
def on_iteration
2017-07-24 08:48:22 -04:00
@screen.poll
2017-07-27 21:55:30 -04:00
@screen.props = state
@screen.draw
2017-07-21 08:03:53 -04:00
end
2017-07-24 18:38:23 -04:00
2017-07-25 19:26:38 -04:00
def on_friends_load(friends)
2017-07-27 21:47:18 -04:00
store.dispatch Actions::LoadFriends.new friends
2017-07-25 19:43:52 -04:00
end
2017-08-11 22:26:11 -04:00
def on_friend_request(public_key, text)
store.dispatch Actions::AddFriend.new @tox_client, public_key, text
2017-07-25 19:26:38 -04:00
end
2017-07-25 21:17:18 -04:00
def on_friend_message(friend, text)
store.dispatch Actions::AddFriendMessage.new friend, text
end
def on_friend_name_change(friend, name)
2017-07-27 21:47:18 -04:00
store.dispatch Actions::ChangeFriendName.new friend, name
end
def on_friend_status_message_change(friend, status_message)
2017-07-27 21:47:18 -04:00
store.dispatch Actions::ChangeFriendStatusMessage.new friend, status_message
end
def on_friend_status_change(friend, status)
2017-07-27 21:47:18 -04:00
store.dispatch Actions::ChangeFriendStatus.new friend, status
2017-07-25 21:17:18 -04:00
end
2017-07-25 22:35:43 -04:00
####################
# Screen callbacks #
####################
def on_window_left
2017-07-27 21:47:18 -04:00
store.dispatch Actions::WindowLeft.new
end
def on_window_right
2017-07-27 21:47:18 -04:00
store.dispatch Actions::WindowRight.new
end
2017-07-25 12:22:38 -04:00
def on_menu_up
2017-07-27 21:47:18 -04:00
store.dispatch Actions::MenuUp.new
2017-07-25 12:22:38 -04:00
end
def on_menu_down
2017-07-27 21:47:18 -04:00
store.dispatch Actions::MenuDown.new
2017-07-25 12:22:38 -04:00
end
2017-07-25 12:36:19 -04:00
2017-07-26 04:07:36 -04:00
def on_new_message_enter
return if state[:data][:active_friend_index].nil?
friend_number = state[:data][:friends].keys[state[:data][:active_friend_index]]
return if friend_number.nil?
text = state[:data][:friends][friend_number][:new_message][:text].strip.freeze
return if text.empty?
friend = @tox_client.friend friend_number
error = false
begin
friend.send_message text
rescue
error = true
end
store.dispatch Actions::AddOutFriendMessage.new friend, text, error
2017-07-26 04:07:36 -04:00
end
2017-07-25 12:36:19 -04:00
def on_new_message_putc(char)
2017-07-27 21:47:18 -04:00
store.dispatch Actions::NewMessagePutc.new char
2017-07-25 12:36:19 -04:00
end
2017-07-25 13:21:15 -04:00
2017-07-25 13:36:05 -04:00
def on_new_message_left
2017-07-27 21:47:18 -04:00
store.dispatch Actions::NewMessageLeft.new
2017-07-25 13:36:05 -04:00
end
def on_new_message_right
2017-07-27 21:47:18 -04:00
store.dispatch Actions::NewMessageRight.new
2017-07-25 13:36:05 -04:00
end
def on_new_message_home
2017-07-27 21:47:18 -04:00
store.dispatch Actions::NewMessageHome.new
2017-07-25 13:36:05 -04:00
end
def on_new_message_end
2017-07-27 21:47:18 -04:00
store.dispatch Actions::NewMessageEnd.new
2017-07-25 13:36:05 -04:00
end
2017-07-25 13:41:55 -04:00
def on_new_message_backspace
2017-07-27 21:47:18 -04:00
store.dispatch Actions::NewMessageBackspace.new
2017-07-25 13:41:55 -04:00
end
def on_new_message_delete
2017-07-27 21:47:18 -04:00
store.dispatch Actions::NewMessageDelete.new
2017-07-25 13:41:55 -04:00
end
end