kolorit 0.2.0.pre.dev → 0.2.0

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: f5e2ca034fd3bfb8d36f988cc12e9014fea4ae1aae98a21f5312e1752b53931e
4
- data.tar.gz: a317b400ccd1adf91302ec9a451a6ef9b6cb5878ba6e7589f18d64c98e742cb3
3
+ metadata.gz: bb708f14550d48ef0bc7715e54948c60310eae2b1b630c664727edec7ffd5a20
4
+ data.tar.gz: b03cb0001044f58e519b414235b49e9e6574e7af3c55f4a26dd2d67b8c768205
5
5
  SHA512:
6
- metadata.gz: c727a260c985a1b711c2caf3f8fc289034ab9b921c75db344ff531f7aec94af5d693dda8c9c9d60bedca754fe0336671e43f208ef0adac70828c91e79eea9621
7
- data.tar.gz: f04a0ab07f3eeae402faa39d3b58b44a14e9e95c1aed2319500fdc90da15ed5131deb99677d7387642b29ec037799b5eb9f24a9fc04f86eda51109e98adf2b90
6
+ metadata.gz: 7abc3838e802e519a0c1657ba29994abefb11aa331a9d147ffbea2529437e33ee9abb9f51f2be6ece8a6eed9d8dff691257504a7f733e28065ae0a52c64c05dd
7
+ data.tar.gz: e25dd52f426f1107cee3755d63001a7842692673e18f3342d18d5af342493be42f5ca0e24891d933338a0730b74b2625f25a5838a4c8a4332de5e03bd3f0b7b3
data/README.md CHANGED
@@ -5,12 +5,10 @@
5
5
  Still in early development stage, meaning windows support should come in near future.
6
6
  For now, windows 10 users should be able to use it with `win32console` gem, but it's not tested yet.
7
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.
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
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)
11
+ # How to install
14
12
 
15
13
  GitHub repo is always updated before rubygems.
16
14
  ```bash
@@ -24,23 +22,24 @@ Install from rubygems:
24
22
  gem install kolorit
25
23
  ```
26
24
 
27
- Usage:
25
+ # How to use:
28
26
 
29
27
  - Include kolorit methods, allow use on string, integer, array, hash
30
28
 
31
29
  ```ruby
32
- require 'kolorit'
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
33
32
 
33
+ require 'kolorit'
34
34
  'this is red string'.red
35
35
 
36
- 'this is bold cyan string'.bold.cyan
37
-
38
- colorize :blue, 'params'
39
36
  ```
40
37
 
41
38
  - Inlcude kolorit module to be used where you need it
42
39
 
43
40
  ```ruby
41
+ # if you include it inside another class/module, call methods with arguments
42
+
44
43
  require 'kolorit/linux' # or 'kolorit/windows'
45
44
 
46
45
  include Kolorit::Linux # or ::Windows
@@ -49,37 +48,11 @@ blue 'some string'
49
48
  red 'red bold string'
50
49
  ```
51
50
 
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
51
+ `#colorize` accept color as first argument, and string as second
52
+ `#kolorize` accept string as first argument and color as second
79
53
 
80
- @var = 'SUCCESS!'
81
- kolorize(@var) { @var.nil? ? :red : :green }
82
- ```
54
+ Both of this methods also accept block - `#colorize` block should return `:color`,
55
+ while `#kolorize` block should return `'string'` to be colorized.
83
56
 
84
57
  You can allow methods to automatically `#puts` or `#print` colorized string:
85
58
 
@@ -100,3 +73,58 @@ Kolorit.disable
100
73
  # or
101
74
  Kolorit.enable
102
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
@@ -54,29 +54,65 @@ module Kolorit
54
54
  # @see https://www.github.com/alx3dev/kolorit/README.md
55
55
  #
56
56
  module Linux
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
64
-
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
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
68
+
69
+ def blue(str = nil)
70
+ kolor(34, str)
71
+ end
72
+
73
+ def pink(str = nil)
74
+ kolor(35, str)
75
+ end
76
+
77
+ def cyan(str = nil)
78
+ kolor(36, str)
79
+ end
80
+
81
+ def gray(str = nil)
82
+ kolor(37, str)
83
+ end
84
+
85
+ def bold(str = nil)
86
+ kolor(1, str)
87
+ end
88
+
89
+ def italic(str = nil)
90
+ kolor(3, str)
91
+ end
92
+
93
+ def underline(str = nil)
94
+ kolor(4, str)
95
+ end
96
+
97
+ def blink(str = nil)
98
+ kolor(5, str)
99
+ end
100
+
101
+ def reverse_color(str = nil)
102
+ kolor(7, str)
103
+ end
70
104
 
71
105
  alias inverse reverse_color
72
106
 
73
107
  def colorize(color, string = nil, &blk)
74
108
  string = yield(blk) if block_given?
109
+ color = KOLORS[color.to_sym] unless color.is_a?(Integer)
75
110
  kolor color, string
76
111
  end
77
112
 
