colsole 0.4.0 → 0.4.1

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
  SHA1:
3
- metadata.gz: 6635dbbf550e4f14b3fce39cdd3bf9ef110f1195
4
- data.tar.gz: 8957b89565d28da19efa69a56ec3b76cd5d77b55
3
+ metadata.gz: 6020823f438cc49eb3eb4fdf1d18aba0d4b44a71
4
+ data.tar.gz: 743c2add7febab44cd646fdcbd967bb25e5b32ae
5
5
  SHA512:
6
- metadata.gz: 0e8d462f68985228c5aac42bba50f76fadcfcc3a064aa44f39dfe560509e5b4b03caacc8bcc01d1ab4a2167d90fb22b56ac482d6a9910decb65f847da2ec2784
7
- data.tar.gz: 4438c599dd9bd5e716b943e43f1d89bad00e9470006c735b17008a545928129f1173a4da0f4851f71822cd9756ecbdc069c0f50b2e4c81c2ad73c6c8b36cb302
6
+ metadata.gz: 4ee4e4296a5905f1c1452806f1c7ba2a34f1dfbf961e898ac9a57377b16567861a64f90d357a42be2f99d6f31f638324cfeb9a6bb6d0f9c1d75c1cffa87da267
7
+ data.tar.gz: 8a91b479300a1eb7f079f87b33466b8e56f387c3068af097502800d180fbd8e319a6dd1a18af42b1992c860ad9b52baa6fe5d50fe80126c268c349acb7ee64fa
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  Colsole
2
2
  ==================================================
3
3
 
4
- [![Gem Version](https://badge.fury.io/rb/colsole.svg)](http://badge.fury.io/rb/colsole)
5
- [![Build Status](https://travis-ci.org/DannyBen/colsole.svg?branch=master)](https://travis-ci.org/DannyBen/colsole)
6
- [![Code Climate](https://codeclimate.com/github/DannyBen/colsole/badges/gpa.svg)](https://codeclimate.com/github/DannyBen/colsole)
7
- [![Gem](https://img.shields.io/gem/dt/colsole.svg)](https://rubygems.org/gems/colsole)
4
+ [![Gem](https://img.shields.io/gem/v/colsole.svg?style=flat-square)](https://rubygems.org/gems/colsole)
5
+ [![Travis](https://img.shields.io/travis/DannyBen/colsole.svg?style=flat-square)](https://travis-ci.org/DannyBen/colsole)
6
+ [![Code Climate](https://img.shields.io/codeclimate/github/DannyBen/colsole.svg?style=flat-square)](https://codeclimate.com/github/DannyBen/colsole)
7
+ [![Gem](https://img.shields.io/gem/dt/colsole.svg?style=flat-square)](https://rubygems.org/gems/colsole)
8
8
 
9
9
  ---
10
10
 
@@ -1,3 +1,3 @@
1
1
  module Colsole
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
data/lib/colsole.rb CHANGED
@@ -71,7 +71,7 @@ module Colsole
71
71
 
72
72
  # Returns [width, height] of terminal when detected, or a default
73
73
  # value otherwise.
74
- def detect_terminal_size(default=80)
74
+ def detect_terminal_size(default=[80,30])
75
75
  if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/)
76
76
  [ENV['COLUMNS'].to_i, ENV['LINES'].to_i]
77
77
  elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exist?('tput')
@@ -83,50 +83,52 @@ module Colsole
83
83
  end
84
84
  end
85
85
 
86
+ # Returns terminal width with re-asking
87
+ def terminal_width
88
+ @terminal_width ||= detect_terminal_size[0]
89
+ end
90
+
86
91
  # Converts a long string to be wrapped keeping words in tact.
87
92
  # If the string starts with one or more spaces, they will be
88
93
  # preserved in all subsequent lines (i.e., remain indented).
89
- def word_wrap(text, length=78)
94
+ def word_wrap(text, length=nil)
95
+ length ||= terminal_width
90
96
  lead = text[/^\s*/]
91
- length -= lead.size
92
- text.gsub(/(.{1,#{length}}\n?)(\s+|\Z)/, "\\1\n#{lead}").rstrip
97
+ text.strip!
98
+ length -= lead.length
99
+ text.split("\n").collect! do |line|
100
+ line.length > length ? line.gsub(/(.{1,#{length}})(\s+|$)/, "#{lead}\\1\n").rstrip : "#{lead}#{line}"
101
+ end * "\n"
93
102
  end
94
103
 
95
104
  # Parses and returns a color-flagged string.
96
105
  # Respects pipe and auto terminates colored strings.
97
106
  # Call without text to see a list/demo of all available colors.
98
107
  def colorize(text=nil, force_color=false, stream=:stdout)
99
- if text.nil? # Demo
100
- i=33;
101
- colors.each do |k,v|
102
- puts colorize "#{k} = !#{k}! #{i} bottles of beer on the wall !txtrst!"
103
- i -= 1
104
- end
105
- return
106
- end
107
-
108
- reset = colors['txtrst']
109
- if terminal?(stream) or force_color
110
- reset_called_last = true
111
-
112
- out = text.gsub(/\!([a-z]{6})\!/) do |m|
113
- reset_called_last = $1 == "txtrst";
114
- colors[$1];
115
- end
116
- reset_called_last or out = "#{out}#{reset}";
117
- else
118
- out = text.gsub(/\!([a-z]{6})\!/, '')
119
- end
120
-
121
- return out
108
+ return show_color_demo if text.nil?
109
+ return strip_color_markers(text) unless terminal?(stream) || force_color
110
+ colorize! text
122
111
  end
123
112
 
124
- private
113
+ private
125
114
 
126
115
  def colors
127
116
  @colors ||= prepare_colors
128
117
  end
129
118
 
119
+ def colorize!(text)
120
+ reset = colors['txtrst']
121
+ reset_called_last = true
122
+
123
+ out = text.gsub(/\!([a-z]{6})\!/) do |m|
124
+ reset_called_last = $1 == "txtrst";
125
+ colors[$1];
126
+ end
127
+
128
+ reset_called_last or out = "#{out}#{reset}";
129
+ out
130
+ end
131
+
130
132
  # Create a colors array with keys such as :txtgrn and :bldgrn
131
133
  # and values which are the escape codes for the colors.
132
134
  def prepare_colors
@@ -149,6 +151,19 @@ module Colsole
149
151
  colors['txtrst'] = pattern_reset
150
152
  colors
151
153
  end
154
+
155
+ def show_color_demo
156
+ i=33;
157
+ colors.each do |k,v|
158
+ puts colorize "#{k} = !#{k}! #{i} bottles of beer on the wall !txtrst!"
159
+ i -= 1
160
+ end
161
+ end
162
+
163
+ def strip_color_markers(text)
164
+ text.gsub(/\!([a-z]{6})\!/, '')
165
+ end
166
+
152
167
  end
153
168
 
154
169
  self.extend Colsole
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: colsole
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-16 00:00:00.000000000 Z
11
+ date: 2016-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: runfile