Created
October 13, 2013 14:57
-
-
Save fukayatsu/6963242 to your computer and use it in GitHub Desktop.
[WIP]ニュースを右から左に流すやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'user_stream' | |
require 'curses' | |
require 'thread' | |
require 'pry' | |
class Text | |
def initialize(x, body) | |
@x = x | |
@body = body | |
end | |
attr_accessor :x, :body | |
end | |
class Screen | |
include Curses | |
def initialize | |
@buffer = Queue.new | |
@texts = {} | |
end | |
def add(text) | |
@buffer.push(text) if @buffer.size < 100 | |
end | |
def run | |
begin | |
init_screen | |
curs_set 0 | |
while true | |
clear | |
(0...lines).each do |row| | |
next if @texts[row] || @buffer.empty? | |
@texts[@texts.size] = Text.new(cols, @buffer.pop) | |
end | |
@texts.keys.each { |row| | |
text = @texts[row] | |
text.x -= 2 | |
if text.x + text.body.length < 0 | |
@texts.delete row | |
nil | |
else | |
if text.x > 0 | |
setpos(row, text.x) | |
else | |
setpos(row, 0) | |
end | |
str = text.body[0 ... (cols - text.x)/2] | |
str = str[- text.x..-1] if text.x < 0 | |
stdscr << str | |
[row, text] | |
end | |
} | |
refresh | |
sleep 0.1 | |
end | |
ensure | |
close_screen | |
end | |
end | |
end | |
screen = Screen.new | |
Thread.new do | |
UserStream.configure do |config| | |
config.consumer_key = '' | |
config.consumer_secret = '' | |
config.oauth_token = '' | |
config.oauth_token_secret = '' | |
end | |
client = UserStream.client | |
client.filter(track: 'ニュース') do |status| | |
# client.sample do |status| | |
screen.add "#{status.text.gsub(/[\r\n]/," ")}" if status.text | |
end | |
end | |
screen.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment