-
-
Notifications
You must be signed in to change notification settings - Fork 40
Add functions to style text (colors, weight, etc) #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gustavothecoder
wants to merge
4
commits into
dry-rb:main
Choose a base branch
from
gustavothecoder:add-colors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| `stylize("This is bold").bold` #=> #{stylize("This is bold").bold} | ||
| `stylize("This is dim").dim` #=> #{stylize("This is dim").dim} | ||
| `stylize("This is italic").italic` #=> #{stylize("This is italic").italic} | ||
| `stylize("This is underline").underline` #=> #{stylize("This is underline").underline} | ||
| `stylize("This blinks").blink` #=> #{stylize("This blinks").blink} | ||
| `stylize("This was reversed").reverse` #=> #{stylize("This was reversed").reverse} | ||
| `stylize("This is invisible").invisible` #=> #{stylize("This is invisible").invisible} (you can't see it, right?) | ||
| `stylize("This is black").black` #=> #{stylize("This is black").black} | ||
| `stylize("This is red").red` #=> #{stylize("This is red").red} | ||
| `stylize("This is green").green` #=> #{stylize("This is green").green} | ||
| `stylize("This is yellow").yellow` #=> #{stylize("This is yellow").yellow} | ||
| `stylize("This is blue").blue` #=> #{stylize("This is blue").blue} | ||
| `stylize("This is magenta").magenta` #=> #{stylize("This is magenta").magenta} | ||
| `stylize("This is cyan").cyan` #=> #{stylize("This is cyan").cyan} | ||
| `stylize("This is white").white` #=> #{stylize("This is white").white} | ||
| `stylize("This is black").on_black` #=> #{stylize("This is black").on_black} | ||
| `stylize("This is red").on_red` #=> #{stylize("This is red").on_red} | ||
| `stylize("This is green").on_green` #=> #{stylize("This is green").on_green} | ||
| `stylize("This is yellow").on_yellow` #=> #{stylize("This is yellow").on_yellow} | ||
| `stylize("This is blue").on_blue` #=> #{stylize("This is blue").on_blue} | ||
| `stylize("This is magenta").on_magenta` #=> #{stylize("This is magenta").on_magenta} | ||
| `stylize("This is cyan").on_cyan` #=> #{stylize("This is cyan").on_cyan} | ||
| `stylize("This is white").on_white` #=> #{stylize("This is white").on_white} | ||
| `stylize("This is bold red").bold.red #=> #{stylize("This is bold red").bold.red} | ||
| `stylize("This is bold on green").bold.on_green` #=> #{stylize("This is bold on green").bold.on_green} | ||
| `stylize("This is bold red on green").bold.red.on_green` #=> #{stylize("This is bold red on green").bold.red.on_green} | ||
| DEMO | ||
| puts demo | ||
| end | ||
| # rubocop:enable Metrics/AbcSize | ||
| end | ||
|
|
||
| register "print", Print | ||
| end | ||
|
|
||
| Dry.CLI(StylesDemo).call | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Dry | ||
| class CLI | ||
| # Collection of functions to style text | ||
| # | ||
| # @since 1.3.0 | ||
| module Styles | ||
| RESET = 0 | ||
| BOLD = 1 | ||
| DIM = 2 | ||
| ITALIC = 3 | ||
| UNDERLINE = 4 | ||
| BLINK = 5 | ||
| REVERSE = 7 | ||
| INVISIBLE = 8 | ||
| BLACK = 30 | ||
| RED = 31 | ||
| GREEN = 32 | ||
| YELLOW = 33 | ||
| BLUE = 34 | ||
| MAGENTA = 35 | ||
| CYAN = 36 | ||
| WHITE = 37 | ||
| ON_BLACK = 40 | ||
| ON_RED = 41 | ||
| ON_GREEN = 42 | ||
| ON_YELLOW = 43 | ||
| ON_BLUE = 44 | ||
| ON_MAGENTA = 45 | ||
| ON_CYAN = 46 | ||
| ON_WHITE = 47 | ||
|
|
||
| # Returns a text that can be styled | ||
| # | ||
| # @param text [String] text to be styled | ||
| # | ||
| # @since 1.3.0 | ||
| def stylize(text) | ||
| StyledText.new(text) | ||
| end | ||
|
|
||
| # Styled text | ||
| # | ||
| # @since 1.3.0 | ||
| class StyledText | ||
| def initialize(text, escape_code = nil) | ||
| @text = text | ||
| @escape_code = escape_code | ||
| end | ||
|
|
||
| # Makes `StyledText` printable | ||
| # | ||
| # @since 1.3.0 | ||
| def to_s | ||
| text + escape_code | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def bold | ||
| chainable_update!(BOLD, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def dim | ||
| chainable_update!(DIM, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def italic | ||
| chainable_update!(ITALIC, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def underline | ||
| chainable_update!(UNDERLINE, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def blink | ||
| chainable_update!(BLINK, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def reverse | ||
| chainable_update!(REVERSE, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def invisible | ||
| chainable_update!(INVISIBLE, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def black | ||
| chainable_update!(BLACK, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def red | ||
| chainable_update!(RED, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def green | ||
| chainable_update!(GREEN, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def yellow | ||
| chainable_update!(YELLOW, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def blue | ||
| chainable_update!(BLUE, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def magenta | ||
| chainable_update!(MAGENTA, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def cyan | ||
| chainable_update!(CYAN, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def white | ||
| chainable_update!(WHITE, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def on_black | ||
| chainable_update!(ON_BLACK, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def on_red | ||
| chainable_update!(ON_RED, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def on_green | ||
| chainable_update!(ON_GREEN, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def on_yellow | ||
| chainable_update!(ON_YELLOW, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def on_blue | ||
| chainable_update!(ON_BLUE, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def on_magenta | ||
| chainable_update!(ON_MAGENTA, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def on_cyan | ||
| chainable_update!(ON_CYAN, text) | ||
| end | ||
|
|
||
| # since 1.3.0 | ||
| def on_white | ||
| chainable_update!(ON_WHITE, text) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :text, :escape_code | ||
|
|
||
| # @since 1.3.0 | ||
| # @api private | ||
| def chainable_update!(style, new_text) | ||
| StyledText.new( | ||
| select_graphic_rendition(style) + new_text, | ||
| select_graphic_rendition(RESET) | ||
| ) | ||
| end | ||
|
|
||
| # @since 1.3.0 | ||
| # @api private | ||
| def select_graphic_rendition(code) | ||
| "\e[#{code}m" | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| `stylize("This is bold").bold` #=> #{stylize("This is bold").bold} | ||
| `stylize("This is dim").dim` #=> #{stylize("This is dim").dim} | ||
| `stylize("This is italic").italic` #=> #{stylize("This is italic").italic} | ||
| `stylize("This is underline").underline` #=> #{stylize("This is underline").underline} | ||
| `stylize("This blinks").blink` #=> #{stylize("This blinks").blink} | ||
| `stylize("This was reversed").reverse` #=> #{stylize("This was reversed").reverse} | ||
| `stylize("This is invisible").invisible` #=> #{stylize("This is invisible").invisible} (you can't see it, right?) | ||
| `stylize("This is black").black` #=> #{stylize("This is black").black} | ||
| `stylize("This is red").red` #=> #{stylize("This is red").red} | ||
| `stylize("This is green").green` #=> #{stylize("This is green").green} | ||
| `stylize("This is yellow").yellow` #=> #{stylize("This is yellow").yellow} | ||
| `stylize("This is blue").blue` #=> #{stylize("This is blue").blue} | ||
| `stylize("This is magenta").magenta` #=> #{stylize("This is magenta").magenta} | ||
| `stylize("This is cyan").cyan` #=> #{stylize("This is cyan").cyan} | ||
| `stylize("This is white").white` #=> #{stylize("This is white").white} | ||
| `stylize("This is black").on_black` #=> #{stylize("This is black").on_black} | ||
| `stylize("This is red").on_red` #=> #{stylize("This is red").on_red} | ||
| `stylize("This is green").on_green` #=> #{stylize("This is green").on_green} | ||
| `stylize("This is yellow").on_yellow` #=> #{stylize("This is yellow").on_yellow} | ||
| `stylize("This is blue").on_blue` #=> #{stylize("This is blue").on_blue} | ||
| `stylize("This is magenta").on_magenta` #=> #{stylize("This is magenta").on_magenta} | ||
| `stylize("This is cyan").on_cyan` #=> #{stylize("This is cyan").on_cyan} | ||
| `stylize("This is white").on_white` #=> #{stylize("This is white").on_white} | ||
| `stylize("This is bold red").bold.red #=> #{stylize("This is bold red").bold.red} | ||
| `stylize("This is bold on green").bold.on_green` #=> #{stylize("This is bold on green").bold.on_green} | ||
| `stylize("This is bold red on green").bold.red.on_green` #=> #{stylize("This is bold red on green").bold.red.on_green} | ||
| DEMO | ||
| puts demo | ||
| end | ||
| # rubocop:enable Metrics/AbcSize | ||
| end | ||
|
|
||
| register "print", Print | ||
| end | ||
|
|
||
| Dry.CLI(StylesDemo).call |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.