raindown 0.19.0 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/raindown/area.rb +88 -0
- data/lib/raindown/chart/drawing.rb +0 -2
- data/lib/raindown/chart/line.rb +1 -1
- data/lib/raindown/cli.rb +2 -2
- data/lib/raindown/config.rb +11 -0
- data/lib/raindown/document.rb +7 -3
- data/lib/raindown/editing.rb +4 -2
- data/lib/raindown/endpoint/curl.rb +0 -1
- data/lib/raindown/grid.rb +154 -0
- data/lib/raindown/grid_directives.rb +43 -0
- data/lib/raindown/grid_wrapping.rb +46 -0
- data/lib/raindown/http_json.rb +45 -0
- data/lib/raindown/image.rb +44 -0
- data/lib/raindown/js/js_nodes.rb +83 -36
- data/lib/raindown/js/quickjs_host.rb +63 -0
- data/lib/raindown/js/quickjs_runtime.js +61 -0
- data/lib/raindown/js/services.js +31 -0
- data/lib/raindown/markdown_nodes.rb +2 -0
- data/lib/raindown/node_blocks.rb +22 -9
- data/lib/raindown/node_injector.rb +56 -17
- data/lib/raindown/nodes.rb +50 -15
- data/lib/raindown/outdent.rb +100 -0
- data/lib/raindown/problem.rb +12 -3
- data/lib/raindown/project/card.rb +54 -0
- data/lib/raindown/project/endpoint.rb +14 -0
- data/lib/raindown/project/project.rb +168 -0
- data/lib/raindown/project_nodes.rb +47 -0
- data/lib/raindown/rasters.rb +98 -0
- data/lib/raindown/scene.rb +10 -0
- data/lib/raindown/services/connections.rb +65 -0
- data/lib/raindown/services/local.rb +139 -0
- data/lib/raindown/services.rb +72 -0
- data/lib/raindown/table/table.rb +2 -2
- data/lib/raindown/table/table_column.rb +0 -2
- data/lib/raindown/table/table_file.rb +0 -1
- data/lib/raindown/table/written_table.rb +0 -2
- data/lib/raindown/tui/endpoints.rb +1 -1
- data/lib/raindown/tui/navigator/navigator.rb +1 -1
- data/lib/raindown/watcher/project.rb +2 -2
- data/lib/raindown/weather/branch_tree.rb +129 -0
- data/lib/raindown/weather/forecast.rb +193 -0
- data/lib/raindown/weather/forecast_cache.rb +58 -0
- data/lib/raindown/weather/forecast_cards.rb +115 -0
- data/lib/raindown/weather/sky.rb +82 -0
- data/lib/raindown/weather/weather.rb +140 -0
- data/lib/raindown.rb +10 -1
- data/lib/version.rb +1 -1
- metadata +27 -6
- data/lib/raindown/js/host.mjs +0 -51
- data/lib/raindown/tui/card.rb +0 -56
- data/lib/raindown/tui/endpoint.rb +0 -16
- data/lib/raindown/tui/project.rb +0 -147
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'net/http'
|
|
4
|
+
require 'openssl'
|
|
5
|
+
|
|
6
|
+
module Raindown
|
|
7
|
+
class Services
|
|
8
|
+
# Connections to the web, kept open after a request for the next to the same host, so a component asking a host for
|
|
9
|
+
# many things, like a map's tiles, doesn't open a connection, with its TLS handshake, for each. Each is used by one
|
|
10
|
+
# request at a time, and Net::HTTP opens it again when it's been idle too long, or retries a request once when the
|
|
11
|
+
# host had closed it.
|
|
12
|
+
module Connections
|
|
13
|
+
# How long a connection's kept idle, in seconds, and how many are kept for each host.
|
|
14
|
+
KEEP_FOR = 30
|
|
15
|
+
MOST_KEPT = 8
|
|
16
|
+
|
|
17
|
+
@idle = Hash.new { |hash, key| hash[key] = [] }
|
|
18
|
+
@lock = Mutex.new
|
|
19
|
+
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
# The response to a request, sent on a kept connection to its URI's host, or a new one.
|
|
23
|
+
def request(uri, http_request, timeout:)
|
|
24
|
+
key = [uri.scheme, uri.host, uri.port]
|
|
25
|
+
connection = take(key) || opened(uri, timeout)
|
|
26
|
+
response = connection.request(http_request)
|
|
27
|
+
keep(key, connection)
|
|
28
|
+
response
|
|
29
|
+
rescue StandardError
|
|
30
|
+
close(connection)
|
|
31
|
+
raise
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def opened(uri, timeout)
|
|
35
|
+
connection = Net::HTTP.new(uri.host, uri.port)
|
|
36
|
+
connection.use_ssl = uri.scheme == 'https'
|
|
37
|
+
connection.open_timeout = timeout
|
|
38
|
+
connection.read_timeout = timeout
|
|
39
|
+
connection.keep_alive_timeout = KEEP_FOR
|
|
40
|
+
connection.start
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def take(key)
|
|
44
|
+
@lock.synchronize { @idle[key].pop }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def keep(key, connection)
|
|
48
|
+
kept = @lock.synchronize { @idle[key].size < MOST_KEPT && @idle[key].push(connection) }
|
|
49
|
+
close(connection) unless kept
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def close(connection)
|
|
53
|
+
connection.finish if connection&.started?
|
|
54
|
+
rescue IOError
|
|
55
|
+
nil
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Forgets the kept connections, closing them.
|
|
59
|
+
def clear
|
|
60
|
+
idle = @lock.synchronize { @idle.values.flatten.tap { @idle.clear } }
|
|
61
|
+
idle.each { close(it) }
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'digest'
|
|
4
|
+
require 'fileutils'
|
|
5
|
+
require 'json'
|
|
6
|
+
require 'net/http'
|
|
7
|
+
require 'open3'
|
|
8
|
+
require 'openssl'
|
|
9
|
+
require_relative 'connections'
|
|
10
|
+
|
|
11
|
+
module Raindown
|
|
12
|
+
class Services
|
|
13
|
+
# The services on the desktop and in the terminal, in Raindown's own process: the web with Net::HTTP, keeping its
|
|
14
|
+
# connections open for the next request to a host, the project's files, a cache in ~/.cache/raindown and git,
|
|
15
|
+
# which it runs.
|
|
16
|
+
class Local
|
|
17
|
+
TIMEOUT = 10
|
|
18
|
+
METHODS = %w[Get Head Post Put Patch Delete].freeze
|
|
19
|
+
REDIRECTS = 3
|
|
20
|
+
|
|
21
|
+
# A call's answer, from the project's services, by its root.
|
|
22
|
+
def self.call(root, name, args)
|
|
23
|
+
@at ||= {}
|
|
24
|
+
(@at[root] ||= new(root:)).public_send(name, *args)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
attr_reader :root
|
|
28
|
+
|
|
29
|
+
def initialize(root:)
|
|
30
|
+
@root = File.expand_path(root)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# The response to a request, as { status, headers, body }, following redirects, its body as text.
|
|
34
|
+
def fetch(url, options = {})
|
|
35
|
+
options = options.transform_keys(&:to_s)
|
|
36
|
+
response = request(URI(url), options)
|
|
37
|
+
return redirected(url, response, options) if redirect?(response, options)
|
|
38
|
+
|
|
39
|
+
headers = response.to_hash.transform_values { it.join(', ') }
|
|
40
|
+
{ 'status' => response.code.to_i, 'headers' => headers, 'body' => body_of(response, options) }
|
|
41
|
+
rescue SocketError, SystemCallError, IOError, Timeout::Error, OpenSSL::SSL::SSLError => e
|
|
42
|
+
raise Error, "Couldn't reach #{url}: #{e.message}"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# A file's text, or its bytes as Base64 with encoding: 'base64', for a file that isn't text, like an image.
|
|
46
|
+
def read(path, options = {})
|
|
47
|
+
return [File.binread(inside(path))].pack('m0') if options.transform_keys(&:to_s)['encoding'] == 'base64'
|
|
48
|
+
|
|
49
|
+
File.read(inside(path))
|
|
50
|
+
rescue Errno::ENOENT
|
|
51
|
+
raise Error, "There's no #{path}"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# The project's files matching a pattern, like 'notes/*.md', from its root.
|
|
55
|
+
def list(pattern = '**/*')
|
|
56
|
+
Dir.glob(pattern, base: root).select { File.file?(File.join(root, it)) }.sort
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def write(path, text)
|
|
60
|
+
path = inside(path)
|
|
61
|
+
FileUtils.mkdir_p(File.dirname(path))
|
|
62
|
+
File.write(path, text)
|
|
63
|
+
nil
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# What was kept under a key for the project, or nil when nothing was, or it's older than the seconds it was kept for.
|
|
67
|
+
def cache_get(key)
|
|
68
|
+
kept = JSON.parse(File.read(cache_file(key)))
|
|
69
|
+
return if kept['until'] && Time.now.to_f > kept['until']
|
|
70
|
+
|
|
71
|
+
kept['value']
|
|
72
|
+
rescue Errno::ENOENT, JSON::ParserError
|
|
73
|
+
nil
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Keeps a value JSON can hold under a key, for so many seconds, or until it's kept again.
|
|
77
|
+
def cache_set(key, value, seconds = nil)
|
|
78
|
+
FileUtils.mkdir_p(File.dirname(cache_file(key)))
|
|
79
|
+
File.write(cache_file(key), JSON.generate('until' => seconds && (Time.now.to_f + seconds), 'value' => value))
|
|
80
|
+
nil
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# What git writes, run in the project with the arguments, like ('branch', '--list').
|
|
84
|
+
def git(*args)
|
|
85
|
+
output, errors, status = Open3.capture3('git', *args.map(&:to_s), chdir: root)
|
|
86
|
+
raise Error, git_error(args, errors) unless status.success?
|
|
87
|
+
|
|
88
|
+
output
|
|
89
|
+
rescue Errno::ENOENT
|
|
90
|
+
raise Error, 'git is not installed'
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
private
|
|
94
|
+
|
|
95
|
+
# Its body as UTF-8 text, or as Base64 with encoding: 'base64', for a file that isn't text, like an image.
|
|
96
|
+
def body_of(response, options)
|
|
97
|
+
return [response.body.to_s].pack('m0') if options['encoding'] == 'base64'
|
|
98
|
+
|
|
99
|
+
response.body.to_s.dup.force_encoding(Encoding::UTF_8).scrub
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def git_error(args, errors)
|
|
103
|
+
return "git #{args.first} failed" if errors.strip.empty?
|
|
104
|
+
|
|
105
|
+
errors.strip
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def request(uri, options)
|
|
109
|
+
method = (options['method'] || 'GET').to_s.capitalize
|
|
110
|
+
raise Error, "Can't #{method.upcase} with fetch" unless METHODS.include?(method)
|
|
111
|
+
|
|
112
|
+
http_request = Net::HTTP.const_get(method).new(uri, options['headers'] || {})
|
|
113
|
+
http_request.body = options['body'] if options['body']
|
|
114
|
+
Connections.request(uri, http_request, timeout: TIMEOUT)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def redirected(url, response, options)
|
|
118
|
+
fetch(URI.join(url, response['location']).to_s, options.merge('redirects' => redirects_left(options) - 1))
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def redirect?(response, options) = response.is_a?(Net::HTTPRedirection) && redirects_left(options).positive?
|
|
122
|
+
|
|
123
|
+
def redirects_left(options) = options.fetch('redirects', REDIRECTS)
|
|
124
|
+
|
|
125
|
+
# The path in the project, which it must be.
|
|
126
|
+
def inside(path)
|
|
127
|
+
full = File.expand_path(path.to_s, root)
|
|
128
|
+
raise Error, "#{path} is outside the project" unless full.start_with?("#{root}/")
|
|
129
|
+
|
|
130
|
+
full
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def cache_file(key)
|
|
134
|
+
name = Digest::SHA256.hexdigest("#{root}\n#{key}")[0, 24]
|
|
135
|
+
File.join(ENV.fetch('XDG_CACHE_HOME', File.join(Dir.home, '.cache')), 'raindown', 'nodes', "#{name}.json")
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raindown
|
|
4
|
+
# What a node can ask Raindown for, the same from Ruby and JavaScript, wherever it runs: the web, its project's files,
|
|
5
|
+
# a cache and git. A Ruby node gets them as raindown:, like metadata:, and a JavaScript one as raindown in its context,
|
|
6
|
+
# each asking through its host, which ends here, like an app's page asking its server. Each call is a name and
|
|
7
|
+
# arguments that JSON can hold, answered by Services.host: Local on the desktop and in the terminal, or an app's own,
|
|
8
|
+
# like the Obsidian plugin's worker, which can't run processes or open sockets. A platform without a service, like
|
|
9
|
+
# git on a phone, raises an Error, which a node can catch. Paths are its project's, from its root, and can't leave it.
|
|
10
|
+
class Services
|
|
11
|
+
# What can be asked for, by the names the hosts send.
|
|
12
|
+
NAMES = %w[fetch read list write cache_get cache_set git].freeze
|
|
13
|
+
|
|
14
|
+
class Error < StandardError; end
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
# What answers each call, with call(root, name, args), which an app can give its own.
|
|
18
|
+
attr_writer :host
|
|
19
|
+
|
|
20
|
+
def host
|
|
21
|
+
@host ||= begin
|
|
22
|
+
require_relative 'services/local'
|
|
23
|
+
Local
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# The services for a project, by its root, made once.
|
|
28
|
+
def at(root)
|
|
29
|
+
@at ||= {}
|
|
30
|
+
@at[File.expand_path(root)] ||= new(root:)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# The services for the page being written, from its metadata's root, or else the current folder.
|
|
34
|
+
def for(metadata)
|
|
35
|
+
root = metadata.root if metadata.respond_to?(:root)
|
|
36
|
+
at(root || Dir.pwd)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
attr_reader :root
|
|
41
|
+
|
|
42
|
+
def initialize(root:)
|
|
43
|
+
@root = File.expand_path(root)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# A call by its name and arguments, as a host sends it.
|
|
47
|
+
def call(name, args = [])
|
|
48
|
+
raise Error, "There's no #{name} service" unless NAMES.include?(name.to_s)
|
|
49
|
+
|
|
50
|
+
self.class.host.call(root, name.to_s, args)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# The response to a request, as { status, headers, body }, following redirects, its body as text.
|
|
54
|
+
def fetch(url, options = {}) = call('fetch', [url, options])
|
|
55
|
+
|
|
56
|
+
def read(path, options = {}) = call('read', [path, options])
|
|
57
|
+
|
|
58
|
+
# The project's files matching a pattern, like 'notes/*.md', from its root.
|
|
59
|
+
def list(pattern = '**/*') = call('list', [pattern])
|
|
60
|
+
|
|
61
|
+
def write(path, text) = call('write', [path, text])
|
|
62
|
+
|
|
63
|
+
# What was kept under a key for the project, or nil when nothing was, or it's older than the seconds it was kept for.
|
|
64
|
+
def cache_get(key) = call('cache_get', [key])
|
|
65
|
+
|
|
66
|
+
# Keeps a value JSON can hold under a key, for so many seconds, or until it's kept again.
|
|
67
|
+
def cache_set(key, value, seconds = nil) = call('cache_set', [key, value, seconds])
|
|
68
|
+
|
|
69
|
+
# What git writes, run in the project with the arguments, like ('branch', '--list').
|
|
70
|
+
def git(*args) = call('git', args)
|
|
71
|
+
end
|
|
72
|
+
end
|
data/lib/raindown/table/table.rb
CHANGED
|
@@ -72,9 +72,9 @@ module Raindown
|
|
|
72
72
|
TableFile.supported?(path) && Utils.inside?(path, data_root(metadata)) && !File.exist?(path)
|
|
73
73
|
end
|
|
74
74
|
|
|
75
|
-
# From the table as it's written
|
|
75
|
+
# From the table as it's written.
|
|
76
76
|
def create_file(path, lines)
|
|
77
|
-
rows = MarkdownTable.parse(lines
|
|
77
|
+
rows = MarkdownTable.parse(lines)
|
|
78
78
|
rows = STARTER if rows.empty?
|
|
79
79
|
TableFile.create(path, rows)
|
|
80
80
|
end
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'unicode/display_width'
|
|
4
|
-
|
|
5
3
|
module Raindown
|
|
6
4
|
# A table's column: its alignment, from its delimiter cell's colons, and its width, that of its widest cell, or as wide
|
|
7
5
|
# as its delimiter when that's equals signs, like |====|, which is fixed. An equals sign typed into dashes, like
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
# Loaded when first used, as it loads strscan, a native extension, which slows a packed executable's start.
|
|
4
4
|
autoload :CSV, 'csv'
|
|
5
|
-
require 'fileutils'
|
|
6
5
|
|
|
7
6
|
module Raindown
|
|
8
7
|
# The CSV or TSV file behind a table:, as rows of cells as wide as each other, written back in its own format.
|
|
@@ -17,9 +17,9 @@ module Raindown
|
|
|
17
17
|
@execute = execute
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
-
# Loads the project's nodes from its .raindown folder.
|
|
20
|
+
# Loads the project's nodes from its .raindown folder, saying why any couldn't be.
|
|
21
21
|
def start
|
|
22
|
-
Nodes.load(File.join(root, Nodes::FOLDER))
|
|
22
|
+
Nodes.load(File.join(root, Nodes::FOLDER)).each { warn "[#{name}] raindown: #{it}" }
|
|
23
23
|
rescue StandardError, ScriptError => e
|
|
24
24
|
raise if ENV['RAINDOWN_DEBUG']
|
|
25
25
|
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'open3'
|
|
4
|
+
|
|
5
|
+
module Raindown
|
|
6
|
+
# The branches of a git repository that aren't merged into its main branch, a line each, with how many commits it has
|
|
7
|
+
# that its parent doesn't, those made earlier first, each branch off main followed by the branches off it, indented
|
|
8
|
+
# under it after an arrow:
|
|
9
|
+
# login · 4 commits
|
|
10
|
+
# -> login-tests · 2 commits
|
|
11
|
+
# search · 6 commits
|
|
12
|
+
# -> search-filters · 3 commits
|
|
13
|
+
# -> search-sort · 1 commit
|
|
14
|
+
# A branch is off the branch it shares the most commits with, or main, and it was made when its first commit that
|
|
15
|
+
# its parent doesn't have was, or its last commit when it has none. Git doesn't keep when a branch was made.
|
|
16
|
+
class BranchTree
|
|
17
|
+
MAINS = %w[main master].freeze
|
|
18
|
+
ARROW = '-> '
|
|
19
|
+
# How much further right a branch off another starts than it.
|
|
20
|
+
INDENT = 4
|
|
21
|
+
|
|
22
|
+
Commit = Data.define(:time, :parents)
|
|
23
|
+
|
|
24
|
+
# A branch as it's written: its name, how deep it is off main, and its own commits.
|
|
25
|
+
Branch = Data.define(:name, :depth, :commits) do
|
|
26
|
+
def arrow = depth.zero? ? '' : ARROW
|
|
27
|
+
def indent = INDENT * depth
|
|
28
|
+
def count = "#{commits} #{commits == 1 ? 'commit' : 'commits'}"
|
|
29
|
+
def to_s = "#{' ' * indent}#{arrow}#{name} · #{count}"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def initialize(root)
|
|
33
|
+
@root = root
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def lines
|
|
37
|
+
branches.map(&:to_s)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Each branch in the order it's written: a branch off main, then the branches off it.
|
|
41
|
+
def branches
|
|
42
|
+
return [] unless main
|
|
43
|
+
|
|
44
|
+
@tips = unmerged_tips
|
|
45
|
+
return [] if @tips.empty?
|
|
46
|
+
|
|
47
|
+
@commits = commits_off_main
|
|
48
|
+
@reach = @tips.to_h { |name, tip| [name, reachable(tip)] }
|
|
49
|
+
@parents = @tips.keys.to_h { [it, parent_of(it)] }
|
|
50
|
+
children_of(nil).flat_map { branches_from(it, 0) }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def main
|
|
56
|
+
@main ||= MAINS.find { git('rev-parse', '--verify', '--quiet', "refs/heads/#{it}") }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Each unmerged branch's name and the commit it's at.
|
|
60
|
+
def unmerged_tips
|
|
61
|
+
output = git('branch', '--no-merged', main, '--format=%(refname:short) %(objectname)').to_s
|
|
62
|
+
output.lines.to_h(&:split)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# The commits on the branches that main doesn't have, by their hashes.
|
|
66
|
+
def commits_off_main
|
|
67
|
+
output = git('log', '--format=%H %ct %P', *@tips.values, "^#{main}").to_s
|
|
68
|
+
output.lines.to_h do |line|
|
|
69
|
+
hash, time, *parents = line.split
|
|
70
|
+
[hash, Commit.new(time: time.to_i, parents:)]
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# The commits from a tip that main doesn't have.
|
|
75
|
+
def reachable(tip)
|
|
76
|
+
found = Set.new
|
|
77
|
+
queue = [tip]
|
|
78
|
+
while (hash = queue.shift)
|
|
79
|
+
next if found.include?(hash) || !@commits.key?(hash)
|
|
80
|
+
|
|
81
|
+
found << hash
|
|
82
|
+
queue.concat(@commits[hash].parents)
|
|
83
|
+
end
|
|
84
|
+
found
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# The branch it shares the most commits with that isn't off it, or main. Of two at the same commit, the one made
|
|
88
|
+
# first is the parent.
|
|
89
|
+
def parent_of(name)
|
|
90
|
+
candidates = @tips.keys.reject { it == name || off?(it, name) }
|
|
91
|
+
shared = candidates.to_h { [it, (@reach[name] & @reach[it]).size] }.select { |_, count| count.positive? }
|
|
92
|
+
shared.max_by { |other, count| [count, -created_at(other, by: nil)] }&.first
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Whether one branch is off another: its tip has the other's, but not the other way round, or they're at the same
|
|
96
|
+
# commit and it's later by name.
|
|
97
|
+
def off?(name, other)
|
|
98
|
+
has = @reach[name].include?(@tips[other])
|
|
99
|
+
return has unless has && @reach[other].include?(@tips[name])
|
|
100
|
+
|
|
101
|
+
name > other
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# When its first commit that its parent doesn't have was made, or its last commit.
|
|
105
|
+
def created_at(name, by: @parents[name])
|
|
106
|
+
own = @reach[name] - (by ? @reach[by] : Set.new)
|
|
107
|
+
times = own.map { @commits[it].time }
|
|
108
|
+
times.min || @commits[@tips[name]]&.time || 0
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def children_of(parent)
|
|
112
|
+
@tips.keys.select { @parents[it] == parent }.sort_by { [created_at(it), it] }
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Each branch off main starts a line further right than the one made before it, and its first branch carries on
|
|
116
|
+
# its line, while its other branches start lines below, their arrows under its first's.
|
|
117
|
+
# The branch and the branches off it, a level deeper, with its own commits, those its parent doesn't have.
|
|
118
|
+
def branches_from(name, depth)
|
|
119
|
+
parent = @parents[name]
|
|
120
|
+
commits = (@reach[name] - (parent ? @reach[parent] : Set.new)).size
|
|
121
|
+
[Branch.new(name:, depth:, commits:), *children_of(name).flat_map { branches_from(it, depth + 1) }]
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def git(*args)
|
|
125
|
+
output, status = Open3.capture2('git', '-C', @root, *args, err: File::NULL)
|
|
126
|
+
output if status.success?
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'date'
|
|
4
|
+
require 'json'
|
|
5
|
+
require_relative '../http_json'
|
|
6
|
+
require_relative 'forecast_cache'
|
|
7
|
+
require_relative 'forecast_cards'
|
|
8
|
+
require_relative 'sky'
|
|
9
|
+
|
|
10
|
+
module Raindown
|
|
11
|
+
# The weather where you are, or in a place you name, from Open-Meteo, which needs no key: the weather now and the next
|
|
12
|
+
# few days', for weather:. Where you are is your time zone's city, like Melbourne for Australia/Melbourne, from where
|
|
13
|
+
# tzdata's zone.tab puts it, and a place you name is found by Open-Meteo's geocoding. It's kept for a while, and kept
|
|
14
|
+
# longer when Open-Meteo can't be reached, so it isn't lost while you're offline.
|
|
15
|
+
class Forecast
|
|
16
|
+
GEOCODING = 'https://geocoding-api.open-meteo.com/v1/search'
|
|
17
|
+
FORECAST = 'https://api.open-meteo.com/v1/forecast'
|
|
18
|
+
ZONE_TAB = '/usr/share/zoneinfo/zone.tab'
|
|
19
|
+
FRESH_FOR = 15 * 60
|
|
20
|
+
# How long after Open-Meteo couldn't be reached it's asked again, using what was kept meanwhile.
|
|
21
|
+
RETRY_AFTER = 60
|
|
22
|
+
DAYS = 7
|
|
23
|
+
# What it asks Open-Meteo for now, for its sky as well as its temperature and weather.
|
|
24
|
+
CURRENT = 'temperature_2m,weather_code,cloud_cover,cloud_cover_low,cloud_cover_mid,cloud_cover_high,rain,showers,' \
|
|
25
|
+
'snowfall,wind_speed_10m,visibility,uv_index_clear_sky'
|
|
26
|
+
# The countries that measure in Fahrenheit.
|
|
27
|
+
FAHRENHEIT = %w[US LR MM].freeze
|
|
28
|
+
# What the WMO's weather codes, which Open-Meteo answers with, are in words.
|
|
29
|
+
WORDS = {
|
|
30
|
+
0 => 'clear', 1 => 'mostly clear', 2 => 'partly cloudy', 3 => 'overcast', 45 => 'fog', 48 => 'freezing fog',
|
|
31
|
+
51 => 'light drizzle', 53 => 'drizzle', 55 => 'heavy drizzle', 56 => 'freezing drizzle', 57 => 'freezing drizzle',
|
|
32
|
+
61 => 'light rain', 63 => 'rain', 65 => 'heavy rain', 66 => 'freezing rain', 67 => 'freezing rain',
|
|
33
|
+
71 => 'light snow', 73 => 'snow', 75 => 'heavy snow', 77 => 'snow grains', 80 => 'light showers', 81 => 'showers',
|
|
34
|
+
82 => 'heavy showers', 85 => 'snow showers', 86 => 'heavy snow showers', 95 => 'thunderstorms',
|
|
35
|
+
96 => 'thunderstorms with hail', 99 => 'thunderstorms with hail'
|
|
36
|
+
}.freeze
|
|
37
|
+
|
|
38
|
+
Error = HTTPJSON::Error
|
|
39
|
+
Place = Data.define(:name, :latitude, :longitude, :country)
|
|
40
|
+
# A day's weather, its high and low, its weather code, and how strong its sun's ultraviolet gets, its UV index.
|
|
41
|
+
Day = Data.define(:date, :high, :low, :code, :uv) do
|
|
42
|
+
def words = WORDS.fetch(code, 'unknown')
|
|
43
|
+
end
|
|
44
|
+
# The weather now and the week's, in a unit, like °C, with the conditions now, as Open-Meteo gives them, for its sky,
|
|
45
|
+
# and how far ahead of UTC the place's clocks are, in seconds.
|
|
46
|
+
Report = Data.define(:place, :temperature, :code, :unit, :days, :conditions, :utc_offset) do
|
|
47
|
+
def words = WORDS.fetch(code, 'unknown')
|
|
48
|
+
|
|
49
|
+
# How strong the sun's ultraviolet is now under a clear sky, its UV index, as the Bureau of Meteorology and NOAA give
|
|
50
|
+
# it, the most it could be, as sun protection goes by, or nil when it wasn't given, like in a forecast kept from
|
|
51
|
+
# before it was asked for. Its conditions' names are text as Open-Meteo gives them, and symbols as they're kept.
|
|
52
|
+
def uv = conditions.transform_keys(&:to_s)['uv_index_clear_sky']
|
|
53
|
+
|
|
54
|
+
# How strong the sun's ultraviolet gets today, at its strongest, under a clear sky, or nil.
|
|
55
|
+
def uv_peak = days.first&.uv
|
|
56
|
+
|
|
57
|
+
# Its lines, the days' cards, as ForecastCards draws them.
|
|
58
|
+
def lines = ForecastCards.lines(days)
|
|
59
|
+
|
|
60
|
+
# The characters of its lines for an app to dim, the cards' borders, as [line, from, to].
|
|
61
|
+
def quiet = ForecastCards.borders(lines)
|
|
62
|
+
|
|
63
|
+
# The characters of its lines for an app to embolden, the cards' days, as [line, from, to].
|
|
64
|
+
def bold = ForecastCards.headings(days)
|
|
65
|
+
|
|
66
|
+
# The characters of its lines for an app to draw as large as a line is tall, the cards' emoji, as [line, from, to].
|
|
67
|
+
def large = ForecastCards.emoji(days)
|
|
68
|
+
|
|
69
|
+
# The weather now as a sky for an app to paint, its temperature in °C.
|
|
70
|
+
def sky
|
|
71
|
+
celsius = temperature
|
|
72
|
+
celsius = (temperature - 32) * 5 / 9 if unit == '°F'
|
|
73
|
+
Sky.of(conditions.transform_keys(&:to_sym).merge(temperature_2m: celsius))
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# The forecast for a place you name, or else where you are, or nil when there's nowhere to forecast for.
|
|
78
|
+
def self.for(place_name = nil, zone: TimeZone.name)
|
|
79
|
+
new(place_name, zone:).report
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# The forecast last kept for a place you name, or else where you are, however old, or nil, without asking for it.
|
|
83
|
+
def self.cached(place_name = nil, zone: TimeZone.name)
|
|
84
|
+
new(place_name, zone:).cached
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def initialize(place_name, zone:)
|
|
88
|
+
@place_name = place_name
|
|
89
|
+
@zone = zone
|
|
90
|
+
@cache = Cache.new(place_name || zone.to_s)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# The forecast, from the cache while it's fresh, or else asked for again, for the place it was kept for, so it's not
|
|
94
|
+
# looked up again, or from the cache however old it is when Open-Meteo can't be reached, which isn't asked again for
|
|
95
|
+
# RETRY_AFTER seconds, or nil when it never has been.
|
|
96
|
+
def report
|
|
97
|
+
cached, fresh = @cache.report
|
|
98
|
+
return cached if fresh
|
|
99
|
+
|
|
100
|
+
place = cached&.place || self.place
|
|
101
|
+
return unless place
|
|
102
|
+
|
|
103
|
+
fetched(place).tap { @cache.keep(it) }
|
|
104
|
+
rescue Error
|
|
105
|
+
raise unless cached
|
|
106
|
+
|
|
107
|
+
@cache.unreached
|
|
108
|
+
cached
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# The forecast kept for this place, however old, or nil, without asking Open-Meteo.
|
|
112
|
+
def cached = @cache.report.first
|
|
113
|
+
|
|
114
|
+
# The place to forecast for, as its name, latitude, longitude and country.
|
|
115
|
+
def place
|
|
116
|
+
return TimeZone.place(@zone) unless @place_name
|
|
117
|
+
|
|
118
|
+
found = HTTPJSON.get("#{GEOCODING}?#{URI.encode_www_form(name: @place_name, count: 1, language: 'en')}")['results']&.first
|
|
119
|
+
raise Error, "Couldn't find #{@place_name}" unless found
|
|
120
|
+
|
|
121
|
+
Place.new(name: found['name'], latitude: found['latitude'], longitude: found['longitude'], country: found['country_code'])
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
private
|
|
125
|
+
|
|
126
|
+
def fetched(place)
|
|
127
|
+
query = { latitude: place.latitude, longitude: place.longitude, current: CURRENT,
|
|
128
|
+
daily: 'weather_code,temperature_2m_max,temperature_2m_min,uv_index_clear_sky_max', timezone: 'auto',
|
|
129
|
+
forecast_days: DAYS }
|
|
130
|
+
query[:temperature_unit] = 'fahrenheit' if fahrenheit?(place)
|
|
131
|
+
report_of(place, HTTPJSON.get("#{FORECAST}?#{URI.encode_www_form(query)}"), unit_of(place))
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def fahrenheit?(place) = FAHRENHEIT.include?(place.country)
|
|
135
|
+
|
|
136
|
+
def unit_of(place)
|
|
137
|
+
return '°F' if fahrenheit?(place)
|
|
138
|
+
|
|
139
|
+
'°C'
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def report_of(place, answer, unit)
|
|
143
|
+
now = answer.fetch('current')
|
|
144
|
+
Report.new(place:, temperature: now['temperature_2m'], code: now['weather_code'], unit:, days: days_of(answer),
|
|
145
|
+
conditions: now.except('time', 'interval'), utc_offset: answer['utc_offset_seconds'].to_i)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def days_of(answer)
|
|
149
|
+
daily = answer.fetch('daily')
|
|
150
|
+
daily.fetch('time').each_index.map do |index|
|
|
151
|
+
Day.new(date: daily['time'][index], high: daily['temperature_2m_max'][index], low: daily['temperature_2m_min'][index],
|
|
152
|
+
code: daily['weather_code'][index], uv: daily['uv_index_clear_sky_max']&.at(index))
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# Your time zone, like Australia/Melbourne, and its city, from tzdata.
|
|
157
|
+
module TimeZone
|
|
158
|
+
module_function
|
|
159
|
+
|
|
160
|
+
# Your time zone's name, from TZ or else where /etc/localtime points, or nil.
|
|
161
|
+
def name
|
|
162
|
+
zone = ENV.fetch('TZ', nil)&.delete_prefix(':')
|
|
163
|
+
zone = File.readlink('/etc/localtime')[%r{zoneinfo/(.+)\z}, 1] if zone.nil? || zone.empty?
|
|
164
|
+
zone
|
|
165
|
+
rescue SystemCallError
|
|
166
|
+
nil
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# The zone's city, where zone.tab puts it, its name from the zone's, like Melbourne, or nil for a zone without one.
|
|
170
|
+
def place(zone, zone_tab: ZONE_TAB)
|
|
171
|
+
line = File.foreach(zone_tab).find { !it.start_with?('#') && it.split("\t")[2] == zone }
|
|
172
|
+
return unless line
|
|
173
|
+
|
|
174
|
+
country, coordinates = line.split("\t")
|
|
175
|
+
latitude, longitude = coordinates.scan(/[+-]\d+/).map { degrees(it) }
|
|
176
|
+
Place.new(name: zone.split('/').last.tr('_', ' '), latitude:, longitude:, country:)
|
|
177
|
+
rescue SystemCallError
|
|
178
|
+
nil
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# Degrees from ISO 6709's ±DDMM, ±DDDMM, ±DDMMSS or ±DDDMMSS.
|
|
182
|
+
def degrees(text)
|
|
183
|
+
digits = text[1..]
|
|
184
|
+
whole = 2 + (digits.length % 2)
|
|
185
|
+
parts = [digits[0, whole], digits[whole, 2], digits[whole + 2, 2]].map(&:to_i)
|
|
186
|
+
value = (parts[0] + (parts[1] / 60.0) + (parts[2] / 3600.0)).round(4)
|
|
187
|
+
return -value if text.start_with?('-')
|
|
188
|
+
|
|
189
|
+
value
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
end
|