Created
March 10, 2021 10:50
-
-
Save ZaWertun/351dbff48c98b9ef09a0b9b1d75255e9 to your computer and use it in GitHub Desktop.
Simple script to flip header from the wttr.in weather report to the right side
This file contains 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 'net/http' | |
COLORS_RE = /\e\[([;\d]+)?m/.freeze | |
def strip_colors(str) | |
str.gsub(COLORS_RE, '') | |
end | |
params = ARGV[0] || '' | |
uri = URI.parse("https://wttr.in/#{params}") | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
reqt = Net::HTTP::Get.new("/#{params}", {'User-Agent' => 'curl/7.71.1'}) | |
resp = http.request(reqt) | |
data = resp.body.force_encoding('utf-8') | |
lines = data.split(/\n\r?/) | |
header = lines[0..6] | |
body = lines[7..] | |
header_max = header.map{|s|strip_colors(s).size}.max | |
body_max = body.map{|s|strip_colors(s).size}.max | |
header.map! do |line| | |
size = strip_colors(line).size | |
line = line + (' ' * (header_max - size)) | |
size = strip_colors(line).size | |
(' ' * (body_max - size)) + line | |
end | |
puts header | |
puts body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment