File: chain.rb

package info (click to toggle)
ruby-ansi 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 404 kB
  • sloc: ruby: 1,880; makefile: 5
file content (50 lines) | stat: -rw-r--r-- 640 bytes parent folder | download | duplicates (4)
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
require 'ansi/code'

module ANSI

  # ANSI::Chain was inspired by Kazuyoshi Tlacaelel's Isna library.
  #
  class Chain

    #
    def initialize(string)
      @string = string.to_s
      @codes  = []
    end

    #
    attr :string

    #
    attr :codes

    #
    def method_missing(s, *a, &b)
      if ANSI::CHART.key?(s)
        @codes << s
        self
      else
        super(s, *a, &b)
      end
    end

    #
    def to_s
      if codes.empty?
        result = @string
      else
        result = Code.ansi(@string, *codes)
        codes.clear
      end
      result
    end

    #
    def to_str
      to_s
    end

  end

end