inkjet 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e023659caec2f6d13e4ccac3a5d9e4f43ad44dac
4
+ data.tar.gz: d60ea9025dc3235ab98070c75db4100e3d0c9945
5
+ SHA512:
6
+ metadata.gz: b30fb2d6a9da6fe1d8a6e0e0792ab7dcca1023db54b7b39de68519183b8451d17a8e20068fa9f6e6f1677902f8b4f6fecb5ac4330d0e5d4af350d9362954d46b
7
+ data.tar.gz: 19f0086725fafe39236dd71caa6257574fc809c9cac33e5ef0d6cc9cf9447be63be62dc26b6329be2e6145a9e462f1ad41871c3f9b8525f636d8803586227fd1
@@ -0,0 +1,97 @@
1
+ module Inkjet
2
+ module Colors
3
+ Colors = %w(black red green yellow blue magenta cyan gray white)
4
+ module Foreground
5
+ Black = 30
6
+ Red = 31
7
+ Green = 32
8
+ Yellow = 33
9
+ Blue = 34
10
+ Magenta = 35
11
+ Cyan = 36
12
+ Gray = 37
13
+ Default = 39
14
+ White = 97
15
+ end
16
+
17
+ module Background
18
+ Black = 40
19
+ Red = 41
20
+ Green = 42
21
+ Yellow = 43
22
+ Blue = 44
23
+ Magenta = 45
24
+ Cyan = 46
25
+ Gray = 47
26
+ Default = 49
27
+ White = 107
28
+ end
29
+
30
+ def self.foreground(color)
31
+ "::Inkjet::Colors::Foreground::#{color.capitalize}".constantize rescue raise("Color does not exist: '#{color}'")
32
+ end
33
+
34
+ def self.background(color)
35
+ "::Inkjet::Colors::Background::#{color.capitalize}".constantize rescue raise("Color does not exist: '#{color}'")
36
+ end
37
+
38
+ module Formatters
39
+ def self.included(base)
40
+ base.send :extend, self
41
+ end
42
+
43
+ def colorize(color, str)
44
+ colorize!(color, str.clone)
45
+ end
46
+
47
+ def colorize!(color, str)
48
+ str.apply_inkjet_code!(Inkjet::Colors.foreground(color))
49
+ end
50
+
51
+ def colorize_background(color, str)
52
+ colorize_background!(color, str.clone)
53
+ end
54
+
55
+ def colorize_background!(color, str)
56
+ str.apply_inkjet_code!(Inkjet::Colors.background(color))
57
+ end
58
+
59
+ def colorize_with_background(fg_color, bg_color, str)
60
+ colorize_with_background!(fg_color, bg_color, str.clone)
61
+ end
62
+
63
+ def colorize_with_background!(fg_color, bg_color, str)
64
+ str.apply_inkjet_codes!(Inkjet::Colors.foreground(fg_color), Inkjet::Colors.background(bg_color))
65
+ end
66
+
67
+ def method_missing(meth, *args)
68
+ if meth.match(/\A(#{Inkjet::Colors::Colors.push("default").join("|")})_text(!?)\Z/)
69
+ colorize($1, args.map(&:to_s).join(" "))
70
+ elsif meth.match(/\A(#{Inkjet::Colors::Colors.push("default").join("|")})_(background|bg)(!?)\Z/)
71
+ colorize_background($1, args.map(&:to_s).join(" "))
72
+ elsif meth.match(/\A(#{Inkjet::Colors::Colors.push("default").join("|")})_(#{Inkjet::Colors::Colors.push("default").join("|")})(!?)\Z/)
73
+ colorize_with_background($1, $2, args.map(&:to_s).join(" "))
74
+ else
75
+ super
76
+ end
77
+ end
78
+ end
79
+
80
+ module String
81
+ def method_missing(meth, *args)
82
+ if meth.match(/\A(#{Inkjet::Colors::Colors.join("|")})(!?)\Z/)
83
+ Inkjet.send("colorize#{$2}", $1, self)
84
+ elsif meth.match(/\A(#{Inkjet::Colors::Colors.join("|")})_(background|bg)(!?)\Z/)
85
+ Inkjet.send("colorize_background#{$3}", $1, self)
86
+ elsif meth.match(/\A(#{Inkjet::Colors::Colors.join("|")})_(#{Inkjet::Colors::Colors.join("|")})(!?)\Z/)
87
+ Inkjet.send("colorize_with_background#{$3}", $1, $2, self)
88
+ else
89
+ super
90
+ end
91
+ end
92
+ end
93
+
94
+ end
95
+ end
96
+
97
+ String.send :include, Inkjet::Colors::String
@@ -0,0 +1,72 @@
1
+ module Inkjet
2
+ class Configuration
3
+ DEFAULT_CONFIGURATION_OPTIONS = {:tabstop => 2}
4
+
5
+ attr_reader *DEFAULT_CONFIGURATION_OPTIONS.keys
6
+
7
+ DEFAULT_CONFIGURATION_OPTIONS.keys.each do |key|
8
+ define_method "#{key.to_s}=" do |val|
9
+ @changed[key] = [send(key), val]
10
+ instance_variable_set "@#{key.to_s}", val
11
+ end
12
+ end
13
+
14
+ # Returns a hash of all the changed keys and values after being reconfigured
15
+ def changed
16
+ @changed = {}
17
+ to_hash.each { |key,val| @changed[key] = [@saved_state[key], val] if @saved_state[key] != val }
18
+ @changed
19
+ end
20
+
21
+ # Check whether a key was changed after being reconfigured
22
+ def changed?(key)
23
+ changed.has_key?(key)
24
+ end
25
+
26
+ # Pass arguments and/or a block to configure the available options
27
+ def configure(args={}, &block)
28
+ save_state
29
+ configure_with_args args
30
+ configure_with_block &block if block_given?
31
+ self
32
+ end
33
+
34
+ # Accepts arguments which are used to configure available options
35
+ def configure_with_args(args)
36
+ args.select { |k,v| DEFAULT_CONFIGURATION_OPTIONS.keys.include?(k) }.each do |key,val|
37
+ instance_variable_set "@#{key.to_s}", val
38
+ end
39
+ end
40
+
41
+ # Accepts a block which is used to configure available options
42
+ def configure_with_block(&block)
43
+ self.instance_eval(&block) if block_given?
44
+ end
45
+
46
+ # Saves a copy of the current state, to be used later to determine what was changed
47
+ def save_state
48
+ @saved_state = clone.to_hash
49
+ @changed = {}
50
+ end
51
+
52
+ def to_hash
53
+ h = {}
54
+ DEFAULT_CONFIGURATION_OPTIONS.keys.each do |key|
55
+ h[key] = instance_variable_get "@#{key.to_s}"
56
+ end
57
+ h
58
+ end
59
+ alias_method :to_h, :to_hash
60
+
61
+ protected
62
+
63
+ def initialize
64
+ DEFAULT_CONFIGURATION_OPTIONS.each do |key,val|
65
+ instance_variable_set "@#{key.to_s}", val
66
+ end
67
+ save_state
68
+ super
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,38 @@
1
+ module Inkjet
2
+ module Indent
3
+ TABSTOP = 2 # TODO set based on config
4
+
5
+ def self.indent(*args, &block)
6
+ @spaces ||= 0
7
+ if block_given?
8
+ @spaces += args[0] || TABSTOP
9
+ class_eval &block
10
+ @spaces -= args[0] || TABSTOP
11
+ else
12
+ spaces = args[1] || TABSTOP
13
+ "#{padding(@spaces + spaces.to_i)}#{args[0].to_s.split("\n").join("\n#{padding(@spaces + spaces.to_i)}")}"
14
+ end
15
+ end
16
+
17
+ def self.padding(spaces)
18
+ spaces.times.map {" "}.join
19
+ end
20
+
21
+ def self.puts(output)
22
+ STDOUT.puts indent(output, 0)
23
+ end
24
+
25
+ def self.print(output)
26
+ STDOUT.print indent(output, 0)
27
+ end
28
+
29
+ module String
30
+ def indent(*args)
31
+ Inkjet::Indent.indent(*args.unshift(self))
32
+ end
33
+ end
34
+
35
+ end
36
+ end
37
+
38
+ String.send :include, Inkjet::Indent::String
@@ -0,0 +1,86 @@
1
+ module Inkjet
2
+ module String
3
+ def bold
4
+ clone.bold!
5
+ end
6
+
7
+ def bold!
8
+ apply_inkjet_code!(Inkjet::Bold)
9
+ end
10
+
11
+ def dim
12
+ clone.dim!
13
+ end
14
+
15
+ def dim!
16
+ apply_inkjet_code!(Inkjet::Dim)
17
+ end
18
+
19
+ def underline
20
+ clone.underline!
21
+ end
22
+
23
+ def underline!
24
+ apply_inkjet_code!(Inkjet::Underline)
25
+ end
26
+
27
+ def blink
28
+ clone.blink!
29
+ end
30
+
31
+ def blink!
32
+ apply_inkjet_code!(Inkjet::Blink)
33
+ end
34
+
35
+ def invert
36
+ clone.invert!
37
+ end
38
+
39
+ def invert!
40
+ apply_inkjet_code!(Inkjet::Invert)
41
+ end
42
+
43
+ def hidden
44
+ clone.hidden!
45
+ end
46
+
47
+ def hidden!
48
+ apply_inkjet_code!(Inkjet::Hidden)
49
+ end
50
+
51
+ def inkjet_codes
52
+ match(/\e\[([\d;]+)m/)[1].split(";")
53
+ rescue
54
+ Array.new
55
+ end
56
+
57
+ def apply_inkjet_code(code)
58
+ clone.apply_inkjet_code!(c)
59
+ end
60
+
61
+ def apply_inkjet_code!(code)
62
+ inkjet_codes.length > 0 ? sub!(/\e\[([\d;]+)m/, "\e[#{inkjet_codes.push(code).join(";")}m") : replace("\e[#{code}m#{self}\e[0m")
63
+ end
64
+
65
+ def apply_inkjet_codes(*codes)
66
+ sclone = clone
67
+ codes.each { |code| sclone.apply_inkjet_code!(code) }
68
+ sclone
69
+ end
70
+
71
+ def apply_inkjet_codes!(*codes)
72
+ codes.each { |code| apply_inkjet_code!(code) }
73
+ self
74
+ end
75
+
76
+ def clean
77
+ clone.clean!
78
+ end
79
+
80
+ def clean!
81
+ gsub!(/\e\[[\d;]+m/, '')
82
+ end
83
+ end
84
+ end
85
+
86
+ String.send :include, Inkjet::String
@@ -0,0 +1,3 @@
1
+ module Inkjet
2
+ VERSION = '0.0.1'
3
+ end
data/lib/inkjet.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'active_support/inflector'
2
+ require 'inkjet/string'
3
+ require 'inkjet/colors'
4
+ require 'inkjet/indent'
5
+
6
+ module Inkjet
7
+ Close = 0
8
+ Bold = 1
9
+ Dim = 2
10
+ Underline = 4
11
+ Blink = 5
12
+ Invert = 7
13
+ Hidden = 8
14
+ include Colors::Formatters
15
+
16
+ def self.escape(code)
17
+ "\e[#{code.to_s.chomp('m')}m"
18
+ end
19
+
20
+ def self.indent(*args, &block)
21
+ Indent.indent(*args, &block)
22
+ end
23
+ end
@@ -0,0 +1 @@
1
+ # TODO specs
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inkjet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mark Rebec
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Formatting, indentation, bash colors, etc. for ruby cli script output
42
+ email:
43
+ - mark@markrebec.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/inkjet/string.rb
49
+ - lib/inkjet/indent.rb
50
+ - lib/inkjet/configuration.rb
51
+ - lib/inkjet/colors.rb
52
+ - lib/inkjet/version.rb
53
+ - lib/inkjet.rb
54
+ - spec/spec_helper.rb
55
+ homepage: http://github.com/markrebec/inkjet
56
+ licenses: []
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.0.0.rc.2
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Pretty output for ruby cli scripts
78
+ test_files:
79
+ - spec/spec_helper.rb
80
+ has_rdoc: