ttyhue 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cb7345390820acfae134f75dc1981a94237f2f928923e69b811b054beefbdeca
4
+ data.tar.gz: e538fbdd297be620d19b00734fa45e32997d6a41908536135b040e1816ab2abe
5
+ SHA512:
6
+ metadata.gz: 172e42263db78b3eaa8ed2560a2f175868d3ef15022f95de19912302398086cd21cc62d3bb305cdfbfbfa2846d3111d62887cfaee88125c41f4bba533fcf180c
7
+ data.tar.gz: a7cf357df077c92732886b0909e7741283688592c9830c6ecb3f0331ec18d03eb11a1e6343edd779ffaa4eeea9f585dc4d7dbf050888fcd3eb773d973434db29
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-10-04
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ttyhue (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ansi (1.5.0)
10
+ builder (3.2.4)
11
+ minitest (5.16.3)
12
+ minitest-reporters (1.5.0)
13
+ ansi
14
+ builder
15
+ minitest (>= 5.0)
16
+ ruby-progressbar
17
+ rake (13.0.6)
18
+ ruby-progressbar (1.11.0)
19
+ shoulda-context (2.0.0)
20
+
21
+ PLATFORMS
22
+ x86_64-linux
23
+
24
+ DEPENDENCIES
25
+ minitest (= 5.16.3)
26
+ minitest-reporters (= 1.5.0)
27
+ rake (~> 13.0)
28
+ shoulda-context (= 2.0.0)
29
+ ttyhue!
30
+
31
+ BUNDLED WITH
32
+ 2.2.22
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2022 Dominik Sito.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # TTYHue
2
+ Ruby gem for colorizing terminal output, supporting terminal theme colors as well as more fine-grained gui colors palette.
3
+
4
+ ## Features
5
+ * no runtime dependencies
6
+ * easy to use formatting HTML-like syntax
7
+ * support for custom styles
8
+
9
+ ## Usage
10
+ Installation
11
+ ```bash
12
+ $ gem install ttyhue
13
+ ```
14
+
15
+ ```ruby
16
+ require 'ttyhue'
17
+ ```
18
+ ### Terminal theme colors
19
+
20
+ `TTYHue` supports default formatting for terminal theme colors:
21
+ ```ruby
22
+ TTYHue.c "This is {red}red{/red} and this is {blue}blue{/blue}"
23
+ ```
24
+ including color `light` options
25
+ ```ruby
26
+ TTYHue.c "This is {lred}light red{/lred} and this is {lblue}light blue{/lblue}"
27
+ ```
28
+ ... or background mode
29
+ ```ruby
30
+ TTYHue.c "This is {blred}light red background{/blred} and this is {bblue}blue background{/bblue}"
31
+ ```
32
+ All possible options can be listed by:
33
+ ```ruby
34
+ TTYHue.preview_termcolors
35
+ ```
36
+ ![screenshot](https://raw.githubusercontent.com/railis/ttyhue/master/examples/term_colors.png)
37
+
38
+ ### GUI colors
39
+
40
+ `TTYHue` also supports term color codes (0-255) directly by prefixing the corresponding code with `gui`.
41
+
42
+ ```ruby
43
+ TTYHue.c "This is {gui123}foreground{gui123} and {bgui123}background{/bgui123}"
44
+ ```
45
+ The absolute list of color codes can be previewed by typing:
46
+ ```ruby
47
+ TTYHue.preview_guicolors
48
+ ```
49
+ ![screenshot](https://raw.githubusercontent.com/railis/ttyhue/master/examples/gui_colors.png)
50
+
51
+ ### Custom styles
52
+
53
+ In order to improve readability, color codes can be wrapped into custom styles.
54
+
55
+ ```ruby
56
+ TTYHUe.c(
57
+ "{header}Title{/header}\n{content}Content{/content}\n{footer}Footer{/footer}",
58
+ header: {fg: :blue},
59
+ content: {fg: :lgray, bg: :gui234},
60
+ footer: {fg: :gui252}
61
+ )
62
+ ```
63
+
64
+ Custom styles can be set globally as well.
65
+ ```ruby
66
+ TTYHue.set_style(
67
+ header: {fg: :blue},
68
+ content: {fg: :lgray, bg: :gui234},
69
+ footer: {fg: :gui252}
70
+ )
71
+
72
+ TTYHue.c "{header}Title{/header}\n{content}Content{/content}\n{footer}Footer{/footer}"
73
+ ```
74
+ ## Contributing
75
+
76
+ ### Workflow
77
+
78
+ 1. Fork it ( https://github.com/railis/ttyhue/fork )
79
+ 2. Create your feature branch (`git checkout -b new_feature`)
80
+ 3. Commit your changes (`git commit -am 'Commit feature'`)
81
+ 4. Push to the branch (`git push origin new_feature`)
82
+ 5. Create a new Pull Request
83
+
84
+ ### Running tests
85
+
86
+ ```bash
87
+ $ bundle install
88
+ $ rake test
89
+ ```
90
+
91
+ ## Copyright
92
+
93
+ Copyright (c) 2022 Dominik Sito. See LICENSE for further details.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ task default: %i[]
5
+ Rake::TestTask.new do |task|
6
+ task.pattern = "test/ttyhue/*_test.rb"
7
+ end
Binary file
Binary file
@@ -0,0 +1,39 @@
1
+ module TTYHue
2
+ class Colorizer
3
+
4
+ class << self
5
+
6
+ def colorize(str)
7
+ Parser.new(str).parse.map do |parsed_section|
8
+ colorized_section(parsed_section)
9
+ end.join
10
+ end
11
+
12
+ private
13
+
14
+ def colorized_section(section)
15
+ "".tap do |str|
16
+ if section[:bg] == :default && section[:fg] == :default
17
+ str << section[:str]
18
+ else
19
+ str << closing_byte
20
+ str << opening_csi_for_color(section[:fg], false) if section[:fg] != :default
21
+ str << opening_csi_for_color(section[:bg], true) if section[:bg] != :default
22
+ str << section[:str]
23
+ str << closing_byte
24
+ end
25
+ end
26
+ end
27
+
28
+ def opening_csi_for_color(color, bg)
29
+ "\x1b[#{TermColor.by_name(color).term_hex(bg)}m"
30
+ end
31
+
32
+ def closing_byte
33
+ "\033[m"
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,30 @@
1
+ module TTYHue
2
+ class Parser
3
+ class ColorStack
4
+
5
+ def initialize
6
+ @stack = []
7
+ end
8
+
9
+ def push(value, opts={})
10
+ @stack.unshift({value: value, opts: opts})
11
+ end
12
+
13
+ def pop(value, opts={})
14
+ @stack.each_with_index do |v,i|
15
+ if v[:value] == value && v[:opts] == opts
16
+ @stack.delete_at(i)
17
+ break
18
+ end
19
+ end
20
+ end
21
+
22
+ def top(opts={})
23
+ @stack.select do |e|
24
+ e[:opts] == opts
25
+ end.first.to_h[:value]
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,25 @@
1
+ module TTYHue
2
+ class Parser
3
+ class ColorTag < Tag
4
+
5
+ def regexp
6
+ /{(?<closing>\/?)(?<bg>b?)(?<name>#{TermColor.defs.map(&:tag_name).join("|")})}/
7
+ end
8
+
9
+ attr_reader :color_name, :bg
10
+
11
+ def bg
12
+ @match_data[:bg] != ""
13
+ end
14
+
15
+ def closing
16
+ @match_data[:closing] != ""
17
+ end
18
+
19
+ def color_name
20
+ TermColor.by_tag(@match_data[:name]).color_name
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ module TTYHue
2
+ class Parser
3
+ class StyleTag < Tag
4
+
5
+ def regexp
6
+ /{(?<closing>\/?)(?<name>#{@styles.keys.map(&:to_s).join("|")})}/
7
+ end
8
+
9
+ def initialize(str, styles = {})
10
+ @styles = styles
11
+ super(str)
12
+ end
13
+
14
+ def fg_color_name
15
+ TermColor.by_tag(styles_hash[:fg].to_s)&.color_name
16
+ end
17
+
18
+ def bg_color_name
19
+ TermColor.by_tag(styles_hash[:bg].to_s)&.color_name
20
+ end
21
+
22
+ private
23
+
24
+ def styles_hash
25
+ @styles[@match_data[:name].to_sym]
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,24 @@
1
+ module TTYHue
2
+ class Parser
3
+ class Tag
4
+
5
+ def initialize(str)
6
+ @str = str
7
+ @match_data = str.match(regexp)
8
+ return unless @match_data
9
+ end
10
+
11
+ def valid?
12
+ !!@match_data
13
+ end
14
+
15
+ def closing
16
+ @match_data[:closing] != ""
17
+ end
18
+
19
+ end
20
+ end
21
+ end
22
+
23
+ require_relative "color_tag"
24
+ require_relative "style_tag"
@@ -0,0 +1,82 @@
1
+ require 'strscan'
2
+ require_relative "parser/tag"
3
+ require_relative "parser/color_stack"
4
+
5
+ module TTYHue
6
+ class Parser
7
+
8
+ TOKENS = {
9
+ tag: /{[^{}]+}/,
10
+ content: /[^{]+/
11
+ }
12
+
13
+ class ParseError < StandardError; end
14
+
15
+ def initialize(string, styles = {})
16
+ @string = string
17
+ @styles = TTYHue.global_style.merge(styles)
18
+ end
19
+
20
+ def parse
21
+ @color_stack = ColorStack.new
22
+ @result = []
23
+ scanner = StringScanner.new(@string)
24
+ until scanner.eos?
25
+ next if handle_content!(scanner.scan(TOKENS[:content]))
26
+ next if handle_tag!(scanner.scan(TOKENS[:tag]))
27
+ end
28
+ flattened_result
29
+ end
30
+
31
+ private
32
+
33
+ def flattened_result
34
+ [].tap do |res|
35
+ @result.each do |e|
36
+ last = res.last
37
+ if last && last[:fg] == e[:fg] && last[:bg] == e[:bg]
38
+ last[:str] += e[:str]
39
+ else
40
+ res << e
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ def handle_content!(str)
47
+ return false unless str
48
+
49
+ @result << {
50
+ fg: @color_stack.top(bg: false) || :default,
51
+ bg: @color_stack.top(bg: true) || :default,
52
+ str: str
53
+ }
54
+ end
55
+
56
+ def handle_tag!(str)
57
+ return false unless str
58
+
59
+ color_tag = ColorTag.new(str)
60
+ style_tag = StyleTag.new(str, @styles)
61
+
62
+ if color_tag.valid?
63
+ if color_tag.closing
64
+ @color_stack.pop(color_tag.color_name, bg: color_tag.bg)
65
+ else
66
+ @color_stack.push(color_tag.color_name, bg: color_tag.bg)
67
+ end
68
+ elsif style_tag.valid?
69
+ if style_tag.closing
70
+ @color_stack.pop(style_tag.fg_color_name, bg: false) if style_tag.fg_color_name
71
+ @color_stack.pop(style_tag.bg_color_name, bg: true) if style_tag.bg_color_name
72
+ else
73
+ @color_stack.push(style_tag.fg_color_name, bg: false) if style_tag.fg_color_name
74
+ @color_stack.push(style_tag.bg_color_name, bg: true) if style_tag.bg_color_name
75
+ end
76
+ else
77
+ handle_content!(str)
78
+ end
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,32 @@
1
+ module TTYHue
2
+ class Preview
3
+
4
+ class << self
5
+
6
+ def preview_termcolors
7
+ %w{black red green yellow blue magenta cyan white}.each do |c|
8
+ ['', 'b'].each do |bg|
9
+ ['', 'l'].each do |l|
10
+ tag_name = "#{bg}#{l}#{c}"
11
+ print TTYHue.c(" {#{tag_name}} #{tag_name.ljust(12)}{/#{tag_name}}")
12
+ end
13
+ end
14
+ print "\n"
15
+ end
16
+ nil
17
+ end
18
+
19
+ def preview_guicolors
20
+ (1..256).each do |x|
21
+ num = x - 1
22
+ tag_name = "gui#{num}"
23
+ print TTYHue.c(" {#{tag_name}}#{num.to_s.ljust(3)}{/#{tag_name}}")
24
+ print "\n" if x % 16 == 0
25
+ end
26
+ nil
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,83 @@
1
+ module TTYHue
2
+ class TermColor
3
+
4
+ LABELS = %w{black red green yellow blue magenta cyan white}
5
+
6
+ def self.defs
7
+ [].tap do |res|
8
+ [false, true].each do |light|
9
+ LABELS.each do |label|
10
+ res << Theme.new(label, light)
11
+ end
12
+ end
13
+ (0..255).each do |code|
14
+ res << Gui.new(code)
15
+ end
16
+ end
17
+ end
18
+
19
+ def self.by_tag(tag_name)
20
+ defs.select {|d| d.tag_name == tag_name}.first
21
+ end
22
+
23
+ def self.by_name(name)
24
+ defs.select {|d| d.color_name == name}.first
25
+ end
26
+
27
+ class Base
28
+
29
+ def inspect
30
+ "#<TermColor @name=#{color_name} @tag=#{tag_name}>"
31
+ end
32
+
33
+ end
34
+
35
+ class Theme < Base
36
+
37
+ def initialize(label, light)
38
+ @label = label
39
+ @light = light
40
+ end
41
+
42
+ def color_name
43
+ [('light' if @light), @label].compact.join("_").to_sym
44
+ end
45
+
46
+ def tag_name
47
+ [('l' if @light), @label].compact.join
48
+ end
49
+
50
+ def term_hex(bg)
51
+ color_index = TermColor::LABELS.index(@label)
52
+ if @light
53
+ bg ? "10#{color_index}" : "9#{color_index}"
54
+ else
55
+ bg ? "4#{color_index}" : "3#{color_index}"
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ class Gui < Base
62
+
63
+ def initialize(code)
64
+ @code = code
65
+ end
66
+
67
+ def color_name
68
+ "gui_#{@code}".to_sym
69
+ end
70
+
71
+ def tag_name
72
+ "gui#{@code.to_s}"
73
+ end
74
+
75
+ def term_hex(bg)
76
+ prefix = bg ? "48;5;" : "38;5;"
77
+ prefix + @code.to_s
78
+ end
79
+
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ module TTYHue
2
+ VERSION = "0.1.0"
3
+ end
data/lib/ttyhue.rb ADDED
@@ -0,0 +1,32 @@
1
+ require_relative "ttyhue/term_color"
2
+ require_relative "ttyhue/parser"
3
+ require_relative "ttyhue/colorizer"
4
+ require_relative "ttyhue/preview"
5
+
6
+ module TTYHue
7
+
8
+ def self.global_style
9
+ begin
10
+ @@styles.to_h
11
+ rescue NameError
12
+ {}
13
+ end
14
+ end
15
+
16
+ def self.set_style(hash)
17
+ @@styles = hash.to_h
18
+ end
19
+
20
+ def self.c(str)
21
+ TTYHue::Colorizer.colorize(str)
22
+ end
23
+
24
+ def self.preview_termcolors
25
+ Preview.preview_termcolors
26
+ end
27
+
28
+ def self.preview_guicolors
29
+ Preview.preview_guicolors
30
+ end
31
+
32
+ end
data/ttyhue.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ require_relative "lib/ttyhue/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "ttyhue"
5
+ spec.version = TTYHue::VERSION
6
+ spec.authors = ["Dominik Sito"]
7
+ spec.email = ["dominik.sito@gmail.com"]
8
+
9
+ spec.summary = "Ruby gem for colorizing terminal output, supporting terminal theme colors as well as more fine-grained gui colors palette."
10
+ spec.description = "Ruby gem for colorizing terminal output, supporting terminal theme colors as well as more fine-grained gui colors palette."
11
+ spec.homepage = "https://github.com/railis/ttyhue"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = ">= 2.4.0"
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/railis/ttyhue"
17
+ spec.metadata["changelog_uri"] = "https://raw.githubusercontent.com/railis/ttyhue/master/CHANGELOG.md"
18
+
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
21
+ end
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "minitest", "5.16.3"
25
+ spec.add_development_dependency "minitest-reporters", "1.5.0"
26
+ spec.add_development_dependency "shoulda-context", "2.0.0"
27
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ttyhue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dominik Sito
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-10-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 5.16.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 5.16.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest-reporters
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.5.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.5.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: shoulda-context
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 2.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 2.0.0
55
+ description: Ruby gem for colorizing terminal output, supporting terminal theme colors
56
+ as well as more fine-grained gui colors palette.
57
+ email:
58
+ - dominik.sito@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - CHANGELOG.md
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - examples/gui_colors.png
70
+ - examples/term_colors.png
71
+ - lib/ttyhue.rb
72
+ - lib/ttyhue/colorizer.rb
73
+ - lib/ttyhue/parser.rb
74
+ - lib/ttyhue/parser/color_stack.rb
75
+ - lib/ttyhue/parser/color_tag.rb
76
+ - lib/ttyhue/parser/style_tag.rb
77
+ - lib/ttyhue/parser/tag.rb
78
+ - lib/ttyhue/preview.rb
79
+ - lib/ttyhue/term_color.rb
80
+ - lib/ttyhue/version.rb
81
+ - ttyhue.gemspec
82
+ homepage: https://github.com/railis/ttyhue
83
+ licenses:
84
+ - MIT
85
+ metadata:
86
+ homepage_uri: https://github.com/railis/ttyhue
87
+ source_code_uri: https://github.com/railis/ttyhue
88
+ changelog_uri: https://raw.githubusercontent.com/railis/ttyhue/master/CHANGELOG.md
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: 2.4.0
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubygems_version: 3.3.22
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Ruby gem for colorizing terminal output, supporting terminal theme colors
108
+ as well as more fine-grained gui colors palette.
109
+ test_files: []