Class: Artsy

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

Overview

artsy.rb ###

Make your Ruby terminal programs look nice!        ###

Developed by Brett (github.com/notronaldmcdonald) ###

Licensed under the GNU LGPLv3 license. See the LICENSE in ###

the project root for more information.          ###

Class Method Summary collapse

Class Method Details

.gradientOut(text, r1, g1, b1, r2, g2, b2, stepping, debug = 0) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/artsy.rb', line 63

def self.gradientOut(text, r1, g1, b1, r2, g2, b2, stepping, debug = 0)
  # before doing anything, check both rgb sets
  ArtsyDev::Handler.rgbOutOfRange("strict", r1, g1, b1)
  ArtsyDev::Handler.rgbOutOfRange("strict", r2, g2, b2)
  # prints text with a gradient
  # increases every value by stepping per character
  arr_Text = text.split("") # split text into array of characters
  if debug == 1
    # show array
    puts arr_Text
  end
  count = arr_Text.count
  if debug == 1
    puts count
  end
  count_cap = count - 1
  cr = r1
  cg = g1
  cb = b1
  for i in 0..count_cap
    # iterate over characters
    if r1 < r2
      # if less than r2, +step and check nr accordingly
      nr = cr + stepping
      if nr < r2
        # as long as less than r2, increment by stepping
        cr = nr # cr is now nr
      end
    else
      # -step and check nr differently
      nr = cr - stepping
      if nr > r2
        cr = nr
      end
    end
    if g1 < g2
      ng = cg + stepping
      if ng < g2
        # green
        cg = ng
      end
    else
      ng = cg - stepping
      if ng > g2
        cg = ng
      end
    end
    if b1 < b2
      nb = cb + stepping
      if nb < b2
        cb = nb
      end
    else
      nb = cb - stepping
      if nb > b2
        cb = nb
      end
    end
    # in conclusion, don't increase if next color exceeds endpoint
    print "\033[38;2;#{cr};#{cg};#{cb}m#{arr_Text[i]}\033[0m"
  end
  print "\n" # print a newline
end

.out(text, colortype, r = 0, g = 0, b = 0, color: "none", formatting: "none") ⇒ Object



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
# File 'lib/artsy.rb', line 14

def self.out(text, colortype, r = 0, g = 0, b = 0, color: "none", formatting: "none")
  definedColours = ["\033[0m", "\033[31m", "\033[32m", "\033[93m", "\033[91m", "\033[92m", "\033[96m"]
  # print colored/formatted text (either from the preset list or with truecolors)
  # you MUST specify if you're printing truecolor or not
  if colortype == "normal"
    # print with regular colors
    if color == "none"
      # fail if color undefined
      raise ArgumentError.new "Invalid color defined (#{color})."
    elsif color == "red"
      # printcolor = red
      printcolor = "#{definedColours[1]}"
    elsif color == "green"
      printcolor = "#{definedColours[2]}"
    elsif color == "yellow"
      printcolor = "#{definedColours[3]}"
    elsif color == "failureRed"
      printcolor = "#{definedColours[4]}"
    elsif color == "successGreen"
      printcolor = "#{definedColours[5]}"
    elsif color == "cyan"
      printcolor = "#{definedColours[6]}"
    else
      # fail
      raise ArgumentError.new "Invalid color defined (#{color})."
    end
  elsif colortype == "true"
    # before doing anything, ensure all rgb values are within range
    ArtsyDev::Handler.rgbOutOfRange("strict", r, g, b) # calls the handler check and passes rgb
    # print with rgb
    printcolor = "\033[38;2;#{r};#{g};#{b}m"
  else
    # fail
    raise ArgumentError.new "Invalid colortype defined (#{colortype})."
  end
  case formatting
    when "none"
      puts "#{printcolor}#{text}#{definedColours[0]}" # done!
    when "bold"
      puts "#{printcolor}\033[1m#{text}#{definedColours[0]}"
    when "underline", "ul"
      puts "#{printcolor}\033[4m#{text}#{definedColours[0]}"
    when "italics"
      # WARNING: Not widely supported by terminal emulators.
      puts "#{printcolor}\033[3m#{text}#{definedColours[0]}"
    else
      raise ArgumentError.new "Invalid formatting option set (#{formatting})."
  end
end