kolorit 0.1.3 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ef040c50bebe6a3d79fcaee8de83f9bd97233b9e432d6a232db7137beff88643
4
- data.tar.gz: c262eabd58497efbfa76e72d918c1b2a6445c2a7dec772a0cbd67c97722d5b86
3
+ metadata.gz: bb708f14550d48ef0bc7715e54948c60310eae2b1b630c664727edec7ffd5a20
4
+ data.tar.gz: b03cb0001044f58e519b414235b49e9e6574e7af3c55f4a26dd2d67b8c768205
5
5
  SHA512:
6
- metadata.gz: 0dac1ce53413bc676b2d03a0d3be3b8b27129ab02f2137dca9112e9ded142d184c320912b7560fd59b377564d3edcf95131ae3b6e372e6b001a3f2b493d45806
7
- data.tar.gz: 63cd157d5400ac32f2eeca5e0b7af678175f5f3e94dc464175b8269ed1ecde08d9632ec52e5023e89e2096005dad35b05c6a62a92baf2d2455445595d5758443
6
+ metadata.gz: 7abc3838e802e519a0c1657ba29994abefb11aa331a9d147ffbea2529437e33ee9abb9f51f2be6ece8a6eed9d8dff691257504a7f733e28065ae0a52c64c05dd
7
+ data.tar.gz: e25dd52f426f1107cee3755d63001a7842692673e18f3342d18d5af342493be42f5ca0e24891d933338a0730b74b2625f25a5838a4c8a4332de5e03bd3f0b7b3
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Alex3Dev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,130 @@
1
+ # Kolorit
2
+
3
+ ![GIF Preview](demo.gif)
4
+
5
+ Still in early development stage, meaning windows support should come in near future.
6
+ For now, windows 10 users should be able to use it with `win32console` gem, but it's not tested yet.
7
+
8
+ Kolorit allow you to easily colorize any string, integer, array, hash, without touching core classes.
9
+ Use methods `#colorize` and `#kolorize` for power of Ruby blocks.
10
+
11
+ # How to install
12
+
13
+ GitHub repo is always updated before rubygems.
14
+ ```bash
15
+ git clone https://www.github.com/alx3dev/kolorit.git
16
+ cd kolorit
17
+ bundle install
18
+ ```
19
+
20
+ Install from rubygems:
21
+ ```bash
22
+ gem install kolorit
23
+ ```
24
+
25
+ # How to use:
26
+
27
+ - Include kolorit methods, allow use on string, integer, array, hash
28
+
29
+ ```ruby
30
+ # you can call method on string only if you include module outside of main namespace.
31
+ # that's what happen when you require kolorit
32
+
33
+ require 'kolorit'
34
+ 'this is red string'.red
35
+
36
+ ```
37
+
38
+ - Inlcude kolorit module to be used where you need it
39
+
40
+ ```ruby
41
+ # if you include it inside another class/module, call methods with arguments
42
+
43
+ require 'kolorit/linux' # or 'kolorit/windows'
44
+
45
+ include Kolorit::Linux # or ::Windows
46
+
47
+ blue 'some string'
48
+ red 'red bold string'
49
+ ```
50
+
51
+ `#colorize` accept color as first argument, and string as second
52
+ `#kolorize` accept string as first argument and color as second
53
+
54
+ Both of this methods also accept block - `#colorize` block should return `:color`,
55
+ while `#kolorize` block should return `'string'` to be colorized.
56
+
57
+ You can allow methods to automatically `#puts` or `#print` colorized string:
58
+
59
+ ```ruby
60
+ # this call without arguments is same as with :puts
61
+ Kolorit.output :puts
62
+ # or
63
+ Kolorit.output :print
64
+
65
+ # everything else is same as false
66
+ Kolorit.output false
67
+ ```
68
+
69
+ Enable or disable colorization globally:
70
+
71
+ ```ruby
72
+ Kolorit.disable
73
+ # or
74
+ Kolorit.enable
75
+ ```
76
+
77
+ > Examples are better then documentation, so until I write docs...
78
+
79
+ **[example 1]**
80
+ ```ruby
81
+ colorize(:green) do
82
+ if RUBY_VERSION.start_with?('3')
83
+ 'Happy Coding!'.bold
84
+ else
85
+ 'You should try Ruby 3, it is much better!'
86
+ end
87
+ end
88
+ ```
89
+
90
+ **[example 2]**
91
+ ```ruby
92
+ def color_line(param)
93
+
94
+ # check if kolorit is enabled (default: true)
95
+ status = Kolorit.enabled?
96
+
97
+ # enable kolorit if disabled
98
+ Kolorit.enable
99
+
100
+ # colorize param based on it's class
101
+ colorize(:cyan) do
102
+ case param.class.name
103
+ when 'String'
104
+ :green
105
+ when 'Integer'
106
+ :yellow
107
+ else
108
+ :gray
109
+ end
110
+ end
111
+
112
+ # keep module status same as before method action
113
+ Kolorit.disable unless status
114
+ end
115
+ ```
116
+
117
+ **[example 3]**
118
+ ```ruby
119
+ # enable automated #puts or #print
120
+ Kolorit.output :puts
121
+
122
+ # define message to print
123
+ message = 'Thank you Matz, for Amazing Ruby '
124
+
125
+ # print bold cyan message based on block evaluation
126
+ colorize(:bold) { RUBY_VERSION[0] == '3' ? message + '3' : message + 'language'}.cyan
127
+
128
+ # disable output
129
+ Kolorit.output false
130
+ ```
data/lib/kolorit/linux.rb CHANGED
@@ -1,52 +1,154 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'version' unless defined? Kolorit::VERSION
4
+
3
5
  module Kolorit
6
+ ##
7
+ # Color codes for Linux systems.
8
+ # Allow use of color methods in many different ways.
9
+ #
10
+ # Include methods outside of main module to use them directly
11
+ # on string (or integer, array, hash). This is done when you call
12
+ # `require 'kolorit'`. You can also just include it in class
13
+ # where you want to use it:
14
+ #
15
+ # @example Include Kolorit::Linux in just one class
16
+ #
17
+ # class MyClass
18
+ # require 'kolorit/linux'
19
+ # include Kolorit::Linux
20
+ # # rest_of_code
21
+ # end
22
+ #
23
+ # @example Use color named methods
24
+ #
25
+ # red 'Hello Red Ruby!'
26
+ # # or
27
+ # green 'Hello Green Ruby!'
28
+ #
29
+ # @example Use colorize/kolorize methods
30
+ #
31
+ # colorize :red, 'Hello Red Ruby!'
32
+ # # or
33
+ # kolorize 'Ruby goes green, again!', :green
34
+ #
35
+ # @example Colorize also accept block
36
+ #
37
+ # # colorize accept color as first argument
38
+ #
39
+ # colorize(:green) do
40
+ # case @var = SomeClass.call_some_method
41
+ # when 'some response'
42
+ # "do_something_with_response #{@var}"
43
+ # else
44
+ # # in this situation #red has precedence over #green
45
+ # red("Returned Error for #{@var}")
46
+ # end
47
+ # end
48
+ #
49
+ # # kolorize accept string as first argument
50
+ #
51
+ # kolorize(@result) { @result.is_a?(String) ? :green : :red }
52
+ #
53
+ # @see Kolorit
54
+ # @see https://www.github.com/alx3dev/kolorit/README.md
55
+ #
4
56
  module Linux
5
- def black = colorize(30)
57
+ def red(str = nil)
58
+ kolor(31, str)
59
+ end
60
+
61
+ def green(str = nil)
62
+ kolor(32, str)
63
+ end
64
+
65
+ def yellow(str = nil)
66
+ kolor(33, str)
67
+ end
6
68
 
7
- def red = colorize(31)
69
+ def blue(str = nil)
70
+ kolor(34, str)
71
+ end
8
72
 
9
- def green = colorize(32)
73
+ def pink(str = nil)
74
+ kolor(35, str)
75
+ end
10
76
 
11
- def yellow = colorize(33)
77
+ def cyan(str = nil)
78
+ kolor(36, str)
79
+ end
12
80
 
13
- def blue = colorize(34)
81
+ def gray(str = nil)
82
+ kolor(37, str)
83
+ end
14
84
 
15
- def pink = colorize(35)
85
+ def bold(str = nil)
86
+ kolor(1, str)
87
+ end
16
88
 
17
- def cyan = colorize(36)
89
+ def italic(str = nil)
90
+ kolor(3, str)
91
+ end
18
92
 
19
- def gray = colorize(37)
93
+ def underline(str = nil)
94
+ kolor(4, str)
95
+ end
20
96
 
21
- def bold = colorize(1)
97
+ def blink(str = nil)
98
+ kolor(5, str)
99
+ end
22
100
 
23
- def italic = colorize(3)
101
+ def reverse_color(str = nil)
102
+ kolor(7, str)
103
+ end
24
104
 
25
- def underline = colorize(4)
105
+ alias inverse reverse_color
26
106
 
27
- def blink = colorize(5)
107
+ def colorize(color, string = nil, &blk)
108
+ string = yield(blk) if block_given?
109
+ color = KOLORS[color.to_sym] unless color.is_a?(Integer)
110
+ kolor color, string
111
+ end
28
112
 
29
- def reverse_color = colorize(7)
113
+ def kolorize(string, color = nil, &blk)
114
+ color = yield(blk) if block_given?
115
+ color = KOLORS[color.to_sym] unless color.is_a?(Integer)
116
+ kolor color, string
117
+ end
30
118
 
31
119
  private
32
120
 
33
- ##
34
- # colorize string based on color_code
35
- #
36
- def colorize(color_code)
37
- # check if we change color or type
38
- type = case color_code
39
- # for type-change
40
- when 1 then 22 # bold
41
- when 3 then 23 # italic
42
- when 4 then 24 # underline
43
- when 5 then 25 # blink
44
- when 7 then 27 # reverse_color
45
- # for color change
46
- else 0
47
- end
48
- # create colorized string
49
- "\e[#{color_code}m#{self}\e[#{type}m"
121
+ def kolor(color, param = nil)
122
+ param = self if param.nil?
123
+ string = color_style color, param
124
+ unless (1..7).include? color
125
+ case Kolorit.output?
126
+ when :puts then puts string
127
+ when :print then print string
128
+ end
129
+ end
130
+ string
131
+ end
132
+
133
+ def color_style(color, param)
134
+ return param unless Kolorit.enabled?
135
+
136
+ style = case color
137
+ when 1 then 22 # bold
138
+ when 3 then 23 # italic
139
+ when 4 then 24 # underline
140
+ when 5 then 25 # blink
141
+ when 7 then 27 # reverse_kolor
142
+ else 0
143
+ end
144
+ "\e[#{color}m#{param}\e[#{style}m"
50
145
  end
51
146
  end
147
+ ##
148
+ # Lookup for color-code by name.
149
+ #
150
+ KOLORS = { red: 31, green: 32, yellow: 33,
151
+ blue: 34, pink: 35, cyan: 36,
152
+ gray: 37, bold: 1, italic: 3,
153
+ underline: 4, blink: 5, inverse: 7 }.freeze
52
154
  end
@@ -1,6 +1,51 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ #
4
+ # Check if we need **win32console** gem.
5
+ # Defined here because we need it in _.gemspec_ file.
6
+ #
7
+ # Check either to puts/print or just return colorized string
8
+ # Disable/Enable globally
9
+ #
3
10
  module Kolorit
4
11
  # gem version
5
- VERSION = '0.1.3'
12
+ VERSION = '0.2.0'
13
+
14
+ class << self
15
+ def win_32_console?
16
+ win? && cygwin?
17
+ end
18
+
19
+ def win?
20
+ ENV['OS'] == 'Windows_NT'
21
+ end
22
+
23
+ def cygwin?
24
+ RUBY_PLATFORM =~ /cygwin/
25
+ end
26
+
27
+ def enabled?
28
+ @enable != false
29
+ end
30
+
31
+ def enable(color: true)
32
+ @enable = color.is_a? TrueClass
33
+ end
34
+ alias enable= enable
35
+
36
+ def disable
37
+ enable color: false
38
+ end
39
+
40
+ def output?
41
+ @output
42
+ end
43
+
44
+ def output(settings = :puts)
45
+ @output = settings
46
+ @output = nil if settings.is_a? FalseClass
47
+ @output = :puts if settings.is_a? TrueClass
48
+ end
49
+ alias output= output
50
+ end
6
51
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'version' unless defined? Kolorit::VERSION
4
+
5
+ module Kolorit
6
+ ##
7
+ # Require `win32console` to work on windows 10.
8
+ #
9
+ module Windows
10
+ begin
11
+ raise StandardError, 'Windows require Cygwin' unless Kolorit.win_32_console?
12
+
13
+ require 'win32console'
14
+ rescue LoadError => e
15
+ raise 'Run: $ gem install win32console' if e.message =~ /win32console/
16
+
17
+ raise e.message
18
+ end
19
+ # Windows without cygwin is not supported, so if we come this far,
20
+ # include Linux color codes.
21
+ require_relative 'linux' unless defined? Kolorit::Linux
22
+ include Kolorit::Linux
23
+ end
24
+ end
data/lib/kolorit.rb CHANGED
@@ -1,19 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'kolorit/version'
3
+ require_relative 'kolorit/version' unless defined? Kolorit::VERSION
4
4
 
5
+ ##
6
+ # Colorize terminal output without touching String class.
7
+ # Work for Linux and Mac, require `win32console` for Windows.
8
+ #
9
+ # @example Use it any way you like
10
+ # require 'kolorit'
11
+ #
12
+ # puts colorize :red, 'this is red string'
13
+ #
14
+ # puts blue 'this is blue string'
15
+ #
16
+ # puts 'this is green string'.green
17
+ #
18
+ # puts cyan 'this is cyan string, make it bold!'.bold
19
+ #
20
+ # @see Kolorit::Linux
21
+ #
5
22
  module Kolorit
6
23
  end
7
24
 
8
- if ENV['OS'] == 'Windows_NT'
9
- # require_relative 'kolorit/windows'
10
- class String
11
- # working on windows color codes
12
- # include Kolorit::Windows unless RUBY_PLATFORM =~ /cygwin/
13
- end
14
- else
25
+ if Kolorit.win? && !defined?(Kolorit::Windows)
26
+ require_relative 'kolorit/windows'
27
+ include(Kolorit::Windows)
28
+ elsif !defined?(Kolorit::Linux)
15
29
  require_relative 'kolorit/linux'
16
- class String
17
- include Kolorit::Linux
18
- end
30
+ include(Kolorit::Linux)
19
31
  end
metadata CHANGED
@@ -1,27 +1,59 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kolorit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - alx3dev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-25 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2022-02-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
13
41
  description: |
14
- Print your terminal output in different colors. Early development stage,
15
- for now only Linux and Mac. MIT license.'
42
+ Colorize terminal output without touching core classes.
43
+ Work for Linux and Mac, require `win32console` for Windows.
44
+ Use on any class, auto #puts or #print, with power of ruby blocks.
16
45
  email:
17
46
  - alx3dev@gmail.com
18
47
  executables: []
19
48
  extensions: []
20
49
  extra_rdoc_files: []
21
50
  files:
51
+ - LICENSE
52
+ - README.md
22
53
  - lib/kolorit.rb
23
54
  - lib/kolorit/linux.rb
24
55
  - lib/kolorit/version.rb
56
+ - lib/kolorit/windows.rb
25
57
  homepage: https://www.github.com/alx3dev/kolorit
26
58
  licenses:
27
59
  - MIT
@@ -30,7 +62,7 @@ metadata:
30
62
  homepage_uri: https://www.github.com/alx3dev/kolorit
31
63
  source_code_uri: https://www.github.com/alx3dev/kolorit
32
64
  changelog_uri: https://www.github.com/alx3dev/kolorit/CHANGELOG.md
33
- documentation_uri: https://rubydoc.info/gems/kolorit
65
+ documentation_uri: https://rubydoc.info/gems/kolorit/0.2.0
34
66
  post_install_message:
35
67
  rdoc_options: []
36
68
  require_paths:
@@ -40,17 +72,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
40
72
  - - ">="
41
73
  - !ruby/object:Gem::Version
42
74
  version: 2.6.0
43
- - - "<"
44
- - !ruby/object:Gem::Version
45
- version: 4.0.0
46
75
  required_rubygems_version: !ruby/object:Gem::Requirement
47
76
  requirements:
48
77
  - - ">="
49
78
  - !ruby/object:Gem::Version
50
79
  version: '0'
51
80
  requirements: []
52
- rubygems_version: 3.2.32
81
+ rubygems_version: 3.3.6
53
82
  signing_key:
54
83
  specification_version: 4
55
- summary: Colorize terminal output. Linux-Mac only, Windows CLI coming soon.
84
+ summary: Colorize terminal output on Linux, Mac and Windows.
56
85
  test_files: []