eskimo 2.2.0 → 3.0.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/lib/eskimo.rb +2 -29
  3. data/lib/eskimo/version.rb +1 -1
  4. metadata +16 -106
  5. data/lib/eskimo/component.rb +0 -37
  6. data/lib/eskimo/components/did_you_mean.rb +0 -34
  7. data/lib/eskimo/components/either.rb +0 -30
  8. data/lib/eskimo/components/gutter.rb +0 -33
  9. data/lib/eskimo/components/highlight.rb +0 -31
  10. data/lib/eskimo/components/highlight_column.rb +0 -61
  11. data/lib/eskimo/components/indent.rb +0 -25
  12. data/lib/eskimo/components/line_break.rb +0 -18
  13. data/lib/eskimo/components/soft_break.rb +0 -16
  14. data/lib/eskimo/components/spacer.rb +0 -33
  15. data/lib/eskimo/components/squeeze.rb +0 -43
  16. data/lib/eskimo/components/strip.rb +0 -15
  17. data/lib/eskimo/components/strip_left.rb +0 -15
  18. data/lib/eskimo/components/strip_right.rb +0 -15
  19. data/lib/eskimo/components/style.rb +0 -25
  20. data/lib/eskimo/components/truncate.rb +0 -31
  21. data/lib/eskimo/components/truncate_rear.rb +0 -25
  22. data/lib/eskimo/components/wrap.rb +0 -26
  23. data/lib/eskimo/constants.rb +0 -7
  24. data/lib/eskimo/renderer.rb +0 -46
  25. data/spec/component_spec.rb +0 -13
  26. data/spec/components/did_you_mean_spec.rb +0 -21
  27. data/spec/components/either_spec.rb +0 -21
  28. data/spec/components/gutter_spec.rb +0 -25
  29. data/spec/components/highlight_column_spec.rb +0 -63
  30. data/spec/components/highlight_spec.rb +0 -25
  31. data/spec/components/indent_spec.rb +0 -15
  32. data/spec/components/line_break_spec.rb +0 -17
  33. data/spec/components/soft_break_spec.rb +0 -17
  34. data/spec/components/spacer_spec.rb +0 -42
  35. data/spec/components/squeeze_spec.rb +0 -61
  36. data/spec/components/strip_left_spec.rb +0 -15
  37. data/spec/components/strip_right_spec.rb +0 -15
  38. data/spec/components/strip_spec.rb +0 -15
  39. data/spec/components/style_spec.rb +0 -25
  40. data/spec/components/truncate_rear_spec.rb +0 -35
  41. data/spec/components/truncate_spec.rb +0 -35
  42. data/spec/components/wrap_spec.rb +0 -15
  43. data/spec/renderer_spec.rb +0 -62
  44. data/spec/spec_helper.rb +0 -42
  45. data/spec/support/component_suite.rb +0 -15
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eskimo
4
- module Components
5
- # Indent text from the left.
6
- #
7
- # Indent.new(width: 8) do
8
- # [ "Hello", SoftBreak.new, "World!" ]
9
- # end
10
- # # => " Hello"
11
- # # " World!"
12
- class Indent < Component
13
- attr_reader :width
14
-
15
- def initialize(width: 4, &children)
16
- @width = width
17
- super
18
- end
19
-
20
- def render(**)
21
- Strings.pad(super, [0,0,0,width])
22
- end
23
- end
24
- end
25
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eskimo
4
- module Components
5
- # Insert a paragraph-like line-break.
6
- #
7
- # [ "Hello", LineBreak.new, "World!" ]
8
- # # => "Hello"
9
- # # ""
10
- # # ""
11
- # # "World!"
12
- class LineBreak
13
- def render(**)
14
- "\n \n"
15
- end
16
- end
17
- end
18
- end
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eskimo
4
- module Components
5
- # Insert a soft line-break.
6
- #
7
- # [ "Hello", SoftBreak.new, "World!" ]
8
- # # => "Hello"
9
- # # "World!"
10
- class SoftBreak
11
- def render(**)
12
- "\n"
13
- end
14
- end
15
- end
16
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eskimo
4
- module Components
5
- # Space consecutive components with soft breaks.
6
- #
7
- # Spacer.new([
8
- # "Hello",
9
- # "World!"
10
- # ])
11
- # # => "Hello"
12
- # # "World!"
13
- #
14
- # The soft breaks for each conditional component will be preserved only
15
- # if they do render some content.
16
- class Spacer
17
- def initialize(children)
18
- if !children.is_a?(Array) || block_given?
19
- raise ArgumentError.new("Spacer works only with an Array of components")
20
- end
21
-
22
- @children = children
23
- end
24
-
25
- def render(**props)
26
- rendered = @children.map(&props[:render])
27
-
28
- without_blanks = rendered.reject(&:empty?)
29
- without_blanks.join("\n")
30
- end
31
- end
32
- end
33
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eskimo
4
- module Components
5
- # Squeeze immediate consecutive soft breaks.
6
- #
7
- # Squeeze.new do
8
- # [
9
- # SoftBreak.new,
10
- # ConditionalComponent.new,
11
- #
12
- # SoftBreak.new,
13
- # false && SomeOtherComponent.new,
14
- #
15
- # SoftBreak.new,
16
- # 'hello',
17
- # ]
18
- # end
19
- # # => ""
20
- # # "hello"
21
- #
22
- # The soft breaks for each conditional component will be preserved only
23
- # if they do render some content.
24
- class Squeeze
25
- def initialize(children)
26
- if !children.is_a?(Array) || block_given?
27
- raise ArgumentError.new("Squeeze works only with an Array of components")
28
- end
29
-
30
- @children = children
31
- end
32
-
33
- def render(**props)
34
- rendered = @children.map(&props[:render])
35
-
36
- without_blanks = rendered.reject(&:empty?)
37
- without_blanks.reject.with_index do |element, index|
38
- element == "\n" && without_blanks[index+1] == "\n"
39
- end
40
- end
41
- end
42
- end
43
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eskimo
4
- module Components
5
- # Remove surrounding whitespace.
6
- #
7
- # Strip.new { " hello world " }
8
- # # => "hello world"
9
- class Strip < Component
10
- def render(**)
11
- super.strip
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eskimo
4
- module Components
5
- # Remove whitespace from the beginning.
6
- #
7
- # StripLeft.new { " hello world " }
8
- # # => "hello world "
9
- class StripLeft < Component
10
- def render(**)
11
- super.lstrip
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eskimo
4
- module Components
5
- # Remove whitespace from the end.
6
- #
7
- # StripRight.new { " hello world " }
8
- # # => " hello world"
9
- class StripRight < Component
10
- def render(**)
11
- super.rstrip
12
- end
13
- end
14
- end
15
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eskimo
4
- module Components
5
- # Style text with colors and custom formatting.
6
- #
7
- # See [Pastel's documentation][pastel] for the accepted styles.
8
- #
9
- # [pastel]: https://github.com/piotrmurach/pastel
10
- class Style < Component
11
- attr_reader :pastel, :style
12
-
13
- def initialize(*style, &children)
14
- @style = style.flatten
15
- @pastel = Pastel.new
16
-
17
- super
18
- end
19
-
20
- def render(**)
21
- pastel.decorate(super, *style)
22
- end
23
- end
24
- end
25
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eskimo
4
- module Components
5
- # Truncate text from the beginning if it exceeds a certain width.
6
- #
7
- # Truncate.new(width: 3) do
8
- # "foo bar"
9
- # end
10
- # # => "... bar"
11
- class Truncate < Component
12
- attr_reader :maxlen
13
-
14
- def initialize(reserve: 0, width: Constants::SCREEN_COLUMNS, &children)
15
- @maxlen = [0, width - reserve].max
16
-
17
- super
18
- end
19
-
20
- def render(**)
21
- text = super
22
-
23
- if text.length >= maxlen
24
- '...' + text[text.length - maxlen - 1 .. -1]
25
- else
26
- text
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'eskimo/components/truncate'
4
-
5
- module Eskimo
6
- module Components
7
- # Truncate text from the rear if it exceeds a certain width.
8
- #
9
- # TruncateRear.new(width: 3) do
10
- # "foo bar"
11
- # end
12
- # # => "foo..."
13
- class TruncateRear < Truncate
14
- def render(render:, **)
15
- text = render[@children]
16
-
17
- if text.length >= maxlen
18
- text[0..maxlen - 1] + '...'
19
- else
20
- text
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eskimo
4
- module Components
5
- # Wrap a block text with newlines at a certain threshold.
6
- #
7
- # Wrap.new(width: 3) do
8
- # "foobar"
9
- # end
10
- # # => "foo"
11
- # # "bar"
12
- #
13
- class Wrap < Component
14
- attr_reader :width
15
-
16
- def initialize(width: Constants::SCREEN_COLUMNS, &children)
17
- @width = width
18
- super
19
- end
20
-
21
- def render(**)
22
- Strings.wrap(super, width)
23
- end
24
- end
25
- end
26
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eskimo
4
- module Constants
5
- SCREEN_COLUMNS = [TTY::Screen.width, 72].min
6
- end
7
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eskimo
4
- # Render a component or a list of ones.
5
- class Renderer
6
- # @param props [Hash]
7
- # Properties to pass to each component being rendered.
8
- def initialize(**props)
9
- @props = { **props, render: method(:render) }
10
- end
11
-
12
- # @param components [Proc]
13
- # A block that returns components to render.
14
- def apply(&components)
15
- render(components)
16
- end
17
-
18
- private
19
-
20
- def render(*components)
21
- buf = +''
22
-
23
- for component in components do
24
- if component.is_a?(String)
25
- buf << component
26
- elsif component.is_a?(Array)
27
- buf << render(*component)
28
- elsif component.is_a?(Proc)
29
- buf << render(component[**@props])
30
- elsif component.respond_to?(:render)
31
- buf << render(component.render(**@props))
32
- elsif component
33
- bail(component)
34
- end
35
- end
36
-
37
- buf
38
- end
39
-
40
- def bail(component)
41
- raise ArgumentError.new(
42
- "Eskimo: don't know how to render #{component.class} => #{component}"
43
- )
44
- end
45
- end
46
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Eskimo::Component do
6
- it 'accepts a block of children' do
7
- expect(
8
- Eskimo::Renderer.new.apply {
9
- Eskimo::Component.new { ['hello', "\n", 'world!'] }
10
- }
11
- ).to eq("hello\nworld!")
12
- end
13
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Eskimo::Components::DidYouMean do
6
- it 'renders a suggestion' do
7
- expect(
8
- renderer.apply {
9
- described_class.new(dictionary: [ 'abc', 'bca' ], item: 'abd')
10
- }
11
- ).to eq("Did you mean? abc")
12
- end
13
-
14
- it 'renders nothing if no suggestions were found' do
15
- expect(
16
- renderer.apply {
17
- described_class.new(dictionary: [ 'abc', 'bca' ], item: 'asdf')
18
- }
19
- ).to eq("")
20
- end
21
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Eskimo::Components::Either do
6
- it 'renders the primary component' do
7
- expect(
8
- renderer.apply {
9
- described_class.new(->(**) { false }, "Fallback here")
10
- }
11
- ).to eq("Fallback here")
12
- end
13
-
14
- it 'renders the fallback if the primary does not' do
15
- expect(
16
- renderer.apply {
17
- described_class.new(->(**) { "Hi!" }, "Fallback here")
18
- }
19
- ).to eq("Hi!")
20
- end
21
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Eskimo::Components::Gutter do
6
- it 'prepends each line with the character' do
7
- expect(
8
- renderer.apply do
9
- ESK::Gutter.new(char: '| ') do
10
- "hello\nworld"
11
- end
12
- end
13
- ).to eq("| hello\n| world")
14
- end
15
-
16
- it 'inserts surrounding whitespace' do
17
- expect(
18
- renderer.apply do
19
- ESK::Gutter.new(char: '| ', spacing: 2) do
20
- "hello\nworld"
21
- end
22
- end
23
- ).to eq("| \n| \n| hello\n| world\n| \n| ")
24
- end
25
- end