eskimo-ascii 3.0.0.pre.2 → 3.0.0.pre.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/eskimo/ascii.rb +5 -5
- data/lib/eskimo/ascii/component.rb +30 -28
- data/lib/eskimo/ascii/components/did_you_mean.rb +23 -21
- data/lib/eskimo/ascii/components/either.rb +20 -18
- data/lib/eskimo/ascii/components/gutter.rb +23 -21
- data/lib/eskimo/ascii/components/highlight.rb +21 -19
- data/lib/eskimo/ascii/components/highlight_column.rb +48 -46
- data/lib/eskimo/ascii/components/indent.rb +17 -15
- data/lib/eskimo/ascii/components/line_break.rb +12 -10
- data/lib/eskimo/ascii/components/soft_break.rb +10 -8
- data/lib/eskimo/ascii/components/spacer.rb +24 -22
- data/lib/eskimo/ascii/components/squeeze.rb +33 -31
- data/lib/eskimo/ascii/components/strip.rb +9 -7
- data/lib/eskimo/ascii/components/strip_left.rb +9 -7
- data/lib/eskimo/ascii/components/strip_right.rb +9 -7
- data/lib/eskimo/ascii/components/style.rb +16 -14
- data/lib/eskimo/ascii/components/truncate.rb +20 -18
- data/lib/eskimo/ascii/components/truncate_rear.rb +15 -13
- data/lib/eskimo/ascii/constants.rb +4 -2
- data/lib/eskimo/ascii/version.rb +1 -1
- data/spec/component_spec.rb +6 -0
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77c7011dd75b1e275fb94531d839512948e3ca92144b4ddbef0f870374358366
|
4
|
+
data.tar.gz: d2684e0564cb79124700ef925a58895c70d15e6a4990f230f41803c8e3a8c3c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8be5a37bac7af74c8779ce4b0dcec516158472a7848d2954b109da0884abde12507835a7ef7085ef69fbe161b3a6bbd6b7083194b4e38088b6520f60563f023d
|
7
|
+
data.tar.gz: 2bb90b39558f64422d1f4a2f2055401e054aee9b727bc901960fefcbe96ea70e29848d2adc2dbb74406b35bb14d28cc5c02d84d9c6364a89a5ad20dfe470f859
|
data/lib/eskimo/ascii.rb
CHANGED
@@ -1,8 +1,3 @@
|
|
1
|
-
module Eskimo
|
2
|
-
module ASCII
|
3
|
-
end
|
4
|
-
end
|
5
|
-
|
6
1
|
begin
|
7
2
|
require 'did_you_mean'
|
8
3
|
rescue LoadError
|
@@ -12,6 +7,11 @@ require 'pastel'
|
|
12
7
|
require 'strings'
|
13
8
|
require 'tty-screen'
|
14
9
|
|
10
|
+
module Eskimo
|
11
|
+
module ASCII
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
15
|
require 'eskimo/ascii/version'
|
16
16
|
require 'eskimo/ascii/constants'
|
17
17
|
require 'eskimo/ascii/component'
|
@@ -1,35 +1,37 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
# text
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
3
|
+
module Eskimo::ASCII
|
4
|
+
# A base component class that renders child components defined in a block into
|
5
|
+
# a String for further formatting.
|
6
|
+
#
|
7
|
+
# class MyComponent < Eskimo::ASCII::Component
|
8
|
+
# def render(**)
|
9
|
+
# text = super
|
10
|
+
# text.is_a?(String) # => true
|
11
|
+
# end
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# Use of this class is optional. What's happening under the hood is:
|
15
|
+
#
|
16
|
+
# 1. Component maintains a reference to the Proc passed to {#initialize}. That
|
17
|
+
# Proc can potentially return a list of child components to render.
|
18
|
+
#
|
19
|
+
# 2. {Component#render} (called via "super" from sub-classes) invokes the
|
20
|
+
# `render` prop provided by {Core::Renderer#apply} with the tracked children
|
21
|
+
# which converts them to a String and returns them.
|
22
|
+
class Component
|
23
|
+
def initialize(*, **, &children_gen)
|
24
|
+
@children = children_gen
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
def render(render:, **)
|
28
|
+
render[@children]
|
29
|
+
end
|
29
30
|
|
30
|
-
|
31
|
+
protected
|
31
32
|
|
32
|
-
|
33
|
-
|
33
|
+
def pastel
|
34
|
+
@pastel ||= Pastel.new
|
35
|
+
end
|
34
36
|
end
|
35
37
|
end
|
@@ -1,30 +1,32 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
|
13
|
-
|
3
|
+
module Eskimo::ASCII
|
4
|
+
# Present the user with the closest possible correction, if any.
|
5
|
+
#
|
6
|
+
# DidYouMean.new(dictionary: [ 'abc', 'bca' ], item: 'abd')
|
7
|
+
# # => "hint: Did you mean? abc"
|
8
|
+
#
|
9
|
+
# DidYouMean.new(dictionary: [ 'abc', 'bca' ], item: 'asdfasdf')
|
10
|
+
# # => ""
|
11
|
+
#
|
12
|
+
# See https://github.com/yuki24/did_you_mean#using-the-didyoumeanspellchecker
|
13
|
+
class DidYouMean < Component
|
14
|
+
attr_reader :corrections, :separator
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
def initialize(dictionary:, item:, separator: " or ", &children)
|
17
|
+
@corrections = ::DidYouMean::SpellChecker.new(
|
18
|
+
dictionary: dictionary
|
19
|
+
).correct(item)
|
19
20
|
|
20
|
-
|
21
|
+
@separator = separator
|
21
22
|
|
22
|
-
|
23
|
-
|
23
|
+
super
|
24
|
+
end
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
def render(**)
|
27
|
+
if corrections.any?
|
28
|
+
"Did you mean? #{corrections.join(separator)}"
|
29
|
+
end
|
28
30
|
end
|
29
31
|
end
|
30
32
|
end
|
@@ -1,26 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
|
13
|
-
|
3
|
+
module Eskimo::ASCII
|
4
|
+
# Render a fallback component in case the first one evaluates to an empty
|
5
|
+
# String.
|
6
|
+
#
|
7
|
+
# Either.new(->(**) { false }, "Fallback here")
|
8
|
+
# # => "Fallback here"
|
9
|
+
#
|
10
|
+
# Either.new(->(**) { "Hi!" }, "Fallback here")
|
11
|
+
# # => "Hi!"
|
12
|
+
#
|
13
|
+
class Either
|
14
|
+
attr_reader :children
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
def initialize(primary, fallback)
|
17
|
+
@children = [ primary, fallback ]
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
def render(render:, **)
|
21
|
+
for child in children do
|
22
|
+
rendered = render[child]
|
22
23
|
|
23
|
-
|
24
|
+
return rendered unless rendered.empty?
|
25
|
+
end
|
24
26
|
end
|
25
27
|
end
|
26
28
|
end
|
@@ -1,29 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
# #
|
10
|
-
|
11
|
-
|
3
|
+
module Eskimo::ASCII
|
4
|
+
# Prepend each line with a character or symbol.
|
5
|
+
#
|
6
|
+
# Gutter.new(char: '| ') do
|
7
|
+
# [ "Hello", "\n", "World!" ]
|
8
|
+
# end
|
9
|
+
# # => "| Hello"
|
10
|
+
# # "| World!"
|
11
|
+
class Gutter < Component
|
12
|
+
attr_reader :char, :spacing
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
def initialize(char:, spacing: 0, &children)
|
15
|
+
@char = char
|
16
|
+
@spacing = spacing
|
16
17
|
|
17
|
-
|
18
|
-
|
18
|
+
super
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
21
|
+
def render(**)
|
22
|
+
spacer = Array.new(spacing, char)
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
[
|
25
|
+
*spacer,
|
26
|
+
super.lines.map { |s| s.prepend(char) }.join,
|
27
|
+
*spacer
|
28
|
+
].join("\n")
|
29
|
+
end
|
28
30
|
end
|
29
31
|
end
|
@@ -1,27 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
# #
|
10
|
-
#
|
11
|
-
|
12
|
-
|
3
|
+
module Eskimo::ASCII
|
4
|
+
# Highlight a substring with ASCII arrows.
|
5
|
+
#
|
6
|
+
# Highlight.new(pattern: /lol/) do
|
7
|
+
# "- include: lol://file.yml"
|
8
|
+
# end
|
9
|
+
# # => "- include: lol://file.yml"
|
10
|
+
# # ^^^
|
11
|
+
#
|
12
|
+
class Highlight < Component
|
13
|
+
attr_reader :pastel, :pattern, :style
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
def initialize(pattern:, style: [:red, :bold, :underline], &children)
|
16
|
+
@pastel = Pastel.new
|
17
|
+
@pattern = pattern
|
18
|
+
@style = style
|
18
19
|
|
19
|
-
|
20
|
-
|
20
|
+
super(&children)
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
def render(**)
|
24
|
+
super.sub(pattern) do |substring|
|
25
|
+
pastel.decorate(substring, *style)
|
26
|
+
end
|
25
27
|
end
|
26
28
|
end
|
27
29
|
end
|
@@ -1,57 +1,59 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
# "
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
1
|
+
module Eskimo::ASCII
|
2
|
+
# Highlight a particular character of a string with an ASCII arrow.
|
3
|
+
#
|
4
|
+
# HighlightColumn.new(line: 0, column: 14) do
|
5
|
+
# "- include: lol://wut.yml"
|
6
|
+
# end
|
7
|
+
# => "- include: lol://wut.yml"
|
8
|
+
# " ^ "
|
9
|
+
# " here "
|
10
|
+
class HighlightColumn < Component
|
11
|
+
def initialize(
|
12
|
+
column:,
|
13
|
+
line:,
|
14
|
+
markers: ['^', 'here'],
|
15
|
+
style: [:bold, :red],
|
16
|
+
&children
|
17
|
+
)
|
18
|
+
pastel = Pastel.new
|
19
|
+
|
20
|
+
@colorize = ->(str) { pastel.decorate(str, *style) }
|
21
|
+
@column = column
|
22
|
+
@line = line
|
23
|
+
@marker_padding = ' ' * @column
|
24
|
+
@markers = markers
|
25
|
+
|
26
|
+
super
|
27
|
+
end
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
def render(**)
|
30
|
+
lines = super.lines
|
31
|
+
line = lines[@line]
|
31
32
|
|
32
|
-
|
33
|
-
|
33
|
+
unless line.nil? || line[@column].nil?
|
34
|
+
lines[@line] = transform_line!(line, @column, &@colorize)
|
35
|
+
end
|
36
|
+
|
37
|
+
lines.join
|
34
38
|
end
|
35
39
|
|
36
|
-
|
37
|
-
end
|
40
|
+
protected
|
38
41
|
|
39
|
-
|
42
|
+
def create_markers()
|
43
|
+
buf = ''
|
40
44
|
|
41
|
-
|
42
|
-
|
45
|
+
for marker in @markers do
|
46
|
+
buf << @marker_padding + @colorize[marker] + "\n"
|
47
|
+
end
|
43
48
|
|
44
|
-
|
45
|
-
buf << @marker_padding + @colorize[marker] + "\n"
|
49
|
+
buf
|
46
50
|
end
|
47
51
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
line << create_markers
|
55
|
-
line
|
52
|
+
def transform_line!(line, column, &fn)
|
53
|
+
line[column] = fn[line[column]]
|
54
|
+
line << "\n" unless line.end_with?("\n")
|
55
|
+
line << create_markers
|
56
|
+
line
|
57
|
+
end
|
56
58
|
end
|
57
59
|
end
|
@@ -1,21 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
# #
|
10
|
-
|
11
|
-
|
3
|
+
module Eskimo::ASCII
|
4
|
+
# Indent text from the left.
|
5
|
+
#
|
6
|
+
# Indent.new(width: 8) do
|
7
|
+
# [ "Hello", SoftBreak.new, "World!" ]
|
8
|
+
# end
|
9
|
+
# # => " Hello"
|
10
|
+
# # " World!"
|
11
|
+
class Indent < Component
|
12
|
+
attr_reader :width
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
def initialize(width: 4, &children)
|
15
|
+
@width = width
|
16
|
+
super
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
def render(**)
|
20
|
+
Strings.pad(super, [0,0,0,width])
|
21
|
+
end
|
20
22
|
end
|
21
23
|
end
|
@@ -1,14 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# #
|
8
|
-
# # ""
|
9
|
-
# # "
|
10
|
-
|
11
|
-
|
12
|
-
|
3
|
+
module Eskimo::ASCII
|
4
|
+
# Insert a paragraph-like line-break.
|
5
|
+
#
|
6
|
+
# [ "Hello", LineBreak.new, "World!" ]
|
7
|
+
# # => "Hello"
|
8
|
+
# # ""
|
9
|
+
# # ""
|
10
|
+
# # "World!"
|
11
|
+
class LineBreak
|
12
|
+
def render(**)
|
13
|
+
"\n \n"
|
14
|
+
end
|
13
15
|
end
|
14
16
|
end
|
@@ -1,12 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# #
|
8
|
-
|
9
|
-
|
10
|
-
|
3
|
+
module Eskimo::ASCII
|
4
|
+
# Insert a soft line-break.
|
5
|
+
#
|
6
|
+
# [ "Hello", SoftBreak.new, "World!" ]
|
7
|
+
# # => "Hello"
|
8
|
+
# # "World!"
|
9
|
+
class SoftBreak
|
10
|
+
def render(**)
|
11
|
+
"\n"
|
12
|
+
end
|
11
13
|
end
|
12
14
|
end
|
@@ -1,29 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# "
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# #
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
3
|
+
module Eskimo::ASCII
|
4
|
+
# Space consecutive components with soft breaks.
|
5
|
+
#
|
6
|
+
# Spacer.new([
|
7
|
+
# "Hello",
|
8
|
+
# "World!"
|
9
|
+
# ])
|
10
|
+
# # => "Hello"
|
11
|
+
# # "World!"
|
12
|
+
#
|
13
|
+
# The soft breaks for each conditional component will be preserved only
|
14
|
+
# if they do render some content.
|
15
|
+
class Spacer
|
16
|
+
def initialize(children)
|
17
|
+
if !children.is_a?(Array) || block_given?
|
18
|
+
raise ArgumentError.new("Spacer works only with an Array of components")
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
21
|
+
@children = children
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
24
|
+
def render(**props)
|
25
|
+
rendered = @children.map(&props[:render])
|
25
26
|
|
26
|
-
|
27
|
-
|
27
|
+
without_blanks = rendered.reject(&:empty?)
|
28
|
+
without_blanks.join("\n")
|
29
|
+
end
|
28
30
|
end
|
29
31
|
end
|
@@ -1,39 +1,41 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
# #
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
3
|
+
module Eskimo::ASCII
|
4
|
+
# Squeeze immediate consecutive soft breaks.
|
5
|
+
#
|
6
|
+
# Squeeze.new do
|
7
|
+
# [
|
8
|
+
# SoftBreak.new,
|
9
|
+
# ConditionalComponent.new,
|
10
|
+
#
|
11
|
+
# SoftBreak.new,
|
12
|
+
# false && SomeOtherComponent.new,
|
13
|
+
#
|
14
|
+
# SoftBreak.new,
|
15
|
+
# 'hello',
|
16
|
+
# ]
|
17
|
+
# end
|
18
|
+
# # => ""
|
19
|
+
# # "hello"
|
20
|
+
#
|
21
|
+
# The soft breaks for each conditional component will be preserved only
|
22
|
+
# if they do render some content.
|
23
|
+
class Squeeze
|
24
|
+
def initialize(children)
|
25
|
+
if !children.is_a?(Array) || block_given?
|
26
|
+
raise ArgumentError.new("Squeeze works only with an Array of components")
|
27
|
+
end
|
27
28
|
|
28
|
-
|
29
|
-
|
29
|
+
@children = children
|
30
|
+
end
|
30
31
|
|
31
|
-
|
32
|
-
|
32
|
+
def render(**props)
|
33
|
+
rendered = @children.map(&props[:render])
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
35
|
+
without_blanks = rendered.reject(&:empty?)
|
36
|
+
without_blanks.reject.with_index do |element, index|
|
37
|
+
element == "\n" && without_blanks[index+1] == "\n"
|
38
|
+
end
|
37
39
|
end
|
38
40
|
end
|
39
41
|
end
|
@@ -1,11 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
module Eskimo::ASCII
|
4
|
+
# Remove surrounding whitespace.
|
5
|
+
#
|
6
|
+
# Strip.new { " hello world " }
|
7
|
+
# # => "hello world"
|
8
|
+
class Strip < Component
|
9
|
+
def render(**)
|
10
|
+
super.strip
|
11
|
+
end
|
10
12
|
end
|
11
13
|
end
|
@@ -1,11 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
module Eskimo::ASCII
|
4
|
+
# Remove whitespace from the beginning.
|
5
|
+
#
|
6
|
+
# StripLeft.new { " hello world " }
|
7
|
+
# # => "hello world "
|
8
|
+
class StripLeft < Component
|
9
|
+
def render(**)
|
10
|
+
super.lstrip
|
11
|
+
end
|
10
12
|
end
|
11
13
|
end
|
@@ -1,11 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
module Eskimo::ASCII
|
4
|
+
# Remove whitespace from the end.
|
5
|
+
#
|
6
|
+
# StripRight.new { " hello world " }
|
7
|
+
# # => " hello world"
|
8
|
+
class StripRight < Component
|
9
|
+
def render(**)
|
10
|
+
super.rstrip
|
11
|
+
end
|
10
12
|
end
|
11
13
|
end
|
@@ -1,21 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
|
9
|
-
|
3
|
+
module Eskimo::ASCII
|
4
|
+
# Style text with colors and custom formatting.
|
5
|
+
#
|
6
|
+
# See [Pastel's documentation][pastel] for the accepted styles.
|
7
|
+
#
|
8
|
+
# [pastel]: https://github.com/piotrmurach/pastel
|
9
|
+
class Style < Component
|
10
|
+
attr_reader :pastel, :style
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
def initialize(*style, &children)
|
13
|
+
@style = style.flatten
|
14
|
+
@pastel = Pastel.new
|
14
15
|
|
15
|
-
|
16
|
-
|
16
|
+
super
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
def render(**)
|
20
|
+
pastel.decorate(super, *style)
|
21
|
+
end
|
20
22
|
end
|
21
23
|
end
|
@@ -1,27 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
|
10
|
-
|
3
|
+
module Eskimo::ASCII
|
4
|
+
# Truncate text from the beginning if it exceeds a certain width.
|
5
|
+
#
|
6
|
+
# Truncate.new(width: 3) do
|
7
|
+
# "foo bar"
|
8
|
+
# end
|
9
|
+
# # => "... bar"
|
10
|
+
class Truncate < Component
|
11
|
+
attr_reader :maxlen
|
11
12
|
|
12
|
-
|
13
|
-
|
13
|
+
def initialize(reserve: 0, width: Constants::SCREEN_COLUMNS, &children)
|
14
|
+
@maxlen = [0, width - reserve].max
|
14
15
|
|
15
|
-
|
16
|
-
|
16
|
+
super
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
def render(**)
|
20
|
+
text = super
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
if text.length >= maxlen
|
23
|
+
'...' + text[text.length - maxlen - 1 .. -1]
|
24
|
+
else
|
25
|
+
text
|
26
|
+
end
|
25
27
|
end
|
26
28
|
end
|
27
29
|
end
|
@@ -2,20 +2,22 @@
|
|
2
2
|
|
3
3
|
require 'eskimo/ascii/components/truncate'
|
4
4
|
|
5
|
-
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
|
12
|
-
|
13
|
-
|
5
|
+
module Eskimo::ASCII
|
6
|
+
# Truncate text from the rear if it exceeds a certain width.
|
7
|
+
#
|
8
|
+
# TruncateRear.new(width: 3) do
|
9
|
+
# "foo bar"
|
10
|
+
# end
|
11
|
+
# # => "foo..."
|
12
|
+
class TruncateRear < Truncate
|
13
|
+
def render(render:, **)
|
14
|
+
text = render[@children]
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
if text.length >= maxlen
|
17
|
+
text[0..maxlen - 1] + '...'
|
18
|
+
else
|
19
|
+
text
|
20
|
+
end
|
19
21
|
end
|
20
22
|
end
|
21
23
|
end
|
data/lib/eskimo/ascii/version.rb
CHANGED
data/spec/component_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eskimo-ascii
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.0.pre.
|
4
|
+
version: 3.0.0.pre.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ahmad Amireh
|
@@ -14,16 +14,16 @@ dependencies:
|
|
14
14
|
name: eskimo-core
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 3.0.0.pre.
|
19
|
+
version: 3.0.0.pre.3
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 3.0.0.pre.
|
26
|
+
version: 3.0.0.pre.3
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: pastel
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|