forked from bf4/toot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoot.rb
204 lines (177 loc) · 4.38 KB
/
toot.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
require "cgi"
require "tempfile"
require "thread"
require "io/console"
require "values"
require "twitter"
require "leveldb"
require_relative "screen"
require_relative "cursor"
require_relative "tweet"
require_relative "database"
require_relative "timeline"
require_relative "twitter_lib"
require_relative "selectable_queue"
require_relative "tweet_renderer"
class World < Value.new(:timeline)
end
class Toot
def initialize(screen, using_network: true)
@screen = screen
@using_network = using_network
end
def run
TwitterLib.authenticate
database = Database.default
timeline = Timeline.new(database.timeline)
timeline_queue = SelectableQueue.new
start_timeline_stream(timeline, timeline_queue) if @using_network
world = World.new(timeline)
view = TimelineView.new(@screen)
EventLoop.new(database, world, timeline_queue, view).run
end
# hits the network
def start_timeline_stream(timeline, timeline_queue)
timeline_stream = TimelineStream.new(timeline, timeline_queue)
Thread.new { timeline_stream.run }
end
class EventLoop
def initialize(database, world, timeline_queue, view)
@database = database
@world = world
@timeline_queue = timeline_queue
@view = view
end
def run
loop do
@view.display(@world)
read, _, _ = select([$stdin, @timeline_queue], nil, nil)
handle_stdin if read.include?($stdin)
handle_timeline if read.include?(@timeline_queue)
end
end
def handle_stdin
@view.key($stdin.getc)
end
def handle_timeline
timeline = @timeline_queue.pop
@database.write_timeline(timeline.tweets)
@world = World.new(timeline)
end
end
end
class TimelineStream
def initialize(timeline, queue)
@timeline = timeline
@queue = queue
end
def run
loop do
@timeline = @timeline.add(TwitterLib.timeline_tweets)
@queue << @timeline
sleep(10)
end
end
end
module View
def height
@screen.usable_height
end
end
class TimelineView
include View
def initialize(screen)
@screen = screen
@cursor = Cursor.new([])
end
def display(world)
tweets = world.timeline.tweets
@cursor = @cursor.with_tweets(tweets)
draw_tweets
@screen.status("#{tweets.count} toots", "q=Quit")
end
def draw_tweets
# row = 0
# leftover from when replacing tweet view with tweet renderer
# def draw_tweets
# row = 0
# @cursor.starting_at_index(@cursor.selection_index).each do |tweet|
# TweetView.new(@screen, @cursor, tweet, row).display
# row += tweet.line_count
# break if row > height
# end
#
# blank_lines_from_row(row)
# end
#
# def blank_lines_from_row(start_row)
# if start_row < height
# (start_row..height).each do |row|
# @screen.write(row, 0, "")
# end
# end
# end
#
tweets = @cursor.starting_at_index(@cursor.selection_index)
lines = TweetRenderer.new(tweets, @cursor.selection, height).render
@screen.write_lines(0, lines)
end
def key(key)
case key
when ?c then ComposeView.new(@screen).compose
when ?j then @cursor = @cursor.down
when ?k then @cursor = @cursor.up
when ?d then @cursor = @cursor.down(jump_size)
when ?u then @cursor = @cursor.up(jump_size)
when ?q then raise SystemExit
when Screen::KEY_CTRL_C then raise SystemExit
end
end
def jump_size
height / 2
end
end
class ComposeView
def initialize(screen)
@screen = screen
end
def compose
file = Tempfile.open("compose-tweet")
begin
file.close
@screen.suspend do
system("vi #{file.path}")
end
file.open
text = file.read
unless text.empty?
TwitterLib.update(text)
end
end
ensure
file.close
file.unlink
end
end
if $0 == __FILE__
Thread.abort_on_exception = true
config = Class.new do
def initialize(args)
@args = args
end
def using_network
!@args.include?("--without-network")
end
end.new(ARGV.to_a)
# world = World.blank(options)
Screen.with_screen do |screen|
# start_line = screen.height - 10
# while not world.done?
# render(world, screen, start_line)
# world = handle_key(world)
# end
Toot.new(screen, using_network: config.using_network).run
# screen.move_cursor(screen.height - 1, 0)
end
# puts world.selection
end