Class: ArTTY::Art

Inherits:
Object
  • Object
show all
Defined in:
lib/arTTY/art.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Art

Returns a new instance of Art.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/arTTY/art.rb', line 88

def initialize(file = nil)
    if (!file.nil?)
        @file = Pathname.new(file).expand_path
        @json = JSON.parse(File.read(@file))
    else
        @file = nil
        @json = Hash.new
    end

    @debug = false
    @legend = @json["legend"] || Hash.new
    @name = @json["name"] || "none"
    @pixels = @json["pixels"] || Array.new
    @sysinfo = nil
end

Instance Attribute Details

#legendObject

Returns the value of attribute legend.



7
8
9
# File 'lib/arTTY/art.rb', line 7

def legend
  @legend
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/arTTY/art.rb', line 8

def name
  @name
end

#pixelsObject (readonly)

Returns the value of attribute pixels.



6
7
8
# File 'lib/arTTY/art.rb', line 6

def pixels
  @pixels
end

#sysinfoObject

Returns the value of attribute sysinfo.



9
10
11
# File 'lib/arTTY/art.rb', line 9

def sysinfo
  @sysinfo
end

Instance Method Details

#drawObject



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
81
82
# File 'lib/arTTY/art.rb', line 11

def draw
    offset = nil
    out = ""
    pixels = @pixels
    sysinfo = Array.new
    if (@sysinfo)
        offset = @pixels[0] ? @pixels[0].length + 2 : 0
        sysinfo = @sysinfo.info
    end

    if ((pixels.length % 2) != 0)
        filler = " " * pixels[0].length
        pixels.insert(0, filler)
    end

    while (!pixels.empty?)
        out += " "
        top = pixels.delete_at(0)
        bottom = pixels.delete_at(0)

        top += " " while (top.length < bottom.length)
        bottom += " " while (top.length > bottom.length)

        top.chars.zip(bottom.chars).each_with_index do |map, i|
            t = map[0].strip
            b = map[1].strip

            if (
                (t.empty? && b.empty?) ||
                (!t.empty? && !@legend.include?(t)) ||
                (!b.empty? && !@legend.include?(b))
            )
                out += " "
                next
            end

            if (t.empty?)
                c = "".send(@legend[b])
            elsif (b.empty?)
                c = "".send(@legend[t]).swap
            else
                c = "".send(@legend[b]).send("on_#{@legend[t]}")
            end

            out += c
        end

        if (offset)
            info = sysinfo.delete_at(0)
            if (info)
                filler = offset - top.length
                out += " " * filler if (filler > 0)
                out += info
            else
                offset = nil
            end
        end

        out += "\n"
    end

    if (offset)
        loop do
            info = sysinfo.delete_at(0)
            break if (info.nil?)
            out += " " * (offset + 1)
            out += "#{info}\n"
        end
    end

    return out
end

#heightObject



84
85
86
# File 'lib/arTTY/art.rb', line 84

def height
    return @json["height"]
end

#to_sObject



104
105
106
# File 'lib/arTTY/art.rb', line 104

def to_s
    return draw
end

#widthObject



108
109
110
# File 'lib/arTTY/art.rb', line 108

def width
    return @json["width"]
end