colsole 0.1.3.pre
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 +7 -0
- data/lib/colsole.rb +140 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bafb72291698639e2a12345bbf7de2dfc75f261f
|
4
|
+
data.tar.gz: ec45fb81a649e350b16b97d2bf7704e0920f6537
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 593f13b898d632c9b4b1d4c015ecdbfe67fc72fff6b2452110fd9ff854d237d9ffa574c3f723046eaf3446db2333b55e56a857529a525c062209eed2faa39035
|
7
|
+
data.tar.gz: 47eaf0c846adb07fd7571175bd37607ba074b6b8311eb168cdece0e741878616e7a0c5a6a44d541fc281318864c2555791534d64c5f7ec7923f70a7733c701aa
|
data/lib/colsole.rb
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
# Colsole - Coloful Console Applications
|
2
|
+
#
|
3
|
+
# This class provides several utility functions for console
|
4
|
+
# appliucation developers.
|
5
|
+
#
|
6
|
+
# - #colorize string - return a colorized strings
|
7
|
+
# - #say string - print a string with colors
|
8
|
+
# - #resay string - same as say, but overwrite current line
|
9
|
+
# - #word_wrap strimg - wrap a string and maintain indentation
|
10
|
+
# - #detect_terminal_size
|
11
|
+
#
|
12
|
+
# Credits:
|
13
|
+
# terminal width detection by Gabrial Horner https://github.com/cldwalker
|
14
|
+
|
15
|
+
module Colsole
|
16
|
+
|
17
|
+
# Prints a color-flagged string.
|
18
|
+
# Use color flags (like !txtred!) to change color in the string.
|
19
|
+
# Space terminated strings will leave the cursor at the same line.
|
20
|
+
def say(text, force_color=false)
|
21
|
+
last = text[-1, 1]
|
22
|
+
if last == ' ' or last == '\t'
|
23
|
+
print colorize text, force_color
|
24
|
+
else
|
25
|
+
print colorize "#{text}!txtrst!\n", $force_color;
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Erase the current output line, and say a new string.
|
30
|
+
# This should be used after a space terminated say().
|
31
|
+
def resay(text, force_color=false)
|
32
|
+
is_terminal and text = "\033[2K\r#{text}"
|
33
|
+
say text, force_color
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns true if interactive terminal, false if piped.
|
37
|
+
def is_terminal
|
38
|
+
STDOUT.tty?
|
39
|
+
end
|
40
|
+
|
41
|
+
# Determines if a shell command exists.
|
42
|
+
def command_exist?(command)
|
43
|
+
ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exist? File.join(d, command) }
|
44
|
+
end
|
45
|
+
|
46
|
+
# Returns [width, height] of terminal when detected, or a default
|
47
|
+
# value otherwise.
|
48
|
+
def detect_terminal_size(default=80)
|
49
|
+
if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/)
|
50
|
+
[ENV['COLUMNS'].to_i, ENV['LINES'].to_i]
|
51
|
+
elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exist?('tput')
|
52
|
+
[`tput cols`.to_i, `tput lines`.to_i]
|
53
|
+
elsif STDIN.tty? && command_exist?('stty')
|
54
|
+
`stty size`.scan(/\d+/).map { |s| s.to_i }.reverse
|
55
|
+
else
|
56
|
+
default
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Converts a long string to be wrapped.
|
61
|
+
# If the string starts with one or more spaces, they will be
|
62
|
+
# preserved in all subsequent lines (i.e., remain indented).
|
63
|
+
#--
|
64
|
+
# TODO: this is currently not word wrap, but just wrap.
|
65
|
+
# either rename or redo
|
66
|
+
def word_wrap(str, length=80, character=$/)
|
67
|
+
lead = str[/^\s+/]
|
68
|
+
length -= lead.size
|
69
|
+
str.scan(/.{#{length}}|.+/).map { |x| "#{lead}#{x.strip}" }.join(character)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Parses and returns a color-flagged string.
|
73
|
+
# Respects pipe and auto terminates colored strings.
|
74
|
+
# Call without text to see a list/demo of all available colors.
|
75
|
+
def colorize(text=nil, force_color=false)
|
76
|
+
colors = {
|
77
|
+
'txtblk' => '[0;30m', # Black - Regular
|
78
|
+
'txtred' => '[0;31m', # Red
|
79
|
+
'txtgrn' => '[0;32m', # Green
|
80
|
+
'txtylw' => '[0;33m', # Yellow
|
81
|
+
'txtblu' => '[0;34m', # Blue
|
82
|
+
'txtpur' => '[0;35m', # Purple
|
83
|
+
'txtcyn' => '[0;36m', # Cyan
|
84
|
+
'txtwht' => '[0;37m', # White
|
85
|
+
'bldblk' => '[1;30m', # Black - Bold
|
86
|
+
'bldred' => '[1;31m', # Red
|
87
|
+
'bldgrn' => '[1;32m', # Green
|
88
|
+
'bldylw' => '[1;33m', # Yellow
|
89
|
+
'bldblu' => '[1;34m', # Blue
|
90
|
+
'bldpur' => '[1;35m', # Purple
|
91
|
+
'bldcyn' => '[1;36m', # Cyan
|
92
|
+
'bldwht' => '[1;37m', # White
|
93
|
+
'unkblk' => '[4;30m', # Black - Underline
|
94
|
+
'undred' => '[4;31m', # Red
|
95
|
+
'undgrn' => '[4;32m', # Green
|
96
|
+
'undylw' => '[4;33m', # Yellow
|
97
|
+
'undblu' => '[4;34m', # Blue
|
98
|
+
'undpur' => '[4;35m', # Purple
|
99
|
+
'undcyn' => '[4;36m', # Cyan
|
100
|
+
'undwht' => '[4;37m', # White
|
101
|
+
'bakblk' => '[40m' , # Black - Background
|
102
|
+
'bakred' => '[41m' , # Red
|
103
|
+
'bakgrn' => '[42m' , # Green
|
104
|
+
'bakylw' => '[43m' , # Yellow
|
105
|
+
'bakblu' => '[44m' , # Blue
|
106
|
+
'bakpur' => '[45m' , # Purple
|
107
|
+
'bakcyn' => '[46m' , # Cyan
|
108
|
+
'bakwht' => '[47m' , # White
|
109
|
+
'txtrst' => '[0m' , # Text Reset
|
110
|
+
}
|
111
|
+
|
112
|
+
if text.nil? # Demo
|
113
|
+
i=33;
|
114
|
+
colors.each do |k,v|
|
115
|
+
puts colorize "#{k} = !#{k}! #{i} bottles of beer on the wall !txtrst!"
|
116
|
+
i -= 1
|
117
|
+
end
|
118
|
+
return
|
119
|
+
end
|
120
|
+
|
121
|
+
esc = 27.chr
|
122
|
+
reset = esc + colors['txtrst']
|
123
|
+
if is_terminal or force_color
|
124
|
+
reset_called_last = false
|
125
|
+
|
126
|
+
out = text.gsub /\!([a-z]{6})\!/ do |m|
|
127
|
+
reset_called_last = $1 == "txtrst";
|
128
|
+
esc + colors[$1];
|
129
|
+
end
|
130
|
+
|
131
|
+
reset_called_last or out = "#{out}#{reset}";
|
132
|
+
else
|
133
|
+
out = text.gsub /\!([a-z]{6})\!/, ''
|
134
|
+
end
|
135
|
+
|
136
|
+
return out
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
self.extend Colsole
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: colsole
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3.pre
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Danny Ben Shitrit
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Utility functions for colorful console applications
|
14
|
+
email: db@dannyben.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/colsole.rb
|
20
|
+
homepage: http://sector-seven.net
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">"
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.3.1
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.4.6
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Colorful Console Applications
|
44
|
+
test_files: []
|
45
|
+
has_rdoc:
|