diff --git a/docsite/source/colorizing-your-output.html.md b/docsite/source/colorizing-your-output.html.md deleted file mode 100644 index 452947e..0000000 --- a/docsite/source/colorizing-your-output.html.md +++ /dev/null @@ -1,55 +0,0 @@ ---- -title: Colorizing your output -layout: gem-single -name: dry-cli ---- - -`dry-cli` comes with some functions to help you print colored text in the terminal. The program bellow demonstrate all available functions: - -```ruby -#!/usr/bin/env ruby -require "bundler/setup" -require "dry/cli" - -module ColorsDemo - extend Dry::CLI::Registry - - class Print < Dry::CLI::Command - desc "Demonstrate all available colors" - - def call - demo = <<~DEMO - # Style ############################################################### - Bold: #{bold("This is bold")} - # Foreground ########################################################## - Black: #{black("This is black")} - Red: #{red("This is red")} - Green: #{green("This is green")} - Yellow: #{yellow("This is yellow")} - Blue: #{blue("This is blue")} - Magenta: #{magenta("This is magenta")} - Cyan: #{cyan("This is cyan")} - White: #{white("This is white")} - # Background ########################################################## - Black: #{on_black("This is black")} - Red: #{on_red("This is red")} - Green: #{on_green("This is green")} - Yellow: #{on_yellow("This is yellow")} - Blue: #{on_blue("This is blue")} - Magenta: #{on_magenta("This is magenta")} - Cyan: #{on_cyan("This is cyan")} - White: #{on_white("This is white")} - # Combinations ######################################################## - Bold+Foreground: #{bold(red("This is bold red"))} - Bold+Background: #{bold(on_green("This is bold on green"))} - Bold+Foreground+Background: #{bold(red(on_green("This is bold red on green")))} - DEMO - puts demo - end - end - - register "print", Print -end - -Dry.CLI(ColorsDemo).call -``` diff --git a/docsite/source/index.html.md b/docsite/source/index.html.md index d0e3a65..4241a07 100644 --- a/docsite/source/index.html.md +++ b/docsite/source/index.html.md @@ -12,7 +12,7 @@ sections: - variadic-arguments - commands-with-subcommands-and-params - callbacks - - colorizing-your-output + - styling-your-output --- `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. diff --git a/docsite/source/styling-your-output.html.md b/docsite/source/styling-your-output.html.md new file mode 100644 index 0000000..931657e --- /dev/null +++ b/docsite/source/styling-your-output.html.md @@ -0,0 +1,59 @@ +--- +title: Styling your output +layout: gem-single +name: dry-cli +--- + +`dry-cli` comes with some functions to help you style text in the terminal. The program bellow demonstrate all available styles: + +```ruby +#!/usr/bin/env ruby +require "bundler/setup" +require "dry/cli" + +module StylesDemo + extend Dry::CLI::Registry + + class Print < Dry::CLI::Command + desc "Demonstrate all available styles" + + # rubocop:disable Metrics/AbcSize + def call + demo = <<~DEMO + `bold` #{bold("This is bold")} + `dim` #{dim("This is dim")} + `italic` #{italic("This is italic")} + `underline` #{underline("This is underline")} + `blink` #{blink("This blinks")} + `reverse` #{reverse("This was reversed")} + `invisible` #{invisible("This is invisible")} (you can't see it, right?) + `black` #{black("This is black")} + `red` #{red("This is red")} + `green` #{green("This is green")} + `yellow` #{yellow("This is yellow")} + `blue` #{blue("This is blue")} + `magenta` #{magenta("This is magenta")} + `cyan` #{cyan("This is cyan")} + `white` #{white("This is white")} + `on_black` #{on_black("This is black")} + `on_red` #{on_red("This is red")} + `on_green` #{on_green("This is green")} + `on_yellow` #{on_yellow("This is yellow")} + `on_blue` #{on_blue("This is blue")} + `on_magenta` #{on_magenta("This is magenta")} + `on_cyan` #{on_cyan("This is cyan")} + `on_white` #{on_white("This is white")} + `bold`+`red`: #{bold(red("This is bold red"))} + `bold`+`on_green`: #{bold(on_green("This is bold on green"))} + `bold`+`red`+`on_green`: #{bold(red(on_green("This is bold red on green")))} + DEMO + puts demo + end + # rubocop:enable Metrics/AbcSize + end + + register "print", Print +end + +Dry.CLI(StylesDemo).call +``` diff --git a/lib/dry/cli/command.rb b/lib/dry/cli/command.rb index 81d7fad..2a53680 100644 --- a/lib/dry/cli/command.rb +++ b/lib/dry/cli/command.rb @@ -2,7 +2,7 @@ require "forwardable" require "dry/cli/option" -require "dry/cli/colors" +require "dry/cli/styles" module Dry class CLI @@ -10,7 +10,7 @@ class CLI # # @since 0.1.0 class Command - include Colors + include Styles # @since 0.1.0 # @api private diff --git a/lib/dry/cli/colors.rb b/lib/dry/cli/styles.rb similarity index 75% rename from lib/dry/cli/colors.rb rename to lib/dry/cli/styles.rb index 4ba4ac2..4df7fa3 100644 --- a/lib/dry/cli/colors.rb +++ b/lib/dry/cli/styles.rb @@ -2,15 +2,45 @@ module Dry class CLI - # Collection of functions to colorize text. + # Collection of functions to style text. # # @since 1.3.0 - module Colors + module Styles # since 1.3.0 def bold(text) ensure_clean_sequence("\e[1m#{text}") end + # since 1.3.0 + def dim(text) + ensure_clean_sequence("\e[2m#{text}") + end + + # since 1.3.0 + def italic(text) + ensure_clean_sequence("\e[3m#{text}") + end + + # since 1.3.0 + def underline(text) + ensure_clean_sequence("\e[4m#{text}") + end + + # since 1.3.0 + def blink(text) + ensure_clean_sequence("\e[5m#{text}") + end + + # since 1.3.0 + def reverse(text) + ensure_clean_sequence("\e[7m#{text}") + end + + # since 1.3.0 + def invisible(text) + ensure_clean_sequence("\e[8m#{text}") + end + # since 1.3.0 def black(text) ensure_clean_sequence("\e[30m#{text}") diff --git a/spec/support/fixtures/colors b/spec/support/fixtures/colors deleted file mode 100755 index a6ccb22..0000000 --- a/spec/support/fixtures/colors +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -$LOAD_PATH.unshift "#{__dir__}/../../../lib" -require "dry/cli" - -module ColorsDemo - extend Dry::CLI::Registry - - class Print < Dry::CLI::Command - desc "Demonstrate all available colors" - - # rubocop:disable Metrics/AbcSize - def call - demo = <<~DEMO - # Style ############################################################### - Bold: #{bold("This is bold")} - # Foreground ########################################################## - Black: #{black("This is black")} - Red: #{red("This is red")} - Green: #{green("This is green")} - Yellow: #{yellow("This is yellow")} - Blue: #{blue("This is blue")} - Magenta: #{magenta("This is magenta")} - Cyan: #{cyan("This is cyan")} - White: #{white("This is white")} - # Background ########################################################## - Black: #{on_black("This is black")} - Red: #{on_red("This is red")} - Green: #{on_green("This is green")} - Yellow: #{on_yellow("This is yellow")} - Blue: #{on_blue("This is blue")} - Magenta: #{on_magenta("This is magenta")} - Cyan: #{on_cyan("This is cyan")} - White: #{on_white("This is white")} - # Combinations ######################################################## - Bold+Foreground: #{bold(red("This is bold red"))} - Bold+Background: #{bold(on_green("This is bold on green"))} - Bold+Foreground+Background: #{bold(red(on_green("This is bold red on green")))} - DEMO - puts demo - end - # rubocop:enable Metrics/AbcSize - end - - register "print", Print -end - -Dry.CLI(ColorsDemo).call diff --git a/spec/support/fixtures/styles b/spec/support/fixtures/styles new file mode 100755 index 0000000..638bffa --- /dev/null +++ b/spec/support/fixtures/styles @@ -0,0 +1,51 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +$LOAD_PATH.unshift "#{__dir__}/../../../lib" +require "dry/cli" + +module StylesDemo + extend Dry::CLI::Registry + + class Print < Dry::CLI::Command + desc "Demonstrate all available styles" + + # rubocop:disable Metrics/AbcSize + def call + demo = <<~DEMO + `bold` #{bold("This is bold")} + `dim` #{dim("This is dim")} + `italic` #{italic("This is italic")} + `underline` #{underline("This is underline")} + `blink` #{blink("This blinks")} + `reverse` #{reverse("This was reversed")} + `invisible` #{invisible("This is invisible")} (you can't see it, right?) + `black` #{black("This is black")} + `red` #{red("This is red")} + `green` #{green("This is green")} + `yellow` #{yellow("This is yellow")} + `blue` #{blue("This is blue")} + `magenta` #{magenta("This is magenta")} + `cyan` #{cyan("This is cyan")} + `white` #{white("This is white")} + `on_black` #{on_black("This is black")} + `on_red` #{on_red("This is red")} + `on_green` #{on_green("This is green")} + `on_yellow` #{on_yellow("This is yellow")} + `on_blue` #{on_blue("This is blue")} + `on_magenta` #{on_magenta("This is magenta")} + `on_cyan` #{on_cyan("This is cyan")} + `on_white` #{on_white("This is white")} + `bold`+`red`: #{bold(red("This is bold red"))} + `bold`+`on_green`: #{bold(on_green("This is bold on green"))} + `bold`+`red`+`on_green`: #{bold(red(on_green("This is bold red on green")))} + DEMO + puts demo + end + # rubocop:enable Metrics/AbcSize + end + + register "print", Print +end + +Dry.CLI(StylesDemo).call diff --git a/spec/unit/dry/cli/colors_spec.rb b/spec/unit/dry/cli/colors_spec.rb deleted file mode 100644 index 7b6f433..0000000 --- a/spec/unit/dry/cli/colors_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe "Colors" do - module Dummy - module_function - - include Dry::CLI::Colors - - def only_bold - bold("one") - end - - def bold_and_black - black(bold("two")) - end - end - - it "doesn't duplicate clean escape sequences" do - only_bold = Dummy.only_bold - expect(only_bold.end_with?("\e[0m")).to eq(true) - expect(only_bold.end_with?("\e[0m\e[0m")).to eq(false) - bold_and_black = Dummy.bold_and_black - expect(bold_and_black.end_with?("\e[0m")).to eq(true) - expect(bold_and_black.end_with?("\e[0m\e[0m")).to eq(false) - end -end diff --git a/spec/unit/dry/cli/styles_spec.rb b/spec/unit/dry/cli/styles_spec.rb new file mode 100644 index 0000000..7cff1d9 --- /dev/null +++ b/spec/unit/dry/cli/styles_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +RSpec.describe "Styles" do + class Dummy + include Dry::CLI::Styles + + def bold_and_black + black(bold("two")) + end + end + + it "doesn't duplicate clean escape sequences" do + bold_and_black = Dummy.new.bold_and_black + expect(bold_and_black.end_with?("\e[0m")).to eq(true) + expect(bold_and_black.end_with?("\e[0m\e[0m")).to eq(false) + end +end