This repository has been archived by the owner on Aug 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
display.rb
172 lines (146 loc) · 3.41 KB
/
display.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
class BasicDisplay
class DisplayThread
def initialize(inbound_queue)
@inbound_queue = inbound_queue
end
def run
@thread = Thread.new do
while true
msg = @inbound_queue.pop
if msg
puts "#{msg['client']}: #{msg['text']}"
else
break
end
end
end
end
def join
@thread.join
end
end
def initialize(outbound_queue, client_name, inbound_queue)
@outbound_queue = outbound_queue
@client_name = client_name
@inbound_queue = inbound_queue
@display_thread = setup_display(inbound_queue)
@display_thread.run
end
def setup_display(inbound_queue)
DisplayThread.new(inbound_queue)
end
def run
input_lines.each { |line| queue_text(line) }
cleanup_display
close
end
def input_lines
Enumerator.new do |y|
while line = STDIN.gets
line.strip!
if line.empty?
break
else
y << line
end
end
end
end
def cleanup_display
# No-op
end
def queue_text(msg)
packet = {:client => @client_name, :text => msg}
@outbound_queue << packet
end
def close
@outbound_queue << false
@inbound_queue << false
@display_thread.join
end
end
class CursesDisplay < BasicDisplay
require 'curses'
class CursesDisplayThread
def initialize(inbound_queue, chat_win, limit)
@inbound_queue = inbound_queue
@chat_win = chat_win
@limit = limit
end
def run
@thread = Thread.new do
messages = []
while true
msg = @inbound_queue.pop
if msg
messages << msg
min = [messages.length, @limit].min
messages = messages[-min, min]
@chat_win.clear
messages.each_with_index do |value, index|
@chat_win.setpos(index, 0)
@chat_win.addstr("#{value["client"]}: #{value["text"]}")
end
@chat_win.refresh
else
break
end
end
end
end
def join
@thread.join
end
end
def setup_display(inbound_queue)
limit = setup_screen
CursesDisplayThread.new(inbound_queue, @chat_win, limit)
end
def setup_screen
@scr = Curses.init_screen
@scr.keypad(true)
Curses.cbreak
Curses.curs_set(0)
rows = @scr.maxy
cols = @scr.maxx
#create our windows
@div_win = Curses::Window.new(1, cols + 1, rows - 3, 0)
@chat_win = Curses::Window.new(rows - 3, cols, 0, 0)
@msg_win = Curses::Window.new(1, cols, rows - 2, 0)
@instructions_win = Curses::Window.new(1, cols, rows - 1, 0)
# setup the divider
div = "=" * cols
@div_win.addstr(div)
@div_win.refresh
@instructions_win.addstr("Type a message, Hit <ENTER> to send. An empty message quits the program")
@instructions_win.refresh
rows - 4
end
def input_lines
Enumerator.new do |y|
current_msg = ''
while true
c = @msg_win.getch
if c == 10
if current_msg.empty?
break
else
@msg_win.clear
@msg_win.refresh
line = current_msg
current_msg = ''
y << line
end
else
current_msg += c.chr
end
end
end
end
def cleanup_display
@scr.keypad(false)
Curses.nocbreak
Curses.curs_set(1)
Curses.close_screen
end
end