Handle Tox::Friend#send_message exceptions
This commit is contained in:
parent
b3d653c982
commit
8e064c8ac0
4 changed files with 109 additions and 58 deletions
|
@ -23,6 +23,9 @@ Metrics/LineLength:
|
|||
Metrics/MethodLength:
|
||||
Enabled: false
|
||||
|
||||
Metrics/ParameterLists:
|
||||
Enabled: false
|
||||
|
||||
Metrics/PerceivedComplexity:
|
||||
Enabled: false
|
||||
|
||||
|
|
17
lib/main.rb
17
lib/main.rb
|
@ -263,7 +263,13 @@ private
|
|||
|
||||
return if text.empty?
|
||||
|
||||
@tox_client.friend(friend_number).send_message text
|
||||
error = false
|
||||
|
||||
begin
|
||||
@tox_client.friend(friend_number).send_message text
|
||||
rescue
|
||||
error = true
|
||||
end
|
||||
|
||||
@state = state.merge(
|
||||
friends: state[:friends].merge(
|
||||
|
@ -274,10 +280,11 @@ private
|
|||
),
|
||||
|
||||
history: (state[:friends][friend_number][:history] + [
|
||||
out: true,
|
||||
time: Time.now.utc.freeze,
|
||||
name: @tox_client.name.freeze,
|
||||
text: text,
|
||||
error: error,
|
||||
out: true,
|
||||
time: Time.now.utc.freeze,
|
||||
name: @tox_client.name.freeze,
|
||||
text: text,
|
||||
]).freeze,
|
||||
).freeze,
|
||||
).freeze,
|
||||
|
|
121
lib/style.rb
121
lib/style.rb
|
@ -18,13 +18,15 @@ class Style
|
|||
Curses.init_pair cursor_id, cursor_color, cursor_bg
|
||||
Curses.init_pair menu_item_id, menu_item_color, menu_item_bg
|
||||
Curses.init_pair active_menu_item_id, active_menu_item_color, active_menu_item_bg
|
||||
Curses.init_pair message_time_id, message_time_color, message_time_bg
|
||||
Curses.init_pair message_author_id, message_author_color, message_author_bg
|
||||
Curses.init_pair peer_info_name_id, peer_info_name_color, peer_info_name_bg
|
||||
|
||||
Curses.init_pair online_mark_id, online_mark_color, online_mark_bg
|
||||
Curses.init_pair away_mark_id, away_mark_color, away_mark_bg
|
||||
Curses.init_pair busy_mark_id, busy_mark_color, busy_mark_bg
|
||||
|
||||
Curses.init_pair message_time_id, message_time_color, message_time_bg
|
||||
Curses.init_pair message_author_id, message_author_color, message_author_bg
|
||||
Curses.init_pair message_error_id, message_error_color, message_error_bg
|
||||
end
|
||||
|
||||
def logo(window)
|
||||
|
@ -76,20 +78,6 @@ class Style
|
|||
window.attroff active_menu_item_attr
|
||||
end
|
||||
|
||||
def message_time(window)
|
||||
window.attron message_time_attr
|
||||
yield
|
||||
ensure
|
||||
window.attroff message_time_attr
|
||||
end
|
||||
|
||||
def message_author(window)
|
||||
window.attron message_author_attr
|
||||
yield
|
||||
ensure
|
||||
window.attroff message_author_attr
|
||||
end
|
||||
|
||||
def peer_info_name(window)
|
||||
window.attron peer_info_name_attr
|
||||
yield
|
||||
|
@ -118,6 +106,27 @@ class Style
|
|||
window.attroff busy_mark_attr
|
||||
end
|
||||
|
||||
def message_time(window)
|
||||
window.attron message_time_attr
|
||||
yield
|
||||
ensure
|
||||
window.attroff message_time_attr
|
||||
end
|
||||
|
||||
def message_author(window)
|
||||
window.attron message_author_attr
|
||||
yield
|
||||
ensure
|
||||
window.attroff message_author_attr
|
||||
end
|
||||
|
||||
def message_error(window)
|
||||
window.attron message_error_attr
|
||||
yield
|
||||
ensure
|
||||
window.attroff message_error_attr
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def logo_attr
|
||||
|
@ -148,14 +157,6 @@ private
|
|||
Curses.color_pair active_menu_item_id
|
||||
end
|
||||
|
||||
def message_time_attr
|
||||
Curses.color_pair message_time_id
|
||||
end
|
||||
|
||||
def message_author_attr
|
||||
Curses.color_pair(message_author_id) | Curses::A_BOLD
|
||||
end
|
||||
|
||||
def peer_info_name_attr
|
||||
Curses.color_pair(peer_info_name_id) | Curses::A_BOLD
|
||||
end
|
||||
|
@ -172,6 +173,18 @@ private
|
|||
Curses.color_pair busy_mark_id
|
||||
end
|
||||
|
||||
def message_time_attr
|
||||
Curses.color_pair message_time_id
|
||||
end
|
||||
|
||||
def message_author_attr
|
||||
Curses.color_pair(message_author_id) | Curses::A_BOLD
|
||||
end
|
||||
|
||||
def message_error_attr
|
||||
Curses.color_pair message_error_id
|
||||
end
|
||||
|
||||
def logo_id
|
||||
@logo_id ||= self.class.counter
|
||||
end
|
||||
|
@ -200,14 +213,6 @@ private
|
|||
@active_menu_item_id ||= self.class.counter
|
||||
end
|
||||
|
||||
def message_time_id
|
||||
@message_time_id ||= self.class.counter
|
||||
end
|
||||
|
||||
def message_author_id
|
||||
@message_author_id ||= self.class.counter
|
||||
end
|
||||
|
||||
def peer_info_name_id
|
||||
@peer_info_name_id ||= self.class.counter
|
||||
end
|
||||
|
@ -224,6 +229,18 @@ private
|
|||
@busy_mark_id ||= self.class.counter
|
||||
end
|
||||
|
||||
def message_time_id
|
||||
@message_time_id ||= self.class.counter
|
||||
end
|
||||
|
||||
def message_author_id
|
||||
@message_author_id ||= self.class.counter
|
||||
end
|
||||
|
||||
def message_error_id
|
||||
@message_error_id ||= self.class.counter
|
||||
end
|
||||
|
||||
def logo_color
|
||||
Curses::COLOR_BLUE
|
||||
end
|
||||
|
@ -280,22 +297,6 @@ private
|
|||
Curses::COLOR_BLUE
|
||||
end
|
||||
|
||||
def message_time_color
|
||||
Curses::COLOR_CYAN
|
||||
end
|
||||
|
||||
def message_time_bg
|
||||
Curses::COLOR_BLACK
|
||||
end
|
||||
|
||||
def message_author_color
|
||||
Curses::COLOR_GREEN
|
||||
end
|
||||
|
||||
def message_author_bg
|
||||
Curses::COLOR_BLACK
|
||||
end
|
||||
|
||||
def peer_info_name_color
|
||||
Curses::COLOR_WHITE
|
||||
end
|
||||
|
@ -327,4 +328,28 @@ private
|
|||
def busy_mark_bg
|
||||
Curses::COLOR_BLACK
|
||||
end
|
||||
|
||||
def message_time_color
|
||||
Curses::COLOR_CYAN
|
||||
end
|
||||
|
||||
def message_time_bg
|
||||
Curses::COLOR_BLACK
|
||||
end
|
||||
|
||||
def message_author_color
|
||||
Curses::COLOR_GREEN
|
||||
end
|
||||
|
||||
def message_author_bg
|
||||
Curses::COLOR_BLACK
|
||||
end
|
||||
|
||||
def message_error_color
|
||||
Curses::COLOR_RED
|
||||
end
|
||||
|
||||
def message_error_bg
|
||||
Curses::COLOR_BLACK
|
||||
end
|
||||
end
|
||||
|
|
|
@ -13,13 +13,13 @@ module Widgets
|
|||
offset = 0
|
||||
|
||||
props[:messages].reverse_each do |msg|
|
||||
offset += draw_message offset, msg[:out], msg[:time].strftime('%H:%M:%S'), msg[:name], msg[:text]
|
||||
offset += draw_message offset, msg[:error], msg[:out], msg[:time].strftime('%H:%M:%S'), msg[:name], msg[:text]
|
||||
|
||||
break if offset >= props[:height]
|
||||
end
|
||||
end
|
||||
|
||||
def draw_message(offset, out, time, name, text)
|
||||
def draw_message(offset, error, out, time, name, text)
|
||||
width = props[:width] / 3 * 2
|
||||
left = out ? props[:width] - width : 0
|
||||
|
||||
|
@ -37,14 +37,22 @@ module Widgets
|
|||
addstr s
|
||||
end
|
||||
|
||||
draw_header props[:height] - offset - lines - 1, out, time, name
|
||||
draw_header props[:height] - offset - lines - 1, error, out, time, name
|
||||
|
||||
1 + lines
|
||||
end
|
||||
|
||||
def draw_header(y, out, name, time)
|
||||
def draw_header(y, error, out, name, time)
|
||||
if out
|
||||
setpos props[:width] - name.length - time.length - 1, y
|
||||
setpos props[:width] - name.length - time.length - (error ? 3 : 1), y
|
||||
|
||||
if error
|
||||
Style.default.message_error window do
|
||||
addstr 'x'
|
||||
end
|
||||
|
||||
addstr ' '
|
||||
end
|
||||
|
||||
Style.default.message_time window do
|
||||
addstr time
|
||||
|
@ -67,6 +75,14 @@ module Widgets
|
|||
Style.default.message_time window do
|
||||
addstr time
|
||||
end
|
||||
|
||||
if error
|
||||
addstr ' '
|
||||
|
||||
Style.default.message_error window do
|
||||
addstr 'x'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Reference in a new issue