ansi_text_styles 1.0.0 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aed6d3739fa4c771809a72dec79d896a93dd405c
4
- data.tar.gz: 5665e88349cc2b1ad2e372faea083034fa10e49e
3
+ metadata.gz: ce57a08a3632e003a946d04b3356266a149a501c
4
+ data.tar.gz: 2efe703e32a6aa8f9637574bbed7003e05ad702e
5
5
  SHA512:
6
- metadata.gz: 110e80f80b2d9d54b9521ad8d2d4e762b9ba3a943eb820dc8368c1e75e4b51817e5035c91e1029d1e259a019732e753da77346a9926d97ae0d41b07ba11166fc
7
- data.tar.gz: 925fe64c3795c2aea5f4f5bbbf7e82a2e8bb950548e0430a19bf5ebd03104ead8b2c56ba777b1ae81f7969ea5b6389276cbaaff2fce988093be5a42ac8a0d2a6
6
+ metadata.gz: 3a89e30934e52f48c3556f6b77b6c8b89b8218016f503d05b9e7b05b88284eca089b1375b49ef2feebecc86c32c32845ba57c9b72aa53e65189dee4501f576a4
7
+ data.tar.gz: 3cdac58a6cedadc16859b9cc7acc3bb134bc550058b967faf59da58950c8489ab4e1ff17e927fa457e5e522238dfde86c4d1c98d6e0b398bcfebd7a8f53ca740
@@ -0,0 +1,43 @@
1
+ require 'ansi_text_styles'
2
+
3
+ using AnsiTextStyles::Refinement
4
+
5
+ puts
6
+ puts "Standard and high intensity colours"
7
+ puts
8
+
9
+ # Standard and high intensity colours
10
+ (0..15).each do |fg|
11
+ row = (0..15).collect do |bg|
12
+ 'xxx'.center(5).style(fg: fg, bg: bg)
13
+ end
14
+ puts row.join
15
+ end
16
+
17
+ puts
18
+ puts "216 colours"
19
+ puts
20
+
21
+ # 216 colours
22
+ colours = (16..231).collect do |bg|
23
+ fg = (bg - 16) % 36 < 18 ? 15 : 0
24
+ bg.to_s.center(5).style(fg: fg, bg: bg)
25
+ end
26
+
27
+ colours.each_slice(36).collect do |row|
28
+ puts row.join
29
+ end
30
+
31
+ puts
32
+ puts "Greyscale colours"
33
+ puts
34
+
35
+ # Greyscale colours
36
+ colours = (232..255).collect do |bg|
37
+ fg = (bg - 16) % 24 < 12 ? 15 : 0
38
+ bg.to_s.center(5).style(fg: fg, bg: bg)
39
+ end
40
+
41
+ colours.each_slice(12) do |row|
42
+ puts row.join
43
+ end
@@ -0,0 +1,6 @@
1
+ require 'ansi_text_styles'
2
+
3
+ using AnsiTextStyles
4
+
5
+ puts "How are you?".style(:blue, :bold) + " " + 'I am good!'.style(:red, :bold)
6
+ puts '%s %s' % ["How are you?".style(:blue, :bold), 'I am good!'.style(:red, :bold)]
@@ -0,0 +1,6 @@
1
+ require 'ansi_text_styles'
2
+
3
+ using AnsiTextStyles
4
+
5
+ puts 'How are you?'.blue.bold + " " + 'I am good!'.red.bold
6
+ puts '%s %s' % ["How are you?".blue.bold, 'I am good!'.red.bold]
@@ -0,0 +1,24 @@
1
+ require 'ansi_text_styles'
2
+
3
+ class ColourLogger
4
+ using AnsiTextStyles
5
+
6
+ def self.status(text)
7
+ "[#{text}]".ljust(7)
8
+ end
9
+
10
+ def self.log_msg(status_text, message)
11
+ "%s %s" % [status(status_text), message]
12
+ end
13
+
14
+ def self.log_info(message)
15
+ log_msg('INFO'.green, message)
16
+ end
17
+
18
+ def self.log_error(message)
19
+ log_msg('ERROR'.red, message)
20
+ end
21
+ end
22
+
23
+ puts ColourLogger.log_info('a pretty colourised info message')
24
+ puts ColourLogger.log_error('a pretty colourised error message')
@@ -0,0 +1,14 @@
1
+ require 'ansi_text_styles'
2
+
3
+ using AnsiTextStyles
4
+
5
+ text_styles = {
6
+ red_bold: [:red, :bold],
7
+ blue_underline: [:blue, :underline],
8
+ pretty: [:bg_magenta, :blink],
9
+ }
10
+
11
+ text_styles.each do |name, style|
12
+ styled_text = "Text styled multiple ways".style(style)
13
+ puts "%s: %s" % [name, styled_text]
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'ansi_text_styles'
2
+
3
+ using AnsiTextStyles::Refinement
4
+
5
+ # String.include AnsiTextStyles
6
+ puts 'Foreground using RGB array'.style(fg: [123, 6, 88])
7
+ puts 'Foreground using RGB hash'.style(fg: { r: 5, g: 67, b: 145})
8
+
9
+ puts 'Background using RGB array'.style(bg: [6, 200, 56])
10
+ puts 'Background using RGB hash'.style(bg: { red: 56, green: 99, blue: 240})
11
+
12
+
13
+ puts 'Foreground and Background using RGB array'.style(fg: [250, 200, 4], bg: [6, 7, 245])
14
+ puts 'Foreground and Background using RGB hash'.style(fg: { red: 70, green: 222, blue: 9}, bg: { r: 200, g: 8, b: 0})
@@ -1,17 +1,21 @@
1
1
  module AnsiTextStyles
