File: color.rb

package info (click to toggle)
ruby-tty-color 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 180 kB
  • sloc: ruby: 450; sh: 4; makefile: 4
file content (80 lines) | stat: -rw-r--r-- 1,550 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# frozen_string_literal: true

require_relative "color/mode"
require_relative "color/support"
require_relative "color/version"

module TTY
  # Responsible for checking terminal color support
  #
  # @api public
  module Color
    extend self

    NoValue = Module.new

    @verbose = false

    @output = $stderr

    attr_accessor :output, :verbose

    # Check if terminal supports colors
    #
    # @return [Boolean]
    #
    # @api public
    def support?
      Support.new(ENV, verbose: verbose).support?
    end
    alias supports? support?
    alias color? support?
    alias supports_color? support?

    # Detect if color support has been disabled with NO_COLOR ENV var.
    #
    # @return [Boolean]
    #   true when terminal color support has been disabled, false otherwise
    #
    # @api public
    def disabled?
      Support.new(ENV, verbose: verbose).disabled?
    end

    # Check how many colors this terminal supports
    #
    # @return [Integer]
    #
    # @api public
    def mode
      Mode.new(ENV).mode
    end

    # Check if output is linked with terminal
    #
    # @return [Boolean]
    #
    # @api public
    def tty?
      output.respond_to?(:tty?) && output.tty?
    end

    # Check if command can be run
    #
    # @return [Boolean]
    #
    # @api public
    def command?(cmd)
      !!system(cmd, out: ::File::NULL, err: ::File::NULL)
    end

    # Check if Windowz
    #
    # @return [Boolean]
    #
    # @api public
    def windows?
      ::File::ALT_SEPARATOR == "\\"
    end
  end # Color
end # TTY