Skip to content

Commit 11cb3fa

Browse files
Rename Colors to Styles
1 parent 1b8e035 commit 11cb3fa

File tree

9 files changed

+162
-135
lines changed

9 files changed

+162
-135
lines changed

docsite/source/colorizing-your-output.html.md

-55
This file was deleted.

docsite/source/index.html.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ sections:
1212
- variadic-arguments
1313
- commands-with-subcommands-and-params
1414
- callbacks
15-
- colorizing-your-output
15+
- styling-your-output
1616
---
1717

1818
`dry-cli` is a general-purpose framework for developing Command Line Interface (CLI) applications. It represents commands as objects that can be registered and offers support for arguments, options and forwarding variadic arguments to a sub-command.
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: Styling your output
3+
layout: gem-single
4+
name: dry-cli
5+
---
6+
7+
`dry-cli` comes with some functions to help you style text in the terminal. The program bellow demonstrate all available styles:
8+
9+
```ruby
10+
#!/usr/bin/env ruby
11+
require "bundler/setup"
12+
require "dry/cli"
13+
14+
module StylesDemo
15+
extend Dry::CLI::Registry
16+
17+
class Print < Dry::CLI::Command
18+
desc "Demonstrate all available styles"
19+
20+
# rubocop:disable Metrics/AbcSize
21+
def call
22+
demo = <<~DEMO
23+
`bold` #{bold("This is bold")}
24+
`dim` #{dim("This is dim")}
25+
`italic` #{italic("This is italic")}
26+
`underline` #{underline("This is underline")}
27+
`blink` #{blink("This blinks")}
28+
`reverse` #{reverse("This was reversed")}
29+
`invisible` #{invisible("This is invisible")} (you can't see it, right?)
30+
`black` #{black("This is black")}
31+
`red` #{red("This is red")}
32+
`green` #{green("This is green")}
33+
`yellow` #{yellow("This is yellow")}
34+
`blue` #{blue("This is blue")}
35+
`magenta` #{magenta("This is magenta")}
36+
`cyan` #{cyan("This is cyan")}
37+
`white` #{white("This is white")}
38+
`on_black` #{on_black("This is black")}
39+
`on_red` #{on_red("This is red")}
40+
`on_green` #{on_green("This is green")}
41+
`on_yellow` #{on_yellow("This is yellow")}
42+
`on_blue` #{on_blue("This is blue")}
43+
`on_magenta` #{on_magenta("This is magenta")}
44+
`on_cyan` #{on_cyan("This is cyan")}
45+
`on_white` #{on_white("This is white")}
46+
`bold`+`red`: #{bold(red("This is bold red"))}
47+
`bold`+`on_green`: #{bold(on_green("This is bold on green"))}
48+
`bold`+`red`+`on_green`: #{bold(red(on_green("This is bold red on green")))}
49+
DEMO
50+
puts demo
51+
end
52+
# rubocop:enable Metrics/AbcSize
53+
end
54+
55+
register "print", Print
56+
end
57+
58+
Dry.CLI(StylesDemo).call
59+
```

lib/dry/cli/command.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
require "forwardable"
44
require "dry/cli/option"
5-
require "dry/cli/colors"
5+
require "dry/cli/styles"
66

77
module Dry
88
class CLI
99
# Base class for commands
1010
#
1111
# @since 0.1.0
1212
class Command
13-
include Colors
13+
include Styles
1414

1515
# @since 0.1.0
1616
# @api private

lib/dry/cli/colors.rb lib/dry/cli/styles.rb

+32-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,45 @@
22

33
module Dry
44
class CLI
5-
# Collection of functions to colorize text.
5+
# Collection of functions to style text.
66
#
77
# @since 1.3.0
8-
module Colors
8+
module Styles
99
# since 1.3.0
1010
def bold(text)
1111
ensure_clean_sequence("\e[1m#{text}")
1212
end
1313

14+
# since 1.3.0
15+
def dim(text)
16+
ensure_clean_sequence("\e[2m#{text}")
17+
end
18+
19+
# since 1.3.0
20+
def italic(text)
21+
ensure_clean_sequence("\e[3m#{text}")
22+
end
23+
24+
# since 1.3.0
25+
def underline(text)
26+
ensure_clean_sequence("\e[4m#{text}")
27+
end
28+
29+
# since 1.3.0
30+
def blink(text)
31+
ensure_clean_sequence("\e[5m#{text}")
32+
end
33+
34+
# since 1.3.0
35+
def reverse(text)
36+
ensure_clean_sequence("\e[7m#{text}")
37+
end
38+
39+
# since 1.3.0
40+
def invisible(text)
41+
ensure_clean_sequence("\e[8m#{text}")
42+
end
43+
1444
# since 1.3.0
1545
def black(text)
1646
ensure_clean_sequence("\e[30m#{text}")

spec/support/fixtures/colors

-49
This file was deleted.

spec/support/fixtures/styles

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
$LOAD_PATH.unshift "#{__dir__}/../../../lib"
5+
require "dry/cli"
6+
7+
module StylesDemo
8+
extend Dry::CLI::Registry
9+
10+
class Print < Dry::CLI::Command
11+
desc "Demonstrate all available styles"
12+
13+
# rubocop:disable Metrics/AbcSize
14+
def call
15+
demo = <<~DEMO
16+
`bold` #{bold("This is bold")}
17+
`dim` #{dim("This is dim")}
18+
`italic` #{italic("This is italic")}
19+
`underline` #{underline("This is underline")}
20+
`blink` #{blink("This blinks")}
21+
`reverse` #{reverse("This was reversed")}
22+
`invisible` #{invisible("This is invisible")} (you can't see it, right?)
23+
`black` #{black("This is black")}
24+
`red` #{red("This is red")}
25+
`green` #{green("This is green")}
26+
`yellow` #{yellow("This is yellow")}
27+
`blue` #{blue("This is blue")}
28+
`magenta` #{magenta("This is magenta")}
29+
`cyan` #{cyan("This is cyan")}
30+
`white` #{white("This is white")}
31+
`on_black` #{on_black("This is black")}
32+
`on_red` #{on_red("This is red")}
33+
`on_green` #{on_green("This is green")}
34+
`on_yellow` #{on_yellow("This is yellow")}
35+
`on_blue` #{on_blue("This is blue")}
36+
`on_magenta` #{on_magenta("This is magenta")}
37+
`on_cyan` #{on_cyan("This is cyan")}
38+
`on_white` #{on_white("This is white")}
39+
`bold`+`red`: #{bold(red("This is bold red"))}
40+
`bold`+`on_green`: #{bold(on_green("This is bold on green"))}
41+
`bold`+`red`+`on_green`: #{bold(red(on_green("This is bold red on green")))}
42+
DEMO
43+
puts demo
44+
end
45+
# rubocop:enable Metrics/AbcSize
46+
end
47+
48+
register "print", Print
49+
end
50+
51+
Dry.CLI(StylesDemo).call

spec/unit/dry/cli/colors_spec.rb

-26
This file was deleted.

spec/unit/dry/cli/styles_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe "Styles" do
4+
class Dummy
5+
include Dry::CLI::Styles
6+
7+
def bold_and_black
8+
black(bold("two"))
9+
end
10+
end
11+
12+
it "doesn't duplicate clean escape sequences" do
13+
bold_and_black = Dummy.new.bold_and_black
14+
expect(bold_and_black.end_with?("\e[0m")).to eq(true)
15+
expect(bold_and_black.end_with?("\e[0m\e[0m")).to eq(false)
16+
end
17+
end

0 commit comments

Comments
 (0)