2
2
 
3
- VERSION = '1.0.0'
3
+ VERSION = '1.1.0'
4
4
 
5
5
  STYLES = {
6
6
  # text properties
7
7
  none: 0, # turn off all attributes
8
- bold: 1, bright: 1, # these do the same thing really
9
- italic: 3, underline: 4, blink: 5,
8
+ bold: 1, bright: 1, # bold or increased intensity
9
+ faint: 2, # decreased intensity
10
+ italic: 3, underline: 4,
11
+ blink: 5, blink_slow: 5, # slow blink
12
+ blink_fast: 6, # fast blink
10
13
  reverse: 7, # swap foreground and background colours
11
14
  hide: 8, # foreground color same as background
15
+ crossed_out: 9,
12
16
 
13
17
  # foreground colours
14
- black: 30, grey: 90, lt_grey: 37, :white => 97,
18
+ black: 30, grey: 90, lt_grey: 37, white: 97,
15
19
  red: 31, lt_red: 91,
16
20
  green: 32, lt_green: 92,
17
21
  dk_yellow: 33, brown: 33, yellow: 93,
@@ -19,6 +23,7 @@ module AnsiTextStyles
19
23
  magenta: 35, pink: 95, lt_magenta: 95,
20
24
  cyan: 36, lt_cyan: 96,
21
25
  default: 39,
26
+ default_fg: 39,
22
27
 
23
28
  # background colours
24
29
  bg_black: 40, bg_grey: 100, bg_lt_grey: 47, bg_white: 107,
@@ -28,6 +33,7 @@ module AnsiTextStyles
28
33
  bg_blue: 44, bg_lt_blue: 104,
29
34
  bg_magenta: 45, bg_pink: 105, bg_lt_magenta: 105,
30
35
  bg_cyan: 46, bg_lt_cyan: 106,
36
+ default_bg: 49,
31
37
  }
32
38
 
33
39
  def self.styles
@@ -35,10 +41,29 @@ module AnsiTextStyles
35
41
  end
36
42
 
37
43
  refine String do
38
- # applies the text attributes to the current string
39
- def style(*styles)
44
+ def style_code(*codes)
45
+ "\e[%sm%s\e[m" % [codes.flatten.compact.join(';'), self.to_s]
46
+ end
47
+
48
+ # applies the named style to the current string
49
+ def style(*styles, fg: nil, bg: nil)
50
+ # convert styles to codes
40
51
  codes = STYLES.values_at(*styles.flatten).compact
41
- "\e[%sm%s\e[m" % [codes.join(';'), self.to_s]
52
+
53
+ # convert foreground and background codes
54
+ codes.push(*coerce_rgb_code(fg))
55
+ codes.push(*coerce_rgb_code(bg, bg: true))
56
+
57
+ style_code(codes)
58
+ end
59
+
60
+ def coerce_rgb_code(value, bg: false)
61
+ v = bg ? 48 : 38
62
+ case value
63
+ when Array ; [v, 2, *Array.new(3) {|i| value[i] || 0 }[0...3]]
64
+ when Hash ; [v, 2, value[:r] || value[:red] || 0, value[:g] || value[:green] || 0, value[:b] || value[:blue] || 0]
65
+ when Integer ; [v, 5, value]
66
+ end
42
67
  end
43
68
 
44
69
  # create instance methods for each text attribute (chainable)
@@ -48,4 +73,3 @@ module AnsiTextStyles
48
73
  end
49
74
 
50
75
  end
51
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ansi_text_styles
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brent Jacobs
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-10-06 00:00:00.000000000 Z
12
+ date: 2017-10-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email:
@@ -18,6 +18,12 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/ansi_text_styles.rb
21
+ - lib/ansi_text_styles/examples/8_bit_colour.rb
22
+ - lib/ansi_text_styles/examples/basic.rb
23
+ - lib/ansi_text_styles/examples/chainable.rb
24
+ - lib/ansi_text_styles/examples/logger.rb
25
+ - lib/ansi_text_styles/examples/stored_styles.rb
26
+ - lib/ansi_text_styles/examples/true_colour.rb
21
27
  homepage: https://github.com/br3nt/ansi_text_styles
22
28
  licenses:
23
29
  - MIT
@@ -38,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
44
  version: '0'
39
45
  requirements: []
40
46
  rubyforge_project:
41
- rubygems_version: 2.4.5.1
47
+ rubygems_version: 2.6.14
42
48
  signing_key:
43
49
  specification_version: 4
44
50
  summary: Adds methods to apply ANSI colour codes and text styles to strings.