Created
August 8, 2013 11:23
-
-
Save devinceble/6183809 to your computer and use it in GitHub Desktop.
Hackers New For Terminal Flexible Width
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
#!/usr/bin/env ruby | |
require "httparty" | |
require "nokogiri" | |
require "rainbow" | |
require "ostruct" | |
require "shorturl" | |
def show_latest | |
response = HTTParty.get("http://news.ycombinator.com") | |
if response.body == @previous_body | |
return | |
end | |
@previous_body = response.body | |
html = Nokogiri::HTML(response.body) | |
items = [] | |
html.css("td.title a").each do |node| | |
unless node.text == "More" | |
items << OpenStruct.new(:points => node.parent.parent.next_sibling.css("span").text, :title => node.text, :url => node["href"]) | |
end | |
break if items.size == 10 | |
end | |
puts "\e[H\e[2J" | |
max_width = terminal_size()[0] | |
puts "Hacker News".ljust(max_width).background("#ff6600").foreground(:black) | |
puts " ".ljust(max_width).background("#f6f6ef").color(:blue) | |
items.each do |item| | |
title = (item.title + " ").background("#f6f6ef").color(:black) | |
points = "(#{item.points})".rjust(max_width - item.title.size - 1).background("#f6f6ef").color(:red) | |
if item.url.size > max_width | |
url = item.url.ljust(item.url.size + (max_width - (item.url.size - max_width))).background("#f6f6ef").color(:blue) | |
else | |
url = item.url.ljust(max_width).background("#f6f6ef").color(:blue) | |
end | |
puts "#{title}#{points}" | |
puts "#{url}" | |
end | |
end | |
def terminal_size | |
`stty size`.split.map { |x| x.to_i }.reverse | |
end | |
trap("SIGINT") { throw :ctrl_c } | |
catch :ctrl_c do | |
begin | |
loop { | |
show_latest | |
sleep 60 | |
} | |
rescue Exception | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment