kolorit 0.1.5 → 0.2.0.pre.dev

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fdbfa5dc4a917af5db6c5af7d92c29784754f17afe28e28b9e9b2183bc30812d
4
- data.tar.gz: 9c0c4f30ec7147f8b0ef26a794aca493a58eb150a050bb9f9004480efdb4bd0d
3
+ metadata.gz: f5e2ca034fd3bfb8d36f988cc12e9014fea4ae1aae98a21f5312e1752b53931e
4
+ data.tar.gz: a317b400ccd1adf91302ec9a451a6ef9b6cb5878ba6e7589f18d64c98e742cb3
5
5
  SHA512:
6
- metadata.gz: 312362814ac515dd8ab83a13531ea3fbbae6abade783a78f640b288869c9e0cdf0d36c1b9be8607328835373a1cb63d95f77081ac2c23fdf9c5c5ec8eff202f7
7
- data.tar.gz: 58e3143322813034aee0ebc4fc57d8edf29fa5048fecec0005898aa26b2b5c0661b55d827ca1b4785ee103ea852ca49ebdff88a6c776865c31352b038dfa30cc
6
+ metadata.gz: c727a260c985a1b711c2caf3f8fc289034ab9b921c75db344ff531f7aec94af5d693dda8c9c9d60bedca754fe0336671e43f208ef0adac70828c91e79eea9621
7
+ data.tar.gz: f04a0ab07f3eeae402faa39d3b58b44a14e9e95c1aed2319500fdc90da15ed5131deb99677d7387642b29ec037799b5eb9f24a9fc04f86eda51109e98adf2b90
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,102 @@
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
+ Gem work with `Ruby 2.6 >`. It's almost compatible with `colorize` gem, but some colors are not implemented yet
9
+ _(light blue, light green ...)_, and background colors.
10
+
11
+ You should call `String#red` not `String#colorize(:red)`, it takes more resources.
12
+ #### TO-DO: documentation
13
+ [some badly written docs](https://rubydoc.info/gems/kolorit/0.1.5)
14
+
15
+ GitHub repo is always updated before rubygems.
16
+ ```bash
17
+ git clone https://www.github.com/alx3dev/kolorit.git
18
+ cd kolorit
19
+ bundle install
20
+ ```
21
+
22
+ Install from rubygems:
23
+ ```bash
24
+ gem install kolorit
25
+ ```
26
+
27
+ Usage:
28
+
29
+ - Include kolorit methods, allow use on string, integer, array, hash
30
+
31
+ ```ruby
32
+ require 'kolorit'
33
+
34
+ 'this is red string'.red
35
+
36
+ 'this is bold cyan string'.bold.cyan
37
+
38
+ colorize :blue, 'params'
39
+ ```
40
+
41
+ - Inlcude kolorit module to be used where you need it
42
+
43
+ ```ruby
44
+ require 'kolorit/linux' # or 'kolorit/windows'
45
+
46
+ include Kolorit::Linux # or ::Windows
47
+
48
+ blue 'some string'
49
+ red 'red bold string'
50
+ ```
51
+
52
+ `#colorize` accept color as argument, and string as second
53
+ `#kolorize` accept string as argument and color as second
54
+
55
+ Both of this methods also accept block - Colorize block should return `:color`,
56
+ while `#kolorize` block should return string to be colored.
57
+
58
+ ```ruby
59
+ colorize(:red) do
60
+ if some_variable > 10
61
+ puts 'more then 10'
62
+ else
63
+ puts 'less then 10'.bold
64
+ end
65
+ end
66
+
67
+ @var = true
68
+ colorize(:bold) { @var.is_a?(TrueClass) ? :green : :red }
69
+ ```
70
+
71
+ ```ruby
72
+ kolorize('some_variable') do
73
+ if some_variable > 10
74
+ :green
75
+ else
76
+ :red
77
+ end
78
+ end
79
+
80
+ @var = 'SUCCESS!'
81
+ kolorize(@var) { @var.nil? ? :red : :green }
82
+ ```
83
+
84
+ You can allow methods to automatically `#puts` or `#print` colorized string:
85
+
86
+ ```ruby
87
+ # this call without arguments is same as with :puts
88
+ Kolorit.output :puts
89
+ # or
90
+ Kolorit.output :print
91
+
92
+ # everything else is same as false
93
+ Kolorit.output false
94
+ ```
95
+
96
+ Enable or disable colorization globally:
97
+
98
+ ```ruby
99
+ Kolorit.disable
100
+ # or
101
+ Kolorit.enable
102
+ ```
data/lib/kolorit/linux.rb CHANGED
@@ -1,96 +1,115 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'version' unless defined? Kolorit::VERSION
4
+
3
5
  module Kolorit
4
6
  ##
5
- # kolor codes for linux systems.
6
- # Allow use of kolor methods when included in class.
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
+ #
7
53
  # @see Kolorit
8
54
  # @see https://www.github.com/alx3dev/kolorit/README.md
9
55
  #
10
- # @todo Use `def red = kolor(int)` in ruby > 3.0
11
- #
12
56
  module Linux
13
- def red
14
- kolor(31)
15
- end
16
-
17
- def green
18
- kolor(32)
19
- end
20
-
21
- def yellow
22
- kolor(33)
23
- end
24
-
25
- def blue
26
- kolor(34)
27
- end
28
-
29
- def pink
30
- kolor(35)
31
- end
57
+ def red(str = nil); kolor(31, str) end
58
+ def green(str = nil); kolor(32, str) end
59
+ def yellow(str = nil); kolor(33, str) end
60
+ def blue(str = nil); kolor(34, str) end
61
+ def pink(str = nil); kolor(35, str) end
62
+ def cyan(str = nil); kolor(36, str) end
63
+ def gray(str = nil); kolor(37, str) end
32
64
 
33
- def cyan
34
- kolor(36)
35
- end
36
-
37
- def gray
38
- kolor(37)
39
- end
40
-
41
- def bold
42
- kolor(1)
43
- end
65
+ def bold(str = nil); kolor(1, str) end
66
+ def italic(str = nil); kolor(3, str) end
67
+ def underline(str = nil); kolor(4, str) end
68
+ def blink(str = nil); kolor(5, str) end
69
+ def reverse_color(str = nil); kolor(7, str) end
44
70
 
45
- def italic
46
- kolor(3)
47
- end
71
+ alias inverse reverse_color
48
72
 
49
- def underline
50
- kolor(4)
73
+ def colorize(color, string = nil, &blk)
74
+ string = yield(blk) if block_given?
75
+ kolor color, string
51
76
  end
52
77
 
53
- def blink
54
- kolor(5)
78
+ def kolorize(string, color = nil, &blk)
79
+ color = block_given? ? yield(blk) : :cyan
80
+ kolor color, string
55
81
  end
56
82
 
57
- def reverse_color
58
- kolor(7)
59
- end
60
- alias inverse reverse_color
83
+ private
61
84
 
62
- # @depreceated Use direct color methods insted #red, #blue ...
63
- # this is part of code that make rubocop sad
64
- def colorize(clr)
65
- case clr.to_sym
66
- when :red then red
67
- when :green then green
68
- when :yellow then yellow
69
- when :blue then blue
70
- when :pink then pink
71
- when :cyan then cyan
72
- when :gray then gray
73
- when :bold then bold
74
- when :italic then italic
75
- when :underline then underline
76
- when :blink then blink
77
- when :reverse_color, :inverse then reverse_color
78
- else self
79
- end
80
- end
85
+ def kolor(color, param = nil)
86
+ param = self if param.nil?
87
+ return param unless Kolorit.enabled?
81
88
 
82
- private
89
+ color = Kolors[color.to_sym] unless color.is_a?(Integer)
90
+ style =
91
+ case color
92
+ when 1 then 22 # bold
93
+ when 3 then 23 # italic
94
+ when 4 then 24 # underline
95
+ when 5 then 25 # blink
96
+ when 7 then 27 # reverse_kolor
97
+ else 0
98
+ end
83
99
 
84
- def kolor(color_code)
85
- style = case color_code
86
- when 1 then 22 # bold
87
- when 3 then 23 # italic
88
- when 4 then 24 # underline
89
- when 5 then 25 # blink
90
- when 7 then 27 # reverse_kolor
91
- else 0
92
- end
93
- "\e[#{color_code}m#{self}\e[#{style}m"
100
+ string = "\e[#{color}m#{param}\e[#{style}m"
101
+ case Kolorit.output?
102
+ when :puts then puts string
103
+ when :print then print string
104
+ end unless (1..7).include? color
105
+ string
94
106
  end
95
107
  end
108
+ ##
109
+ # Lookup for color-code by name.
110
+ #
111
+ Kolors = { red: 31, green: 32, yellow: 33,
112
+ blue: 34, pink: 35, cyan: 36,
113
+ gray: 37, bold: 1, italic: 3,
114
+ underline: 4, blink: 5, inverse: 7 }.freeze
96
115
  end
@@ -2,5 +2,49 @@
2
2
 
3
3
  module Kolorit
4
4
  # gem version
5
- VERSION = '0.1.5'
5
+ VERSION = '0.2.0-dev'
6
+
7
+ ##
8
+ # Check if we need **win32console** gem.
9
+ # Defined here because we need it in _.gemspec_ file.
10
+ #
11
+ # Check either to puts/print or just return colorized string
12
+ #
13
+ class << self
14
+ def win_32_console?
15
+ win? && cygwin?
16
+ end
17
+
18
+ def win?
19
+ ENV['OS'] == 'Windows_NT'
20
+ end
21
+
22
+ def cygwin?
23
+ RUBY_PLATFORM =~ /cygwin/
24
+ end
25
+
26
+ def enabled?
27
+ @enable != false
28
+ end
29
+
30
+ def enable(param = true)
31
+ @enable = param.is_a? TrueClass
32
+ end
33
+ alias enable= enable
34
+
35
+ def disable
36
+ enable false
37
+ end
38
+
39
+ def output?
40
+ @output
41
+ end
42
+
43
+ def output(settings = :puts)
44
+ @output = settings
45
+ @output = nil if settings.is_a? FalseClass
46
+ @output = :puts if settings.is_a? TrueClass
47
+ end
48
+ alias output= output
49
+ end
6
50
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'version' unless defined? Kolorit::VERSION
4
+
5
+ module Kolorit
6
+ module Windows
7
+ begin
8
+ raise RuntimeError, 'Windows require Cygwin' unless Kolorit.win_32_console?
9
+ require 'win32console'
10
+ rescue LoadError => e
11
+ raise 'Run: $ gem install win32console' if e.message =~ /win32console/
12
+ raise e.message
13
+ end
14
+ # Windows without cygwin is not supported, so if we come this far,
15
+ # include Linux color codes.
16
+ require_relative 'linux' unless defined? Kolorit::Linux
17
+ include Kolorit::Linux
18
+ end
19
+ end
data/lib/kolorit.rb CHANGED
@@ -1,63 +1,32 @@
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
5
  ##
6
- # Handle modules with color codes and methods to colorize string.
7
- # Right module is included depending on user OS.
6
+ # Colorize terminal output without touching String class.
7
+ # Work for Linux and Mac, require `win32console` for Windows.
8
8
  #
9
- # @example (opt 1) - Require everything and override String class
10
- # require 'kolorit' => include color methods in class String
9
+ # @example Use it any way you like
10
+ # require 'kolorit'
11
11
  #
12
- # @example (opt 2) - Require what you need, include where you need it
13
- # require 'kolorit/linux' => just require, no override
14
- # include Kolorit::Linux => use color methods in your class
12
+ # puts colorize :red, 'this is red string'
15
13
  #
16
- # @example Colorize string
17
- # 'I am mr Red String'.red
18
- # 'I am mr Green String'.green
19
- # 'I am bold (fat) mis Pink String'.pink.bold
14
+ # puts blue 'this is blue string'
20
15
  #
21
- # @example Available colors
22
- # red, green, yellow, blue, pink, cyan, gray
16
+ # puts 'this is green string'.green
23
17
  #
24
- # @example Available styles
25
- # bold, italic, underline, blink, reverse_color
18
+ # puts cyan 'this is cyan string, make it bold!'.bold
19
+ #
20
+ # @see Kolorit::Linux
26
21
  #
27
22
  module Kolorit
28
- class << self
29
- def env_os_win?
30
- ENV['OS'] == 'Windows_NT'
31
- end
32
-
33
- def on_windows?
34
- if env_os_win?
35
- return false if RUBY_PLATFORM =~ /cygwin/
36
-
37
- true
38
- else
39
- false
40
- end
41
- end
42
- alias windows? on_windows?
43
- end
44
23
  end
45
24
 
46
- raise(StandardError, 'Windows not supported, yet!', []) if Kolorit.on_windows?
47
25
 
48
- klass = String
49
-
50
- if Kolorit.windows? && !defined?(Kolorit::Windows)
26
+ if Kolorit.win? && !defined?(Kolorit::Windows)
51
27
  require_relative 'kolorit/windows'
52
- klass.include Kolorit::Windows
53
-
28
+ include(Kolorit::Windows)
54
29
  elsif !defined?(Kolorit::Linux)
55
- begin
56
- require 'win32console' if Kolorit.env_os_win?
57
- rescue StandardError
58
- raise StandardError 'please run: $ gem install win32console.'
59
- ensure
60
- require_relative 'kolorit/linux'
61
- klass.include(Kolorit::Linux)
62
- end
30
+ require_relative 'kolorit/linux'
31
+ include(Kolorit::Linux)
63
32
  end
metadata CHANGED
@@ -1,28 +1,58 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kolorit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0.pre.dev
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-30 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: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '13.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '13.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.3'
13
41
  description: |
14
- Print your terminal output in different colors. Now compatible with Ruby version 2,
15
- not only 3. Linux and Mac, Windows should work with `win32console` but not tested.
16
- Early development stage, MIT license.
42
+ Colorize terminal output without touching String class.
43
+ Work for Linux and Mac, require `win32console` for Windows.
17
44
  email:
18
45
  - alx3dev@gmail.com
19
46
  executables: []
20
47
  extensions: []
21
48
  extra_rdoc_files: []
22
49
  files:
50
+ - LICENSE
51
+ - README.md
23
52
  - lib/kolorit.rb
24
53
  - lib/kolorit/linux.rb
25
54
  - lib/kolorit/version.rb
55
+ - lib/kolorit/windows.rb
26
56
  homepage: https://www.github.com/alx3dev/kolorit
27
57
  licenses:
28
58
  - MIT
@@ -31,7 +61,7 @@ metadata:
31
61
  homepage_uri: https://www.github.com/alx3dev/kolorit
32
62
  source_code_uri: https://www.github.com/alx3dev/kolorit
33
63
  changelog_uri: https://www.github.com/alx3dev/kolorit/CHANGELOG.md
34
- documentation_uri: https://rubydoc.info/gems/kolorit/0.1.5
64
+ documentation_uri: https://rubydoc.info/gems/kolorit/0.2.0.pre.dev
35
65
  license_uri: https://www.github.com/alx3dev/kolorit/LICENSE
36
66
  post_install_message:
37
67
  rdoc_options: []
@@ -42,17 +72,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
42
72
  - - ">="
43
73
  - !ruby/object:Gem::Version
44
74
  version: 2.6.0
45
- - - "<"
46
- - !ruby/object:Gem::Version
47
- version: 3.2.0
48
75
  required_rubygems_version: !ruby/object:Gem::Requirement
49
76
  requirements:
50
- - - ">="
77
+ - - ">"
51
78
  - !ruby/object:Gem::Version
52
- version: '0'
79
+ version: 1.3.1
53
80
  requirements: []
54
81
  rubygems_version: 3.3.6
55
82
  signing_key:
56
83
  specification_version: 4
57
- summary: Colorize terminal output. Linux-Mac only, Windows CLI coming soon.
84
+ summary: Colorize terminal output on Linux, Mac and Windows.
58
85
  test_files: []