mini_progress 0.1.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 +7 -0
- data/LICENSE.txt +21 -0
- data/lib/mini_progress/factory.rb +24 -0
- data/lib/mini_progress/formatting.rb +38 -0
- data/lib/mini_progress/locales/mini_progress.en.yml +25 -0
- data/lib/mini_progress/plain.rb +171 -0
- data/lib/mini_progress/reporter.rb +162 -0
- data/lib/mini_progress/tui/ansi.rb +78 -0
- data/lib/mini_progress/tui/renderer.rb +543 -0
- data/lib/mini_progress/tui.rb +221 -0
- data/lib/mini_progress/version.rb +5 -0
- data/lib/mini_progress.rb +32 -0
- metadata +97 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 550001f661a8fd761bf314ae9adb9c4aa0affee5bf550798610384ceae25e4aa
|
|
4
|
+
data.tar.gz: ea328592af2939dea1076e1452a1197008e08c139b20d531b62c52f0ec2d3f80
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: fb91776936815bfd4e6f1f42512c747312abbb7f94e66a0a58ba2526934852d162bf5e71b09abc29f8286c35fb6bc29acd0868cb6f765d1dd8ed8bb9629c290e
|
|
7
|
+
data.tar.gz: 5d35aa9036399f2eddc4f7483fd87fa7098f050f123f03264fa5724e7b40d62a53848893dbb466e81a74049244aa05d1adc82e0f5041881960aac6484b5dc658
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Civilized Discourse Construction Kit, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MiniProgress
|
|
4
|
+
# Picks the reporter for a run. {Tui}, with its live progress region, needs a
|
|
5
|
+
# real terminal. Everything else (pipes, CI, and dumb terminals) gets the
|
|
6
|
+
# line-based {Plain}. To force plain output on a real terminal, set
|
|
7
|
+
# `TERM=dumb`.
|
|
8
|
+
module Factory
|
|
9
|
+
def self.build(output: $stdout, titles: [])
|
|
10
|
+
if plain?(output)
|
|
11
|
+
Plain.new(output:)
|
|
12
|
+
else
|
|
13
|
+
Tui.new(output:, titles:)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.plain?(output)
|
|
18
|
+
return true unless output.respond_to?(:tty?) && output.tty?
|
|
19
|
+
|
|
20
|
+
term = ENV["TERM"].to_s
|
|
21
|
+
term.empty? || term == "dumb"
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The number helper relies on ActiveSupport's autoload setup, which only the
|
|
4
|
+
# base require installs.
|
|
5
|
+
require "active_support"
|
|
6
|
+
require "active_support/number_helper"
|
|
7
|
+
|
|
8
|
+
module MiniProgress
|
|
9
|
+
# Formats counts and durations. Both reporters use it, so the TUI and the
|
|
10
|
+
# plain output look the same.
|
|
11
|
+
module Formatting
|
|
12
|
+
def format_count(number)
|
|
13
|
+
ActiveSupport::NumberHelper.number_to_delimited(number)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Builds a count label with the right singular/plural form. `count` picks
|
|
17
|
+
# the form; `number` is the same value already grouped, which the
|
|
18
|
+
# translation inserts.
|
|
19
|
+
def count_label(kind, count)
|
|
20
|
+
I18n.t("mini_progress.#{kind}", count:, number: format_count(count))
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Formats a duration as M:SS, switching to H:MM:SS past an hour.
|
|
24
|
+
# Sub-second durations show as "<1s": rounding them up to "0:01" would
|
|
25
|
+
# overstate them, and actual milliseconds would mix units into the time
|
|
26
|
+
# column, which makes it hard to scan.
|
|
27
|
+
def format_duration(seconds)
|
|
28
|
+
return "<1s" if seconds < 1
|
|
29
|
+
|
|
30
|
+
whole = seconds.ceil
|
|
31
|
+
if whole >= 3600
|
|
32
|
+
format("%d:%02d:%02d", whole / 3600, whole % 3600 / 60, whole % 60)
|
|
33
|
+
else
|
|
34
|
+
format("%d:%02d", whole / 60, whole % 60)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
---
|
|
2
|
+
en:
|
|
3
|
+
mini_progress:
|
|
4
|
+
counting: counting…
|
|
5
|
+
eta: ETA
|
|
6
|
+
failed: failed
|
|
7
|
+
interrupted: interrupted
|
|
8
|
+
interrupted_at: interrupted at %{percent}%
|
|
9
|
+
processed: processed
|
|
10
|
+
skips:
|
|
11
|
+
one: "%{number} skip"
|
|
12
|
+
other: "%{number} skips"
|
|
13
|
+
warnings:
|
|
14
|
+
one: "%{number} warning"
|
|
15
|
+
other: "%{number} warnings"
|
|
16
|
+
errors:
|
|
17
|
+
one: "%{number} error"
|
|
18
|
+
other: "%{number} errors"
|
|
19
|
+
finishing_up: Finishing up…
|
|
20
|
+
total: Total
|
|
21
|
+
steps:
|
|
22
|
+
one: "%{number} step"
|
|
23
|
+
other: "%{number} steps"
|
|
24
|
+
steps_failed: "%{number} failed"
|
|
25
|
+
steps_skipped: "%{number} skipped"
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MiniProgress
|
|
4
|
+
# Line-based reporting for pipes, CI logs, and dumb terminals — anywhere
|
|
5
|
+
# {Tui} can't run ({Factory} decides). It never moves the cursor and has no
|
|
6
|
+
# live region. It prints one line when a step starts and one when it
|
|
7
|
+
# finishes, one line as progress advances (every 10% by default; or, when the
|
|
8
|
+
# total is unknown, one every few seconds), and notices as they come. It only writes whole
|
|
9
|
+
# lines, behind a mutex, so two threads can't mix half-written lines together.
|
|
10
|
+
class Plain < Reporter
|
|
11
|
+
include Formatting
|
|
12
|
+
|
|
13
|
+
HEARTBEAT_SECONDS = 5 # how often to print progress when the total is unknown
|
|
14
|
+
PROGRESS_LOG_INTERVAL = 10 # print a progress line every N percent
|
|
15
|
+
|
|
16
|
+
Step =
|
|
17
|
+
Struct.new(
|
|
18
|
+
:title,
|
|
19
|
+
:started_at,
|
|
20
|
+
:total,
|
|
21
|
+
:current,
|
|
22
|
+
:logged_percent,
|
|
23
|
+
:last_heartbeat,
|
|
24
|
+
:skip_count,
|
|
25
|
+
:warning_count,
|
|
26
|
+
:error_count,
|
|
27
|
+
keyword_init: true,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
def initialize(output: $stdout, clock: nil)
|
|
31
|
+
super()
|
|
32
|
+
@output = output
|
|
33
|
+
@output.sync = true if @output.respond_to?(:sync=)
|
|
34
|
+
@clock = clock || -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
|
|
35
|
+
@steps = {}
|
|
36
|
+
@mutex = Mutex.new
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def report_start(id, title)
|
|
40
|
+
@mutex.synchronize do
|
|
41
|
+
now = monotonic
|
|
42
|
+
@steps[id] = Step.new(
|
|
43
|
+
title:,
|
|
44
|
+
started_at: now,
|
|
45
|
+
total: nil,
|
|
46
|
+
current: 0,
|
|
47
|
+
logged_percent: 0,
|
|
48
|
+
last_heartbeat: now,
|
|
49
|
+
skip_count: 0,
|
|
50
|
+
warning_count: 0,
|
|
51
|
+
error_count: 0,
|
|
52
|
+
)
|
|
53
|
+
@output.puts(title)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def report_notice(id, message)
|
|
58
|
+
@mutex.synchronize do
|
|
59
|
+
step = @steps[id]
|
|
60
|
+
@output.puts(" #{step.title} #{message}") if step
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def report_progress_begin(id, max_progress)
|
|
65
|
+
@mutex.synchronize { @steps[id].total = max_progress if @steps[id] }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def report_concurrency(_id, _count)
|
|
69
|
+
# No-op: the plain log skips the live concurrency count.
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def report_progress(id, current, skip_count, warning_count, error_count)
|
|
73
|
+
@mutex.synchronize do
|
|
74
|
+
step = @steps[id]
|
|
75
|
+
next unless step
|
|
76
|
+
|
|
77
|
+
step.current = current
|
|
78
|
+
step.skip_count = skip_count
|
|
79
|
+
step.warning_count = warning_count
|
|
80
|
+
step.error_count = error_count
|
|
81
|
+
|
|
82
|
+
if step.total&.positive?
|
|
83
|
+
log_progress(step, current)
|
|
84
|
+
else
|
|
85
|
+
log_heartbeat(step, current)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def report_finish(id, outcome)
|
|
91
|
+
@mutex.synchronize do
|
|
92
|
+
step = @steps[id]
|
|
93
|
+
next unless step
|
|
94
|
+
|
|
95
|
+
count = outcome == :done ? step.total || step.current : step.current
|
|
96
|
+
line = +"#{finish_glyph(outcome)} #{step.title}"
|
|
97
|
+
line << " #{format_count(count)}" if count > 0
|
|
98
|
+
line << " (#{format_duration(monotonic - step.started_at)})"
|
|
99
|
+
line << " — #{I18n.t("mini_progress.#{outcome}")}" unless outcome == :done
|
|
100
|
+
line << annotations(step)
|
|
101
|
+
@output.puts(line)
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def report_finalizing_begin
|
|
106
|
+
@mutex.synchronize { @output.puts(I18n.t("mini_progress.finishing_up")) }
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def report_summary(runtime:, total:, failed:, skipped:)
|
|
110
|
+
tally = [I18n.t("mini_progress.steps", count: total, number: total)]
|
|
111
|
+
tally << I18n.t("mini_progress.steps_failed", number: failed) if failed > 0
|
|
112
|
+
tally << I18n.t("mini_progress.steps_skipped", number: skipped) if skipped > 0
|
|
113
|
+
@mutex.synchronize do
|
|
114
|
+
@output.puts(
|
|
115
|
+
"#{I18n.t("mini_progress.total")}: #{tally.join(", ")} (#{format_duration(runtime)})",
|
|
116
|
+
)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def close
|
|
121
|
+
# nothing to free — we write plain lines and keep nothing open
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
private
|
|
125
|
+
|
|
126
|
+
def log_progress(step, current)
|
|
127
|
+
percent = [current * 100 / step.total, 100].min
|
|
128
|
+
milestone = percent - percent % PROGRESS_LOG_INTERVAL
|
|
129
|
+
return if milestone <= step.logged_percent
|
|
130
|
+
|
|
131
|
+
step.logged_percent = milestone
|
|
132
|
+
@output.puts(
|
|
133
|
+
" #{step.title} #{milestone}% (#{format_count(current)}/#{format_count(step.total)})" \
|
|
134
|
+
"#{annotations(step)}",
|
|
135
|
+
)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def log_heartbeat(step, current)
|
|
139
|
+
now = monotonic
|
|
140
|
+
return if now - step.last_heartbeat < HEARTBEAT_SECONDS
|
|
141
|
+
|
|
142
|
+
step.last_heartbeat = now
|
|
143
|
+
@output.puts(
|
|
144
|
+
" #{step.title} #{format_count(current)} #{I18n.t("mini_progress.processed")}#{annotations(step)}",
|
|
145
|
+
)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def finish_glyph(outcome)
|
|
149
|
+
case outcome
|
|
150
|
+
when :failed
|
|
151
|
+
"✗"
|
|
152
|
+
when :interrupted
|
|
153
|
+
"⚠"
|
|
154
|
+
else
|
|
155
|
+
"✓"
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def annotations(step)
|
|
160
|
+
parts = []
|
|
161
|
+
parts << count_label(:skips, step.skip_count) if step.skip_count > 0
|
|
162
|
+
parts << count_label(:warnings, step.warning_count) if step.warning_count > 0
|
|
163
|
+
parts << count_label(:errors, step.error_count) if step.error_count > 0
|
|
164
|
+
parts.empty? ? "" : " — #{parts.join(", ")}"
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def monotonic
|
|
168
|
+
@clock.call
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MiniProgress
|
|
4
|
+
# Reports the progress of a run to the user. A run has one reporter. Call
|
|
5
|
+
# `start_step` to begin a step; it returns a {StepHandle} for that step. Each
|
|
6
|
+
# step gets its own id. The caller never sees the id, but it lets the reporter
|
|
7
|
+
# keep each step's notices and progress apart. This holds even when steps run
|
|
8
|
+
# at the same time, report from another thread, or have the same title.
|
|
9
|
+
#
|
|
10
|
+
# {Tui} and {Plain} are the two reporters; {Factory} picks one. Each one
|
|
11
|
+
# implements the methods below and `close`. {StepHandle} and {Progress} are
|
|
12
|
+
# shared by both.
|
|
13
|
+
class Reporter
|
|
14
|
+
def initialize
|
|
15
|
+
@step_seq = 0
|
|
16
|
+
@step_seq_mutex = Mutex.new
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Starts a step and returns a handle for it. The id stays internal; the
|
|
20
|
+
# caller only uses the handle.
|
|
21
|
+
def start_step(title)
|
|
22
|
+
id = @step_seq_mutex.synchronize { @step_seq += 1 }
|
|
23
|
+
report_start(id, title)
|
|
24
|
+
StepHandle.new(self, id)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Shows a transient "finishing up" status while the block runs, then clears
|
|
28
|
+
# it. Used for the tail of a run where every step is done but background work
|
|
29
|
+
# is still finishing, so the display doesn't just sit at 100%. Returns the
|
|
30
|
+
# block's value.
|
|
31
|
+
def finalizing
|
|
32
|
+
report_finalizing_begin
|
|
33
|
+
yield
|
|
34
|
+
ensure
|
|
35
|
+
report_finalizing_end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Prints the end-of-run summary line: the total runtime and how many steps
|
|
39
|
+
# ran (with any that failed or were skipped).
|
|
40
|
+
# @param runtime [Float] the run's wall-clock seconds
|
|
41
|
+
# @param total [Integer] how many steps the run had
|
|
42
|
+
# @param failed [Integer] how many failed
|
|
43
|
+
# @param skipped [Integer] how many were skipped because a dependency failed
|
|
44
|
+
def report_summary(runtime:, total:, failed:, skipped:)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# The run is over and nothing else will be reported. Free anything held
|
|
48
|
+
# here. Called once per run, also when the run fails.
|
|
49
|
+
def close
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# --- The methods each reporter implements. {StepHandle} calls them. They
|
|
53
|
+
# use the step id to tell steps apart; only `report_start` also gets the
|
|
54
|
+
# title. ---
|
|
55
|
+
|
|
56
|
+
def report_start(_id, _title)
|
|
57
|
+
raise NotImplementedError
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def report_notice(_id, _message)
|
|
61
|
+
raise NotImplementedError
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def report_progress_begin(_id, _max_progress)
|
|
65
|
+
raise NotImplementedError
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def report_concurrency(_id, _count)
|
|
69
|
+
raise NotImplementedError
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def report_progress(_id, _current, _skip_count, _warning_count, _error_count)
|
|
73
|
+
raise NotImplementedError
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# `outcome` is `:done`, `:interrupted`, or `:failed`.
|
|
77
|
+
def report_finish(_id, _outcome)
|
|
78
|
+
raise NotImplementedError
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Show and clear the transient "finishing up" status; `finalizing` wraps them.
|
|
82
|
+
def report_finalizing_begin
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def report_finalizing_end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# A handle for one step. It holds the step's id, so everything it reports
|
|
89
|
+
# goes to the right step, no matter which thread calls it.
|
|
90
|
+
class StepHandle
|
|
91
|
+
def initialize(reporter, id)
|
|
92
|
+
@reporter = reporter
|
|
93
|
+
@id = id
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def notice(message)
|
|
97
|
+
@reporter.report_notice(@id, message)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def report_concurrency(count)
|
|
101
|
+
@reporter.report_concurrency(@id, count)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def begin_progress(max_progress:)
|
|
105
|
+
@reporter.report_progress_begin(@id, max_progress)
|
|
106
|
+
Progress.new(@reporter, @id)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Yields a {Progress}. You may call its `update` from any thread. Pass
|
|
110
|
+
# `max_progress: nil` when the total is not known.
|
|
111
|
+
def with_progress(max_progress:)
|
|
112
|
+
yield begin_progress(max_progress:)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Ends the step. With no `outcome` it runs from an `ensure` and reads `$!`
|
|
116
|
+
# (the exception in flight, if any) to tell how the step ended: a
|
|
117
|
+
# SignalException means Ctrl-C (interrupted), any other exception means it
|
|
118
|
+
# failed, and no exception means it finished cleanly. Pass `outcome:`
|
|
119
|
+
# explicitly when the step ended without an exception in flight — e.g. a
|
|
120
|
+
# threaded pipeline that catches SIGINT with a flag and drains, so `$!` is
|
|
121
|
+
# already nil by the time the step finishes.
|
|
122
|
+
def finish(outcome: nil)
|
|
123
|
+
outcome ||=
|
|
124
|
+
case $!
|
|
125
|
+
when nil
|
|
126
|
+
:done
|
|
127
|
+
when SignalException
|
|
128
|
+
:interrupted
|
|
129
|
+
else
|
|
130
|
+
:failed
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
@reporter.report_finish(@id, outcome)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Adds up the per-batch counts the caller reports and keeps the running
|
|
138
|
+
# totals, then sends them on to the reporter. Thread-safe: calling `update`
|
|
139
|
+
# from several threads at once still gives correct totals.
|
|
140
|
+
class Progress
|
|
141
|
+
def initialize(reporter, id)
|
|
142
|
+
@reporter = reporter
|
|
143
|
+
@id = id
|
|
144
|
+
@current = 0
|
|
145
|
+
@skip_count = 0
|
|
146
|
+
@warning_count = 0
|
|
147
|
+
@error_count = 0
|
|
148
|
+
@mutex = Mutex.new
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def update(increment_by:, skip_count: 0, warning_count: 0, error_count: 0)
|
|
152
|
+
@mutex.synchronize do
|
|
153
|
+
@current += increment_by
|
|
154
|
+
@skip_count += skip_count
|
|
155
|
+
@warning_count += warning_count
|
|
156
|
+
@error_count += error_count
|
|
157
|
+
@reporter.report_progress(@id, @current, @skip_count, @warning_count, @error_count)
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "unicode/display_width"
|
|
4
|
+
|
|
5
|
+
module MiniProgress
|
|
6
|
+
class Tui
|
|
7
|
+
# ANSI and text-width helpers. `unicode-display_width` only looks values up
|
|
8
|
+
# in a table. It never asks the terminal anything (unlike Reline's `\e[6n`),
|
|
9
|
+
# so it can't hang.
|
|
10
|
+
module Ansi
|
|
11
|
+
RESET = "\e[0m"
|
|
12
|
+
BOLD = "\e[1m"
|
|
13
|
+
GREEN = "\e[32m"
|
|
14
|
+
YELLOW = "\e[33m"
|
|
15
|
+
RED = "\e[31m"
|
|
16
|
+
CYAN = "\e[36m"
|
|
17
|
+
MAGENTA = "\e[35m"
|
|
18
|
+
DIM = "\e[90m"
|
|
19
|
+
ERASE_LINE = "\e[2K" # erase the whole line (also clears the terminal's wrap flag)
|
|
20
|
+
ERASE_BELOW = "\e[0J" # erase from the cursor to the end of the screen
|
|
21
|
+
HIDE_CURSOR = "\e[?25l"
|
|
22
|
+
SHOW_CURSOR = "\e[?25h"
|
|
23
|
+
|
|
24
|
+
# Matches SGR ("Select Graphic Rendition") sequences — the color and
|
|
25
|
+
# style codes above, e.g. "\e[1m". They have no visible width, so the
|
|
26
|
+
# width and truncate helpers below strip or skip them.
|
|
27
|
+
SGR = /\e\[[0-9;]*m/
|
|
28
|
+
|
|
29
|
+
def self.cursor_up(count)
|
|
30
|
+
"\e[#{count}A"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# The display width, ignoring SGR color codes. It only removes them
|
|
34
|
+
# (which makes a copy) when the string really has an escape; most cells
|
|
35
|
+
# are plain text.
|
|
36
|
+
def self.width(string)
|
|
37
|
+
string = string.gsub(SGR, "") if string.include?("\e")
|
|
38
|
+
Unicode::DisplayWidth.of(string)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Cut the line down to a visible width, keeping the SGR color codes whole.
|
|
42
|
+
def self.truncate(line, max)
|
|
43
|
+
return line if width(line) <= max
|
|
44
|
+
|
|
45
|
+
out = +""
|
|
46
|
+
used = 0
|
|
47
|
+
emitted_sgr = false
|
|
48
|
+
line.scan(/#{SGR}|\X/) do |token|
|
|
49
|
+
if token.start_with?("\e")
|
|
50
|
+
out << token
|
|
51
|
+
emitted_sgr = true
|
|
52
|
+
next
|
|
53
|
+
end
|
|
54
|
+
w = Unicode::DisplayWidth.of(token)
|
|
55
|
+
break if used + w > max
|
|
56
|
+
out << token
|
|
57
|
+
used += w
|
|
58
|
+
end
|
|
59
|
+
out << RESET if emitted_sgr
|
|
60
|
+
out
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Pad to a display width (wide characters count as 2; SGR codes ignored).
|
|
64
|
+
def self.pad(string, target, align = :left)
|
|
65
|
+
pad_to(string, width(string), target, align)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Like `Ansi.pad` above, but the caller already knows the string's display
|
|
69
|
+
# width. This way each cell is measured once, and the width is reused for
|
|
70
|
+
# both the column sizing and the padding.
|
|
71
|
+
def self.pad_to(string, string_width, target, align = :left)
|
|
72
|
+
gap = target - string_width
|
|
73
|
+
return string if gap <= 0
|
|
74
|
+
align == :right ? "#{" " * gap}#{string}" : "#{string}#{" " * gap}"
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|