tty-color 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/README.md +4 -5
- data/lib/tty/color.rb +11 -2
- data/lib/tty/color/mode.rb +2 -2
- data/lib/tty/color/support.rb +30 -28
- data/lib/tty/color/version.rb +1 -1
- data/spec/unit/mode_spec.rb +63 -41
- data/spec/unit/supports_spec.rb +78 -36
- data/tasks/console.rake +1 -1
- metadata +3 -5
- data/bin/color +0 -42
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e202bf4ea2e5da8b715888731cf0e9488afde5db
|
4
|
+
data.tar.gz: 63d578fe8363573b353d4f0cf867c5c0f78f9b5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5db3f22c98af1803e7fe028263f4f95d3a3fb89e7008cc33f9ce4cc5e5066232be6e8cc7d4f5fb84fda2171525965a3bb53fe13d2cd796dcb58a6b0a667e02aa
|
7
|
+
data.tar.gz: 4547260a08d17f7f11181a38d4f68876a7ef81c8f7388524042f5e704ba0fdc5e34a788fc959f0c7004db430fa2c61d6482e3854343e9640da274034ad0c9a11
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,18 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.2.0] - 2016-01-13
|
4
|
+
|
5
|
+
### Changed
|
6
|
+
|
7
|
+
* Change ordering of color support checks by @janlelis
|
8
|
+
* Change ordering of color mode
|
9
|
+
* Change Support#from_env to check ansicon
|
10
|
+
* Ensure #tty? works for non-terminal devices
|
11
|
+
* Remove color executable
|
12
|
+
|
3
13
|
## [v0.1.0] - 2016-01-02
|
4
14
|
|
5
15
|
* Initial implementation and release
|
6
16
|
|
17
|
+
[v0.2.0]: https://github.com/peter-murach/tty-color/compare/v0.1.0...v0.2.0
|
7
18
|
[v0.1.0]: https://github.com/peter-murach/tty-color/compare/v0.1.0
|
data/README.md
CHANGED
@@ -58,19 +58,18 @@ puts color?
|
|
58
58
|
|
59
59
|
## Command line tool
|
60
60
|
|
61
|
-
|
61
|
+
[tty-color-cli](https://github.com/peter-murach/tty-color-cli) is a command line tool for the **TTY::Color**.
|
62
|
+
|
63
|
+
To check if terminal supports colors do:
|
62
64
|
|
63
65
|
```bash
|
64
|
-
color
|
65
66
|
color -s
|
66
|
-
color --support
|
67
67
|
```
|
68
68
|
|
69
|
-
and to check
|
69
|
+
and to check color mode:
|
70
70
|
|
71
71
|
```bash
|
72
72
|
color -m
|
73
|
-
color --mode
|
74
73
|
```
|
75
74
|
|
76
75
|
## Contributing
|
data/lib/tty/color.rb
CHANGED
@@ -19,6 +19,11 @@ module TTY
|
|
19
19
|
|
20
20
|
attr_accessor :output, :verbose
|
21
21
|
|
22
|
+
# Check if terminal supports colors
|
23
|
+
#
|
24
|
+
# @return [Boolean]
|
25
|
+
#
|
26
|
+
# @api public
|
22
27
|
def supports?
|
23
28
|
Support.new(ENV, verbose: verbose).supports?
|
24
29
|
end
|
@@ -34,9 +39,13 @@ module TTY
|
|
34
39
|
Mode.new(ENV).mode
|
35
40
|
end
|
36
41
|
|
37
|
-
#
|
42
|
+
# Check if output is linked with terminal
|
43
|
+
#
|
44
|
+
# @return [Boolean]
|
45
|
+
#
|
46
|
+
# @api public
|
38
47
|
def tty?
|
39
|
-
output.tty?
|
48
|
+
output.respond_to(:tty?) && output.tty?
|
40
49
|
end
|
41
50
|
end # Color
|
42
51
|
end # TTY
|
data/lib/tty/color/mode.rb
CHANGED
@@ -35,7 +35,7 @@ module TTY
|
|
35
35
|
return 0 unless TTY::Color.tty?
|
36
36
|
|
37
37
|
value = 8
|
38
|
-
%w(from_tput
|
38
|
+
%w(from_term from_tput).each do |from_check|
|
39
39
|
break if (value = public_send(from_check)) != NoValue
|
40
40
|
end
|
41
41
|
return 8 if value == NoValue
|
@@ -67,7 +67,7 @@ module TTY
|
|
67
67
|
#
|
68
68
|
# @api private
|
69
69
|
def from_tput
|
70
|
-
colors =
|
70
|
+
colors = `tput colors 2>/dev/null`.to_i
|
71
71
|
colors >= 8 ? colors : NoValue
|
72
72
|
rescue Errno::ENOENT
|
73
73
|
NoValue
|
data/lib/tty/color/support.rb
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
module TTY
|
4
4
|
module Color
|
5
5
|
class Support
|
6
|
+
SOURCES = %w(from_term from_tput from_env from_curses).freeze
|
7
|
+
|
6
8
|
# Initialize a color support
|
7
9
|
# @api public
|
8
10
|
def initialize(env, options = {})
|
@@ -20,13 +22,40 @@ module TTY
|
|
20
22
|
return false unless TTY::Color.tty?
|
21
23
|
|
22
24
|
value = false
|
23
|
-
|
25
|
+
SOURCES.each do |from_check|
|
24
26
|
break if (value = public_send(from_check)) != NoValue
|
25
27
|
end
|
26
28
|
return false if value == NoValue
|
27
29
|
value
|
28
30
|
end
|
29
31
|
|
32
|
+
# Inspect environment $TERM variable for color support
|
33
|
+
#
|
34
|
+
# @api private
|
35
|
+
def from_term
|
36
|
+
case @env['TERM']
|
37
|
+
when 'dumb' then false
|
38
|
+
when /^screen|^xterm|^vt100|color|ansi|cygwin|linux/i then true
|
39
|
+
else NoValue
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Shell out to tput to check color support
|
44
|
+
#
|
45
|
+
# @api private
|
46
|
+
def from_tput
|
47
|
+
`tput colors 2>/dev/null`.to_i > 2
|
48
|
+
rescue Errno::ENOENT
|
49
|
+
NoValue
|
50
|
+
end
|
51
|
+
|
52
|
+
# Check if environment specifies color variables
|
53
|
+
#
|
54
|
+
# @api private
|
55
|
+
def from_env
|
56
|
+
['COLORTERM', 'ANSICON'].any? { |key| @env.key?(key) } || NoValue
|
57
|
+
end
|
58
|
+
|
30
59
|
# Attempt to load curses to check color support
|
31
60
|
#
|
32
61
|
# @return [Boolean]
|
@@ -48,33 +77,6 @@ module TTY
|
|
48
77
|
warn 'no native curses support' if @verbose
|
49
78
|
NoValue
|
50
79
|
end
|
51
|
-
|
52
|
-
# Shell out to tput to check color support
|
53
|
-
#
|
54
|
-
# @api private
|
55
|
-
def from_tput
|
56
|
-
%x(tput colors 2>/dev/null).to_i > 2
|
57
|
-
rescue Errno::ENOENT
|
58
|
-
NoValue
|
59
|
-
end
|
60
|
-
|
61
|
-
# Inspect environment $TERM variable for color support
|
62
|
-
#
|
63
|
-
# @api private
|
64
|
-
def from_term
|
65
|
-
case @env['TERM']
|
66
|
-
when 'dumb' then false
|
67
|
-
when /^screen|^xterm|^vt100|color|ansi|cygwin|linux/i then true
|
68
|
-
else NoValue
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
# Check if environment specifies color term
|
73
|
-
#
|
74
|
-
# @api private
|
75
|
-
def from_env
|
76
|
-
@env.include?('COLORTERM')
|
77
|
-
end
|
78
80
|
end # Support
|
79
81
|
end # Color
|
80
82
|
end # TTY
|
data/lib/tty/color/version.rb
CHANGED
data/spec/unit/mode_spec.rb
CHANGED
@@ -12,61 +12,83 @@ RSpec.describe TTY::Color::Mode, 'detecting mode' do
|
|
12
12
|
mode = described_class.new({})
|
13
13
|
allow(mode).to receive(:from_term).and_return(TTY::Color::NoValue)
|
14
14
|
allow(mode).to receive(:from_tput).and_return(TTY::Color::NoValue)
|
15
|
+
|
15
16
|
expect(mode.mode).to eq(8)
|
17
|
+
expect(mode).to have_received(:from_term).ordered
|
18
|
+
expect(mode).to have_received(:from_tput).ordered
|
16
19
|
end
|
17
20
|
|
18
|
-
it
|
21
|
+
it "detects color mode" do
|
22
|
+
allow(TTY::Color).to receive(:tty?).and_return(true)
|
19
23
|
mode = described_class.new('TERM' => 'xterm-256color')
|
20
|
-
expect(mode.from_term).to eq(256)
|
21
|
-
end
|
22
24
|
|
23
|
-
|
24
|
-
mode = described_class.new('TERM' => 'iTerm.app')
|
25
|
-
expect(mode.from_term).to eq(256)
|
25
|
+
expect(mode.mode).to eq(256)
|
26
26
|
end
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
context '#from_term' do
|
29
|
+
it 'infers mode for xterm-256color' do
|
30
|
+
mode = described_class.new('TERM' => 'xterm-256color')
|
31
|
+
expect(mode.from_term).to eq(256)
|
32
|
+
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
it 'infers mode for iTerm.app' do
|
35
|
+
mode = described_class.new('TERM' => 'iTerm.app')
|
36
|
+
expect(mode.from_term).to eq(256)
|
37
|
+
end
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
it 'infers mode from terminal environment' do
|
40
|
+
mode = described_class.new('TERM' => 'amiga-8bit')
|
41
|
+
expect(mode.from_term).to eq(256)
|
42
|
+
end
|
42
43
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
44
|
+
it 'infers mode for wy730' do
|
45
|
+
mode = described_class.new('TERM' => 'wy370-105k')
|
46
|
+
expect(mode.from_term).to eq(64)
|
47
|
+
end
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
49
|
+
it 'infers mode for d430-unix-ccc' do
|
50
|
+
mode = described_class.new('TERM' => 'd430-unix-ccc')
|
51
|
+
expect(mode.from_term).to eq(52)
|
52
|
+
end
|
52
53
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
it 'infers mode for d430-unix-s-ccc' do
|
55
|
+
mode = described_class.new('TERM' => 'd430c-unix-s-ccc')
|
56
|
+
expect(mode.from_term).to eq(52)
|
57
|
+
end
|
57
58
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
59
|
+
it 'infers mode for nsterm-bce' do
|
60
|
+
mode = described_class.new('TERM' => 'nsterm-bce')
|
61
|
+
expect(mode.from_term).to eq(16)
|
62
|
+
end
|
62
63
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
64
|
+
it 'infers mode for d430-c' do
|
65
|
+
mode = described_class.new('TERM' => 'd430c-dg')
|
66
|
+
expect(mode.from_term).to eq(16)
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'infers mode for d430-unix-w' do
|
70
|
+
mode = described_class.new('TERM' => 'd430-unix-w')
|
71
|
+
expect(mode.from_term).to eq(16)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'infers mode for vt100' do
|
75
|
+
mode = described_class.new('TERM' => 'konsole-vt100')
|
76
|
+
expect(mode.from_term).to eq(8)
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'infers mode for xnuppc' do
|
80
|
+
mode = described_class.new('TERM' => 'xnuppc+basic')
|
81
|
+
expect(mode.from_term).to eq(8)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'infers mode fro dummy terminal' do
|
85
|
+
mode = described_class.new('TERM' => 'dummy')
|
86
|
+
expect(mode.from_term).to eq(0)
|
87
|
+
end
|
67
88
|
|
68
|
-
|
69
|
-
|
70
|
-
|
89
|
+
it "doesn't match any term variable" do
|
90
|
+
mode = described_class.new({})
|
91
|
+
expect(mode.from_term).to eq(TTY::Color::NoValue)
|
92
|
+
end
|
71
93
|
end
|
72
94
|
end
|
data/spec/unit/supports_spec.rb
CHANGED
@@ -17,54 +17,96 @@ RSpec.describe TTY::Color::Support, '#supports?' do
|
|
17
17
|
allow(support).to receive(:from_env).and_return(TTY::Color::NoValue)
|
18
18
|
|
19
19
|
expect(support.supports?).to eq(false)
|
20
|
+
|
21
|
+
expect(support).to have_received(:from_term).ordered
|
22
|
+
expect(support).to have_received(:from_tput).ordered
|
23
|
+
expect(support).to have_received(:from_env).ordered
|
24
|
+
expect(support).to have_received(:from_curses).ordered
|
20
25
|
end
|
21
26
|
|
22
|
-
it "
|
23
|
-
support = described_class.new(
|
24
|
-
allow(
|
25
|
-
|
26
|
-
allow(support).to receive(:warn)
|
27
|
+
it "detects color support" do
|
28
|
+
support = described_class.new('TERM' => 'xterm')
|
29
|
+
allow(TTY::Color).to receive(:tty?).and_return(true)
|
30
|
+
allow(support).to receive(:from_tput)
|
27
31
|
|
28
|
-
expect(support.
|
29
|
-
expect(support).to_not have_received(:
|
32
|
+
expect(support.supports?).to eq(true)
|
33
|
+
expect(support).to_not have_received(:from_tput)
|
30
34
|
end
|
31
35
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
36
|
+
context '#from_curses' do
|
37
|
+
it "fails to load curses for color support" do
|
38
|
+
support = described_class.new({})
|
39
|
+
allow(support).to receive(:require).with('curses').
|
40
|
+
and_raise(LoadError)
|
41
|
+
allow(support).to receive(:warn)
|
37
42
|
|
38
|
-
|
43
|
+
expect(support.from_curses).to eq(TTY::Color::NoValue)
|
44
|
+
expect(support).to_not have_received(:warn)
|
45
|
+
end
|
39
46
|
|
40
|
-
|
41
|
-
|
47
|
+
it "fails to find Curses namespace" do
|
48
|
+
support = described_class.new({})
|
49
|
+
allow(support).to receive(:require).with('curses')
|
42
50
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
expect(support.from_curses(curses)).to eql(true)
|
53
|
-
expect(curses).to have_received(:has_colors?)
|
54
|
-
end
|
51
|
+
expect(support.from_curses).to eq(TTY::Color::NoValue)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "sets verbose mode on" do
|
55
|
+
support = described_class.new({}, verbose: true)
|
56
|
+
allow(support).to receive(:require).with('curses').
|
57
|
+
and_raise(LoadError)
|
58
|
+
allow(support).to receive(:warn)
|
55
59
|
|
56
|
-
|
57
|
-
|
58
|
-
|
60
|
+
support.from_curses
|
61
|
+
|
62
|
+
expect(support).to have_received(:warn).with(/no native curses support/)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "loads curses for color support" do
|
66
|
+
support = described_class.new({})
|
67
|
+
allow(support).to receive(:require).with('curses').and_return(true)
|
68
|
+
stub_const("Curses", Object.new)
|
69
|
+
curses = double(:curses)
|
70
|
+
allow(curses).to receive(:init_screen)
|
71
|
+
allow(curses).to receive(:has_colors?).and_return(true)
|
72
|
+
allow(curses).to receive(:close_screen)
|
73
|
+
|
74
|
+
expect(support.from_curses(curses)).to eql(true)
|
75
|
+
expect(curses).to have_received(:has_colors?)
|
76
|
+
end
|
59
77
|
end
|
60
78
|
|
61
|
-
|
62
|
-
|
63
|
-
|
79
|
+
context '#form_term' do
|
80
|
+
it "fails to find color for dumb terminal" do
|
81
|
+
support = described_class.new('TERM' => 'dumb')
|
82
|
+
expect(support.from_term).to eq(false)
|
83
|
+
end
|
84
|
+
|
85
|
+
it "inspects term variable for color capabilities" do
|
86
|
+
support = described_class.new('TERM' => 'xterm')
|
87
|
+
expect(support.from_term).to eq(true)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "fails to find color capabilities from term variable " do
|
91
|
+
support = described_class.new('TERM' => 'atari')
|
92
|
+
expect(support.from_term).to eq(TTY::Color::NoValue)
|
93
|
+
end
|
64
94
|
end
|
65
95
|
|
66
|
-
|
67
|
-
support
|
68
|
-
|
96
|
+
context '#from_env' do
|
97
|
+
it "finds color support in colorterm variable" do
|
98
|
+
support = described_class.new('COLORTERM' => true)
|
99
|
+
expect(support.from_env).to eq(true)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "finds ansicon support" do
|
103
|
+
support = described_class.new('ANSICON' => true)
|
104
|
+
expect(support.from_env).to eq(true)
|
105
|
+
end
|
106
|
+
|
107
|
+
it "doesn't find any keys in environment" do
|
108
|
+
support = described_class.new({})
|
109
|
+
expect(support.from_env).to eq(TTY::Color::NoValue)
|
110
|
+
end
|
69
111
|
end
|
70
112
|
end
|
data/tasks/console.rake
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tty-color
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Murach
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -47,8 +47,7 @@ dependencies:
|
|
47
47
|
description: Terminal color capabilities detection
|
48
48
|
email:
|
49
49
|
- ''
|
50
|
-
executables:
|
51
|
-
- color
|
50
|
+
executables: []
|
52
51
|
extensions: []
|
53
52
|
extra_rdoc_files: []
|
54
53
|
files:
|
@@ -60,7 +59,6 @@ files:
|
|
60
59
|
- LICENSE.txt
|
61
60
|
- README.md
|
62
61
|
- Rakefile
|
63
|
-
- bin/color
|
64
62
|
- lib/tty-color.rb
|
65
63
|
- lib/tty/color.rb
|
66
64
|
- lib/tty/color/mode.rb
|
data/bin/color
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
Signal.trap("INT") { exit 1 }
|
4
|
-
|
5
|
-
lib_dir = File.expand_path("../../lib", __FILE__)
|
6
|
-
$LOAD_PATH.unshift(lib_dir) if File.directory?(lib_dir)
|
7
|
-
|
8
|
-
require 'optparse'
|
9
|
-
require 'tty-color'
|
10
|
-
|
11
|
-
options = {}
|
12
|
-
|
13
|
-
parser = OptionParser.new do |opts|
|
14
|
-
opts.on('-s', '--support', 'Check if terminal supports colors') do
|
15
|
-
options[:color] = true
|
16
|
-
end
|
17
|
-
|
18
|
-
opts.on('-m', '--mode', 'Supported colors mode') do
|
19
|
-
options[:mode] = true
|
20
|
-
end
|
21
|
-
|
22
|
-
options[:verbose] = false
|
23
|
-
opts.on('-v', '--verbose', 'Output debug information' ) do
|
24
|
-
options[:verbose] = true
|
25
|
-
end
|
26
|
-
|
27
|
-
opts.on('-h', '--help', 'Display help' ) do
|
28
|
-
puts opts
|
29
|
-
exit
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
parser.parse!
|
34
|
-
|
35
|
-
TTY::Color.verbose = options[:verbose]
|
36
|
-
|
37
|
-
if options[:mode]
|
38
|
-
puts TTY::Color.mode
|
39
|
-
else
|
40
|
-
puts TTY::Color.color?
|
41
|
-
end
|
42
|
-
|