yorha 1.9.4
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 +7 -0
- data/CHANGELOG.md +58 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/exe/yorha +19 -0
- data/lib/yorha/access_point.rb +280 -0
- data/lib/yorha/banner.rb +87 -0
- data/lib/yorha/black_box.rb +187 -0
- data/lib/yorha/chips.rb +317 -0
- data/lib/yorha/cli.rb +280 -0
- data/lib/yorha/config.rb +208 -0
- data/lib/yorha/core_ext.rb +61 -0
- data/lib/yorha/errors.rb +77 -0
- data/lib/yorha/machine.rb +108 -0
- data/lib/yorha/palette.rb +86 -0
- data/lib/yorha/pod.rb +200 -0
- data/lib/yorha/quotes.rb +71 -0
- data/lib/yorha/render.rb +94 -0
- data/lib/yorha/terminal.rb +192 -0
- data/lib/yorha/version.rb +11 -0
- data/lib/yorha/widgets.rb +384 -0
- data/lib/yorha.rb +139 -0
- metadata +69 -0
data/lib/yorha/cli.rb
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Command-line interface for the currently implemented YoRHa components
|
|
4
|
+
# (Integration layer).
|
|
5
|
+
#
|
|
6
|
+
# Design document reference: section 5.11, Yorha::CLI.
|
|
7
|
+
#
|
|
8
|
+
# Responsibilities:
|
|
9
|
+
# - Parse command options.
|
|
10
|
+
# - Route commands to the appropriate component.
|
|
11
|
+
# - Keep normal output and error output separate.
|
|
12
|
+
# - Return an exit status rather than terminating the host application.
|
|
13
|
+
|
|
14
|
+
module Yorha
|
|
15
|
+
class CLI
|
|
16
|
+
# Exit status for a command that completed.
|
|
17
|
+
#
|
|
18
|
+
# @return [Integer]
|
|
19
|
+
SUCCESS = 0
|
|
20
|
+
|
|
21
|
+
# Exit status for an error the gem raised on purpose (Yorha::Error).
|
|
22
|
+
|
|
23
|
+
FAILURE = 1
|
|
24
|
+
|
|
25
|
+
# Exit status for a command line the CLI could not understand.
|
|
26
|
+
|
|
27
|
+
USAGE_ERROR = 2
|
|
28
|
+
|
|
29
|
+
# Everything printed by help below the version line.
|
|
30
|
+
|
|
31
|
+
USAGE = <<~USAGE
|
|
32
|
+
|
|
33
|
+
Usage:
|
|
34
|
+
yorha boot
|
|
35
|
+
yorha quote
|
|
36
|
+
yorha message [--title TITLE] TEXT
|
|
37
|
+
yorha doctor
|
|
38
|
+
yorha palette
|
|
39
|
+
yorha chips
|
|
40
|
+
yorha speak [--anger NUMBER] [--cps NUMBER] TEXT
|
|
41
|
+
yorha --version
|
|
42
|
+
yorha --help
|
|
43
|
+
|
|
44
|
+
Commands:
|
|
45
|
+
boot Play the YoRHa boot sequence.
|
|
46
|
+
quote Print a random YoRHa quotation.
|
|
47
|
+
message Show text in a bordered system message box.
|
|
48
|
+
doctor Show detected terminal capabilities.
|
|
49
|
+
palette Preview the active palette roles.
|
|
50
|
+
chips List chips installed in this process.
|
|
51
|
+
speak Print optional machine-style speech.
|
|
52
|
+
|
|
53
|
+
Examples:
|
|
54
|
+
yorha boot
|
|
55
|
+
yorha message --title NOTICE "Backup complete."
|
|
56
|
+
yorha speak --anger 0.4 "BECOME AS GODS"
|
|
57
|
+
USAGE
|
|
58
|
+
|
|
59
|
+
# Creates a CLI instance with injectable streams.
|
|
60
|
+
|
|
61
|
+
def initialize(io: Yorha.config.io, err: Yorha.config.err)
|
|
62
|
+
@io = io
|
|
63
|
+
@err = err
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Parses and runs one command.
|
|
67
|
+
#
|
|
68
|
+
|
|
69
|
+
def run(argv = ARGV)
|
|
70
|
+
command, *arguments = argv
|
|
71
|
+
|
|
72
|
+
case command
|
|
73
|
+
when nil, "help", "--help", "-h"
|
|
74
|
+
show_help
|
|
75
|
+
when "version", "--version", "-v"
|
|
76
|
+
show_version
|
|
77
|
+
when "boot"
|
|
78
|
+
boot(arguments)
|
|
79
|
+
when "quote"
|
|
80
|
+
quote(arguments)
|
|
81
|
+
when "message"
|
|
82
|
+
message(arguments)
|
|
83
|
+
when "doctor"
|
|
84
|
+
doctor(arguments)
|
|
85
|
+
when "palette"
|
|
86
|
+
palette(arguments)
|
|
87
|
+
when "chips"
|
|
88
|
+
chips(arguments)
|
|
89
|
+
when "speak"
|
|
90
|
+
speak(arguments)
|
|
91
|
+
else
|
|
92
|
+
raise ArgumentError, "invalid argument: unknown command #{command.inspect}"
|
|
93
|
+
end
|
|
94
|
+
rescue ArgumentError => error
|
|
95
|
+
@err.write("YoRHa: #{error.message}\nRun yorha --help for usage.\n")
|
|
96
|
+
USAGE_ERROR
|
|
97
|
+
rescue Yorha::Error => error
|
|
98
|
+
@err.write("YoRHa: #{error.message}\n")
|
|
99
|
+
FAILURE
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
private
|
|
103
|
+
|
|
104
|
+
# Prints the version line followed by the supported commands.
|
|
105
|
+
|
|
106
|
+
def show_help
|
|
107
|
+
@io.write("YoRHa #{Yorha.version}\n#{USAGE}")
|
|
108
|
+
|
|
109
|
+
SUCCESS
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Prints the gem version on its own line.
|
|
113
|
+
|
|
114
|
+
def show_version
|
|
115
|
+
@io.write("#{Yorha.version}\n")
|
|
116
|
+
|
|
117
|
+
SUCCESS
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Plays the boot sequence on the CLI's output stream.
|
|
121
|
+
#
|
|
122
|
+
|
|
123
|
+
def boot(arguments)
|
|
124
|
+
reject_extra_arguments!(arguments)
|
|
125
|
+
|
|
126
|
+
Yorha::Banner.boot(io: @io)
|
|
127
|
+
|
|
128
|
+
SUCCESS
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Prints a random quotation, with the Japanese pair when Unicode works.
|
|
132
|
+
|
|
133
|
+
def quote(arguments)
|
|
134
|
+
reject_extra_arguments!(arguments)
|
|
135
|
+
|
|
136
|
+
entry = Yorha::Quotes.pick
|
|
137
|
+
lines = []
|
|
138
|
+
|
|
139
|
+
if Yorha::Terminal.unicode?(@io)
|
|
140
|
+
lines << Yorha::Render.paint(entry.japanese, :muted, io: @io)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
lines << Yorha::Render.paint(entry.english, :accent, io: @io)
|
|
144
|
+
|
|
145
|
+
@io.write("#{lines.join("\n")}\n")
|
|
146
|
+
|
|
147
|
+
SUCCESS
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# Shows text in a bordered system message box.
|
|
151
|
+
|
|
152
|
+
def message(arguments)
|
|
153
|
+
require "optparse"
|
|
154
|
+
|
|
155
|
+
title = "SYSTEM MESSAGE"
|
|
156
|
+
|
|
157
|
+
parser = OptionParser.new do |settings|
|
|
158
|
+
settings.banner = "Usage: yorha message [--title TITLE] TEXT"
|
|
159
|
+
|
|
160
|
+
settings.on("--title TITLE", String, "Box title") do |value|
|
|
161
|
+
title = value
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
words = parser.parse(arguments)
|
|
166
|
+
|
|
167
|
+
raise OptionParser::MissingArgument, "TEXT" if words.empty?
|
|
168
|
+
|
|
169
|
+
Yorha::Widgets.system_message(title, words.join(" "), io: @io)
|
|
170
|
+
|
|
171
|
+
SUCCESS
|
|
172
|
+
rescue OptionParser::ParseError => error
|
|
173
|
+
raise ArgumentError, error.message
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Reports current output capabilities without changing configuration.
|
|
177
|
+
|
|
178
|
+
def doctor(arguments)
|
|
179
|
+
reject_extra_arguments!(arguments)
|
|
180
|
+
|
|
181
|
+
settings = Yorha.config
|
|
182
|
+
tty = @io.respond_to?(:tty?) && @io.tty?
|
|
183
|
+
palette_name = settings.palette.is_a?(Hash) ? "custom" : settings.palette
|
|
184
|
+
|
|
185
|
+
@io.write(<<~REPORT)
|
|
186
|
+
YORHA TERMINAL DIAGNOSTICS
|
|
187
|
+
Ruby: #{RUBY_VERSION}
|
|
188
|
+
Platform: #{RUBY_PLATFORM}
|
|
189
|
+
TTY: #{tty}
|
|
190
|
+
Color depth: #{Yorha::Terminal.color_depth(@io)}
|
|
191
|
+
Unicode: #{Yorha::Terminal.unicode?(@io)}
|
|
192
|
+
Width: #{Yorha::Terminal.width(@io)}
|
|
193
|
+
Palette: #{palette_name}
|
|
194
|
+
Unit: #{settings.unit}
|
|
195
|
+
REPORT
|
|
196
|
+
|
|
197
|
+
SUCCESS
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# Displays each active palette role using the renderer.
|
|
201
|
+
|
|
202
|
+
def palette(arguments)
|
|
203
|
+
reject_extra_arguments!(arguments)
|
|
204
|
+
|
|
205
|
+
lines = Yorha::Palette.current.each_key.map do |role|
|
|
206
|
+
sample = "#{role.to_s.ljust(12)} YoRHa visual sample"
|
|
207
|
+
|
|
208
|
+
"#{Yorha::Render.paint(sample, role, io: @io)}\n"
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
@io.write(lines.join)
|
|
212
|
+
|
|
213
|
+
SUCCESS
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# Lists the current process's chip registry.
|
|
217
|
+
#
|
|
218
|
+
|
|
219
|
+
def chips(arguments)
|
|
220
|
+
reject_extra_arguments!(arguments)
|
|
221
|
+
|
|
222
|
+
Yorha::Chips.grid(io: @io)
|
|
223
|
+
|
|
224
|
+
SUCCESS
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# Parses machine-speech options and writes the requested message.
|
|
228
|
+
|
|
229
|
+
def speak(arguments)
|
|
230
|
+
require "optparse"
|
|
231
|
+
|
|
232
|
+
options = { anger: 0.2, cps: 40.0 }
|
|
233
|
+
|
|
234
|
+
parser = OptionParser.new do |settings|
|
|
235
|
+
settings.banner = "Usage: yorha speak [options] TEXT"
|
|
236
|
+
|
|
237
|
+
settings.on("--anger NUMBER", Float, "Corruption probability, 0..1") do |value|
|
|
238
|
+
options[:anger] = value
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
settings.on("--cps NUMBER", Float, "Characters per second") do |value|
|
|
242
|
+
options[:cps] = value
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
settings.on("-h", "--help", "Show this help") do
|
|
246
|
+
options[:help] = true
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
settings.on("--version", "Show the YoRHa version") do
|
|
250
|
+
options[:version] = true
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
words = parser.parse(arguments)
|
|
255
|
+
|
|
256
|
+
if options[:help]
|
|
257
|
+
@io.write(parser.help)
|
|
258
|
+
return SUCCESS
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
return show_version if options[:version]
|
|
262
|
+
|
|
263
|
+
raise OptionParser::MissingArgument, "TEXT" if words.empty?
|
|
264
|
+
|
|
265
|
+
Yorha::Machine.speak(words.join(" "), io: @io, anger: options[:anger], cps: options[:cps])
|
|
266
|
+
|
|
267
|
+
SUCCESS
|
|
268
|
+
rescue OptionParser::ParseError => error
|
|
269
|
+
raise ArgumentError, error.message
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
# Rejects arguments for commands that do not accept any.
|
|
273
|
+
|
|
274
|
+
def reject_extra_arguments!(arguments)
|
|
275
|
+
return if arguments.empty?
|
|
276
|
+
|
|
277
|
+
raise ArgumentError, "invalid argument: unexpected arguments: #{arguments.join(' ')}"
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
end
|
data/lib/yorha/config.rb
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Configuration value object for the YoRHa gem.
|
|
4
|
+
#
|
|
5
|
+
# Responsibility:
|
|
6
|
+
# Store the preferences used by terminal detection, rendering, banners,
|
|
7
|
+
# and future utility components.
|
|
8
|
+
#
|
|
9
|
+
# This file does NOT:
|
|
10
|
+
# - Detect terminal capabilities.
|
|
11
|
+
# - Render colors or glyphs.
|
|
12
|
+
# - Print messages.
|
|
13
|
+
# - Install chips.
|
|
14
|
+
# - Change an output stream's encoding.
|
|
15
|
+
#
|
|
16
|
+
# Dependencies:
|
|
17
|
+
# Ruby core classes only.
|
|
18
|
+
|
|
19
|
+
module Yorha
|
|
20
|
+
# Holds the settings shared by YoRHa components.
|
|
21
|
+
#
|
|
22
|
+
# A newly constructed Config is a mutable settings builder.
|
|
23
|
+
# The public Yorha.config API exposes a frozen snapshot instead.
|
|
24
|
+
#
|
|
25
|
+
# Application code changes settings through:
|
|
26
|
+
#
|
|
27
|
+
# Yorha.configure do |settings|
|
|
28
|
+
# settings.color = false
|
|
29
|
+
# settings.unit = "YoRHa-9S"
|
|
30
|
+
# end
|
|
31
|
+
#
|
|
32
|
+
# Supported setting types are documented below. Type/range validation
|
|
33
|
+
# is not implemented in this checkpoint.
|
|
34
|
+
#
|
|
35
|
+
# @api public
|
|
36
|
+
class Config
|
|
37
|
+
# Requested color mode.
|
|
38
|
+
#
|
|
39
|
+
# Values:
|
|
40
|
+
# nil - Ask Terminal to detect color support later.
|
|
41
|
+
# true - Force truecolor.
|
|
42
|
+
# false - Disable ANSI color output.
|
|
43
|
+
# :none - Disable ANSI color output.
|
|
44
|
+
# :truecolor - Use 24-bit RGB colors.
|
|
45
|
+
# :ansi256 - Use the 256-color ANSI palette.
|
|
46
|
+
# :ansi16 - Use the basic ANSI color palette.
|
|
47
|
+
#
|
|
48
|
+
# Storing this preference does not emit ANSI sequences.
|
|
49
|
+
#
|
|
50
|
+
# @return [nil, Boolean, Symbol] requested color mode
|
|
51
|
+
attr_accessor :color
|
|
52
|
+
|
|
53
|
+
# Whether Unicode glyphs may be used.
|
|
54
|
+
#
|
|
55
|
+
# Values:
|
|
56
|
+
# nil - Ask Terminal to detect Unicode support later.
|
|
57
|
+
# true - Allow Unicode glyphs.
|
|
58
|
+
# false - Use ASCII alternatives.
|
|
59
|
+
#
|
|
60
|
+
# This setting does not change fonts or output encodings.
|
|
61
|
+
#
|
|
62
|
+
# @return [nil, Boolean] requested Unicode behaviour
|
|
63
|
+
attr_accessor :unicode
|
|
64
|
+
|
|
65
|
+
# Selected palette name or custom palette data.
|
|
66
|
+
#
|
|
67
|
+
# Named palettes will be resolved by Yorha::Palette later.
|
|
68
|
+
# This configuration object does not check whether a name exists.
|
|
69
|
+
#
|
|
70
|
+
# Custom palette data must contain ordinary, acyclic hashes/arrays,
|
|
71
|
+
# strings, and immutable scalar values such as symbols and integers.
|
|
72
|
+
#
|
|
73
|
+
# @return [Symbol, Hash] selected palette
|
|
74
|
+
attr_accessor :palette
|
|
75
|
+
|
|
76
|
+
# Default destination for normal visual output.
|
|
77
|
+
#
|
|
78
|
+
# Usually $stdout, but tests can inject StringIO.
|
|
79
|
+
# YoRHa does not own this stream and must not close or freeze it.
|
|
80
|
+
#
|
|
81
|
+
# @return [#write] writable normal-output stream
|
|
82
|
+
attr_accessor :io
|
|
83
|
+
|
|
84
|
+
# Default destination for diagnostic output.
|
|
85
|
+
#
|
|
86
|
+
# Usually $stderr. Keeping it separate from normal output prevents
|
|
87
|
+
# error reports from being mixed into data written to stdout.
|
|
88
|
+
#
|
|
89
|
+
# @return [#write] writable diagnostic-output stream
|
|
90
|
+
attr_accessor :err
|
|
91
|
+
|
|
92
|
+
# Unit label displayed by future banners and diagnostic reports.
|
|
93
|
+
#
|
|
94
|
+
# This is presentation text only. It does not affect permissions,
|
|
95
|
+
# process identity, or any operating-system settings.
|
|
96
|
+
#
|
|
97
|
+
# @return [String] display label
|
|
98
|
+
attr_accessor :unit
|
|
99
|
+
|
|
100
|
+
# Capacity budget for the future plug-in chip registry.
|
|
101
|
+
#
|
|
102
|
+
# Defining this value does not create a registry or install chips.
|
|
103
|
+
#
|
|
104
|
+
# @return [Integer] intended nonnegative capacity
|
|
105
|
+
attr_accessor :capacity
|
|
106
|
+
|
|
107
|
+
# Preferred language for future locale-aware presentation.
|
|
108
|
+
#
|
|
109
|
+
# Values:
|
|
110
|
+
# :en - English.
|
|
111
|
+
# :ja - Japanese.
|
|
112
|
+
#
|
|
113
|
+
# Storing :ja does not itself implement translations or force
|
|
114
|
+
# Unicode output.
|
|
115
|
+
#
|
|
116
|
+
# @return [Symbol] preferred locale
|
|
117
|
+
attr_accessor :locale
|
|
118
|
+
|
|
119
|
+
# Creates a mutable settings builder with the default values.
|
|
120
|
+
#
|
|
121
|
+
# Standard streams are captured when this instance is created.
|
|
122
|
+
# Creating the configuration does not write to either stream.
|
|
123
|
+
#
|
|
124
|
+
# @return [Config] a mutable settings builder
|
|
125
|
+
def initialize
|
|
126
|
+
@color = nil
|
|
127
|
+
@unicode = nil
|
|
128
|
+
@palette = :yorha
|
|
129
|
+
|
|
130
|
+
@io = $stdout
|
|
131
|
+
@err = $stderr
|
|
132
|
+
|
|
133
|
+
@unit = "YoRHa-2B"
|
|
134
|
+
@capacity = 32
|
|
135
|
+
@locale = :en
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Creates a fresh, frozen configuration containing the defaults.
|
|
139
|
+
#
|
|
140
|
+
# Each call creates a different object. This lets tests restore a
|
|
141
|
+
# clean configuration without reusing a previous snapshot.
|
|
142
|
+
#
|
|
143
|
+
# @return [Config] a new frozen default snapshot
|
|
144
|
+
def self.default
|
|
145
|
+
new.snapshot
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Converts the current builder into a separate frozen snapshot.
|
|
149
|
+
#
|
|
150
|
+
# The original builder remains mutable.
|
|
151
|
+
#
|
|
152
|
+
# Strings and containers belonging to unit/palette are copied before
|
|
153
|
+
# freezing. This prevents later caller-side changes from modifying
|
|
154
|
+
# an already-published configuration.
|
|
155
|
+
#
|
|
156
|
+
# Output streams are deliberately kept as shared references.
|
|
157
|
+
# Freezing or duplicating them would interfere with their normal use.
|
|
158
|
+
#
|
|
159
|
+
# This method assumes documented setting types; it does not validate
|
|
160
|
+
# settings or verify that a named palette is registered.
|
|
161
|
+
#
|
|
162
|
+
# @return [Config] a distinct frozen configuration snapshot
|
|
163
|
+
def snapshot
|
|
164
|
+
copy = dup
|
|
165
|
+
|
|
166
|
+
copy.unit = immutable_copy(unit)
|
|
167
|
+
copy.palette = immutable_copy(palette)
|
|
168
|
+
|
|
169
|
+
copy.freeze
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
private
|
|
173
|
+
|
|
174
|
+
# Copies supported mutable setting data and freezes the copies.
|
|
175
|
+
#
|
|
176
|
+
# Behaviour:
|
|
177
|
+
# String - Copy and freeze the string.
|
|
178
|
+
# Array - Recursively copy its items, then freeze the new array.
|
|
179
|
+
# Hash - Recursively copy keys/values, then freeze the new hash.
|
|
180
|
+
# Other - Reuse the value, which should be an immutable scalar.
|
|
181
|
+
#
|
|
182
|
+
# This is a settings-data copier, not a general serializer.
|
|
183
|
+
# Self-referencing containers and arbitrary mutable objects are
|
|
184
|
+
# outside its supported input contract.
|
|
185
|
+
#
|
|
186
|
+
# Never apply this helper to output streams.
|
|
187
|
+
#
|
|
188
|
+
# @param value [Object] supported unit or palette value
|
|
189
|
+
# @return [Object] protected copy or original immutable scalar
|
|
190
|
+
# @api private
|
|
191
|
+
def immutable_copy(value)
|
|
192
|
+
case value
|
|
193
|
+
when String
|
|
194
|
+
value.dup.freeze
|
|
195
|
+
when Array
|
|
196
|
+
value.map do |item|
|
|
197
|
+
immutable_copy(item)
|
|
198
|
+
end.freeze
|
|
199
|
+
when Hash
|
|
200
|
+
value.each_with_object({}) do |(key, item), result|
|
|
201
|
+
result[immutable_copy(key)] = immutable_copy(item)
|
|
202
|
+
end.freeze
|
|
203
|
+
else
|
|
204
|
+
value
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Explicitly opt-in String extensions.
|
|
4
|
+
#
|
|
5
|
+
# Load with:
|
|
6
|
+
#
|
|
7
|
+
# require "yorha/core_ext"
|
|
8
|
+
#
|
|
9
|
+
# This file adds methods to Ruby's String class. Requiring "yorha" alone
|
|
10
|
+
# must not load it.
|
|
11
|
+
#
|
|
12
|
+
# Helpers return strings; they do not print.
|
|
13
|
+
|
|
14
|
+
require_relative "../yorha"
|
|
15
|
+
|
|
16
|
+
module Yorha
|
|
17
|
+
module CoreExt
|
|
18
|
+
# String instance methods installed only by this opt-in file.
|
|
19
|
+
module StringMethods
|
|
20
|
+
# Renders this string using a semantic palette role.
|
|
21
|
+
#
|
|
22
|
+
# @param role [Symbol] palette role
|
|
23
|
+
# @param io [Object] stream used for capability detection
|
|
24
|
+
# @return [String] colored or plain string
|
|
25
|
+
#
|
|
26
|
+
# @example
|
|
27
|
+
# puts "SYSTEM READY".yorha(:accent)
|
|
28
|
+
def yorha(role = :fg, io: Yorha.config.io)
|
|
29
|
+
Yorha::Render.paint(self, role, io: io)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Renders this string using the accent role.
|
|
33
|
+
#
|
|
34
|
+
# @param io [Object] stream used for capability detection
|
|
35
|
+
# @return [String] rendered text
|
|
36
|
+
def yorha_accent(io: Yorha.config.io)
|
|
37
|
+
yorha(:accent, io: io)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Renders this string using the muted role.
|
|
41
|
+
#
|
|
42
|
+
# @param io [Object] stream used for capability detection
|
|
43
|
+
# @return [String] rendered text
|
|
44
|
+
def yorha_muted(io: Yorha.config.io)
|
|
45
|
+
yorha(:muted, io: io)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Renders this string using the danger role.
|
|
49
|
+
#
|
|
50
|
+
# @param io [Object] stream used for capability detection
|
|
51
|
+
# @return [String] rendered text
|
|
52
|
+
def yorha_danger(io: Yorha.config.io)
|
|
53
|
+
yorha(:danger, io: io)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Including the same module again does not create duplicate ancestors.
|
|
60
|
+
# This remains a global String change, which is why loading is explicit.
|
|
61
|
+
String.include(Yorha::CoreExt::StringMethods)
|
data/lib/yorha/errors.rb
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
module Yorha
|
|
5
|
+
# Base class for every error intentionally raised by the YoRHa gem.
|
|
6
|
+
#
|
|
7
|
+
# Rescuing this class catches every gem-specific error:
|
|
8
|
+
#
|
|
9
|
+
# begin
|
|
10
|
+
# Yorha.install_chip(:oversized, cost: 99)
|
|
11
|
+
# rescue Yorha::Error => error
|
|
12
|
+
# warn error.message
|
|
13
|
+
# end
|
|
14
|
+
#
|
|
15
|
+
# @api public
|
|
16
|
+
class Error < StandardError
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Raised when installing a chip would exceed the registry capacity.
|
|
20
|
+
#
|
|
21
|
+
# Carries the details a caller needs to print a useful message.
|
|
22
|
+
#
|
|
23
|
+
# @api public
|
|
24
|
+
class ChipOverflowError < Error
|
|
25
|
+
# @return [Symbol] the chip that could not be installed
|
|
26
|
+
attr_reader :name
|
|
27
|
+
|
|
28
|
+
# @return [Integer] the capacity the chip requested
|
|
29
|
+
attr_reader :cost
|
|
30
|
+
|
|
31
|
+
# @return [Integer] the capacity still free at the attempt
|
|
32
|
+
attr_reader :free
|
|
33
|
+
|
|
34
|
+
# @param name [String, Symbol] chip identifier
|
|
35
|
+
# @param cost [Integer] requested capacity
|
|
36
|
+
# @param free [Integer] remaining capacity at the attempt
|
|
37
|
+
def initialize(name:, cost:, free:)
|
|
38
|
+
@name = name
|
|
39
|
+
@cost = cost
|
|
40
|
+
@free = free
|
|
41
|
+
|
|
42
|
+
super(
|
|
43
|
+
"Cannot install #{name.inspect}: cost #{cost} exceeds " \
|
|
44
|
+
"the #{free} free capacity")
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Raised when removing a chip that is not installed.
|
|
49
|
+
#
|
|
50
|
+
# @api public
|
|
51
|
+
class UnknownChipError < Error
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Raised when a Pod is constructed with an unregistered model.
|
|
55
|
+
#
|
|
56
|
+
# @api public
|
|
57
|
+
class UnknownPodModelError < Error
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Base class for AccessPoint failures.
|
|
61
|
+
#
|
|
62
|
+
# @api public
|
|
63
|
+
class AccessPointError < Error
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Raised when the AccessPoint API is used before connect.
|
|
67
|
+
#
|
|
68
|
+
# @api public
|
|
69
|
+
class NotConnectedError < AccessPointError
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Raised when a stored record cannot be parsed.
|
|
73
|
+
#
|
|
74
|
+
# @api public
|
|
75
|
+
class CorruptRecordError < AccessPointError
|
|
76
|
+
end
|
|
77
|
+
end
|