78
113
  def kolorize(string, color = nil, &blk)
79
- color = block_given? ? yield(blk) : :cyan
114
+ color = yield(blk) if block_given?
115
+ color = KOLORS[color.to_sym] unless color.is_a?(Integer)
80
116
  kolor color, string
81
117
  end
82
118
 
@@ -84,31 +120,34 @@ module Kolorit
84
120
 
85
121
  def kolor(color, param = nil)
86
122
  param = self if param.nil?
87
- return param unless Kolorit.enabled?
88
-
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
99
-
100
- string = "\e[#{color}m#{param}\e[#{style}m"
101
- case Kolorit.output?
123
+ string = color_style color, param
124
+ unless (1..7).include? color
125
+ case Kolorit.output?
102
126
  when :puts then puts string
103
127
  when :print then print string
104
- end unless (1..7).include? color
128
+ end
129
+ end
105
130
  string
106
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"
145
+ end
107
146
  end
108
147
  ##
109
148
  # Lookup for color-code by name.
110
149
  #
111
- Kolors = { red: 31, green: 32, yellow: 33,
150
+ KOLORS = { red: 31, green: 32, yellow: 33,
112
151
  blue: 34, pink: 35, cyan: 36,
113
152
  gray: 37, bold: 1, italic: 3,
114
153
  underline: 4, blink: 5, inverse: 7 }.freeze
@@ -1,15 +1,16 @@
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.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
- #
12
+ VERSION = '0.2.0'
13
+
13
14
  class << self
14
15
  def win_32_console?
15
16
  win? && cygwin?
@@ -27,13 +28,13 @@ module Kolorit
27
28
  @enable != false
28
29
  end
29
30
 
30
- def enable(param = true)
31
- @enable = param.is_a? TrueClass
31
+ def enable(color: true)
32
+ @enable = color.is_a? TrueClass
32
33
  end
33
34
  alias enable= enable
34
35
 
35
36
  def disable
36
- enable false
37
+ enable color: false
37
38
  end
38
39
 
39
40
  def output?
@@ -3,12 +3,17 @@
3
3
  require_relative 'version' unless defined? Kolorit::VERSION
4
4
 
5
5
  module Kolorit
6
+ ##
7
+ # Require `win32console` to work on windows 10.
8
+ #
6
9
  module Windows
7
10
  begin
8
- raise RuntimeError, 'Windows require Cygwin' unless Kolorit.win_32_console?
11
+ raise StandardError, 'Windows require Cygwin' unless Kolorit.win_32_console?
12
+
9
13
  require 'win32console'
10
14
  rescue LoadError => e
11
15
  raise 'Run: $ gem install win32console' if e.message =~ /win32console/
16
+
12
17
  raise e.message
13
18
  end
14
19
  # Windows without cygwin is not supported, so if we come this far,
data/lib/kolorit.rb CHANGED
@@ -22,7 +22,6 @@ require_relative 'kolorit/version' unless defined? Kolorit::VERSION
22
22
  module Kolorit
23
23
  end
24
24
 
25
-
26
25
  if Kolorit.win? && !defined?(Kolorit::Windows)
27
26
  require_relative 'kolorit/windows'
28
27
  include(Kolorit::Windows)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kolorit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0.pre.dev
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - alx3dev
@@ -11,36 +11,37 @@ cert_chain: []
11
11
  date: 2022-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rake
14
+ name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '13.0'
19
+ version: '2.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '13.0'
26
+ version: '2.3'
27
27
  - !ruby/object:Gem::Dependency
28
- name: bundler
28
+ name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.3'
33
+ version: '13.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '2.3'
40
+ version: '13.0'
41
41
  description: |
42
- Colorize terminal output without touching String class.
42
+ Colorize terminal output without touching core classes.
43
43
  Work for Linux and Mac, require `win32console` for Windows.
44
+ Use on any class, auto #puts or #print, with power of ruby blocks.
44
45
  email:
45
46
  - alx3dev@gmail.com
46
47
  executables: []
@@ -61,8 +62,7 @@ metadata:
61
62
  homepage_uri: https://www.github.com/alx3dev/kolorit
62
63
  source_code_uri: https://www.github.com/alx3dev/kolorit
63
64
  changelog_uri: https://www.github.com/alx3dev/kolorit/CHANGELOG.md
64
- documentation_uri: https://rubydoc.info/gems/kolorit/0.2.0.pre.dev
65
- license_uri: https://www.github.com/alx3dev/kolorit/LICENSE
65
+ documentation_uri: https://rubydoc.info/gems/kolorit/0.2.0
66
66
  post_install_message:
67
67
  rdoc_options: []
68
68
  require_paths:
@@ -74,9 +74,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
74
  version: 2.6.0
75
75
  required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  requirements:
77
- - - ">"
77
+ - - ">="
78
78
  - !ruby/object:Gem::Version
79
- version: 1.3.1
79
+ version: '0'
80
80
  requirements: []
81
81
  rubygems_version: 3.3.6
82
82
  signing_key: