artsy 0.1.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e36f8972ca13ddf1717d6a715a0bb2f6a53394f5c1c74366a6de39f52becc640
4
- data.tar.gz: ed102c08091538dc5393c2f26c504c03c3733a0c533fdabc8013c4ec7d7df7bc
3
+ metadata.gz: 14db637c153d6dce542eacc55e154dc2dd3a6135894370a4566dd3121e659b95
4
+ data.tar.gz: 1562e5f4683a7fddc5312e36a48badf3e2e853a4b932b74e99da3dd41849455a
5
5
  SHA512:
6
- metadata.gz: 132c65d1b7f53b342b2ce2cf32a1405f0fe0c1be5f43356352c91e9fd883824e39e80eb2c68002ebc6f8f7cc54555f467065116f695ed59fce9b9726f9bdca70
7
- data.tar.gz: 51d37b4dd0b392425f4eeb3f2ed30b688dcfe97fd86878f8da3cca65dc263b3195fceef075fe5230d4fe0ea2434c51341e2e26629d0f2b371d4294f39232856a
6
+ metadata.gz: c38b8fbf673f19350da3b1caef2c49ad41dcc39ed67dc6d1f5a871d1dae562e224dd2e02f164c74bcdbf9e2ee86cfd8c7073b517cd5138056cd31831c2dd95a3
7
+ data.tar.gz: 3911cdd115b9f8ab118b1d312793c95f8f84c00605efc8be601ccfb865ac4910a33c874fe8210fc99c7172958e32a5dde77e1b26a9427da1061867f05ddf7fe5
data/lib/artsy.rb CHANGED
@@ -7,6 +7,7 @@
7
7
 
8
8
  class Artsy
9
9
  # main class
10
+ require 'artsy/devel/handler'
10
11
  def self.out(text, colortype, r = 0, g = 0, b = 0, color: "none", formatting: "none")
11
12
  definedColours = ["\033[0m", "\033[31m", "\033[32m", "\033[93m", "\033[91m", "\033[92m", "\033[96m"]
12
13
  # print colored/formatted text (either from the preset list or with truecolors)
@@ -15,7 +16,7 @@ class Artsy
15
16
  # print with regular colors
16
17
  if color == "none"
17
18
  # fail if color undefined
18
- raise StandardError.new "Invalid color defined (#{color})."
19
+ raise ArgumentError.new "Invalid color defined (#{color})."
19
20
  elsif color == "red"
20
21
  # printcolor = red
21
22
  printcolor = "#{definedColours[1]}"
@@ -31,19 +32,95 @@ class Artsy
31
32
  printcolor = "#{definedColours[6]}"
32
33
  else
33
34
  # fail
34
- raise StandardError.new "Invalid color defined (#{color})."
35
+ raise ArgumentError.new "Invalid color defined (#{color})."
35
36
  end
36
37
  elsif colortype == "true"
38
+ # before doing anything, ensure all rgb values are within range
39
+ ArtsyDev::Handler.rgbOutOfRange("strict", r, g, b) # calls the handler check and passes rgb
37
40
  # print with rgb
38
41
  printcolor = "\033[38;2;#{r};#{g};#{b}m"
39
42
  else
40
43
  # fail
41
- raise StandardError.new "Invalid colortype defined (#{colortype})."
44
+ raise ArgumentError.new "Invalid colortype defined (#{colortype})."
42
45
  end
43
- if formatting == "none"
44
- puts "#{printcolor}#{text}#{definedColours[0]}" # done!
45
- elsif formatting == "bold"
46
- puts "#{printcolor}\033[1m#{text}#{definedColours[0]}"
46
+ case formatting
47
+ when "none"
48
+ puts "#{printcolor}#{text}#{definedColours[0]}" # done!
49
+ when "bold"
50
+ puts "#{printcolor}\033[1m#{text}#{definedColours[0]}"
51
+ when "underline", "ul"
52
+ puts "#{printcolor}\033[4m#{text}#{definedColours[0]}"
53
+ when "italics"
54
+ # WARNING: Not widely supported by terminal emulators.
55
+ puts "#{printcolor}\033[3m#{text}#{definedColours[0]}"
56
+ else
57
+ raise ArgumentError.new "Invalid formatting option set (#{formatting})."
58
+ end
59
+ end
60
+ def self.gradientOut(text, r1, g1, b1, r2, g2, b2, stepping, debug = 0)
61
+ # before doing anything, check both rgb sets
62
+ ArtsyDev::Handler.rgbOutOfRange("strict", r1, g1, b1)
63
+ ArtsyDev::Handler.rgbOutOfRange("strict", r2, g2, b2)
64
+ # prints text with a gradient
65
+ # increases every value by stepping per character
66
+ arr_Text = text.split("") # split text into array of characters
67
+ if debug == 1
68
+ # show array
69
+ puts arr_Text
70
+ end
71
+ count = arr_Text.count
72
+ if debug == 1
73
+ puts count
74
+ end
75
+ count_cap = count - 1
76
+ cr = r1
77
+ cg = g1
78
+ cb = b1
79
+ for i in 0..count_cap
80
+ # iterate over characters
81
+ if r1 < r2
82
+ # if less than r2, +step and check nr accordingly
83
+ nr = cr + stepping
84
+ if nr < r2
85
+ # as long as less than r2, increment by stepping
86
+ cr = nr # cr is now nr
87
+ end
88
+ else
89
+ # -step and check nr differently
90
+ nr = cr - stepping
91
+ if nr > r2
92
+ cr = nr
93
+ end
94
+ end
95
+ if g1 < g2
96
+ ng = cg + stepping
97
+ if ng < g2
98
+ # green
99
+ cg = ng
100
+ end
101
+ else
102
+ ng = cg - stepping
103
+ if ng > g2
104
+ cg = ng
105
+ end
106
+ end
107
+ if b1 < b2
108
+ nb = cb + stepping
109
+ if nb < b2
110
+ cb = nb
111
+ end
112
+ else
113
+ nb = cb - stepping
114
+ if nb > b2
115
+ cb = nb
116
+ end
117
+ end
118
+ # in conclusion, don't increase if next color exceeds endpoint
119
+ print "\033[38;2;#{cr};#{cg};#{cb}m#{arr_Text[i]}\033[0m"
47
120
  end
121
+ print "\n" # print a newline
48
122
  end
49
123
  end
124
+
125
+ # load any other gem components
126
+ require 'artsy/menu'
@@ -0,0 +1,40 @@
1
+ class ArtsyDev
2
+ # parent class as a namespace
3
+ class Handler
4
+ # subclass: contains some preset argumenterror cases
5
+ def self.rgbOutOfRange(mode, r, g, b)
6
+ # HACK: Change mode in the script to change how your artsy install deals with errors.
7
+ if mode == "strict"
8
+ # strict: raise an argument error exception without catching
9
+ limit = 255 # set limit
10
+ unless r <= limit and r >= 0
11
+ # fail if r exceeds limit or if lower than 0.
12
+ raise ArgumentError.new "'r' value out of range. Must be between 0-255 (you entered #{r})."
13
+ end
14
+ unless g <= limit and g >= 0
15
+ # fail for green
16
+ raise ArgumentError.new "'g' value out of range. Must be between 0-255 (you entered #{g})."
17
+ end
18
+ unless b <= limit and b >= 0
19
+ # fail for blue
20
+ raise ArgumentError.new "'b' value out of range. Must be between 0-255 (you entered #{b})."
21
+ end
22
+ elsif mode == "chill"
23
+ # chill: Print a warning message to stderr, but don't raise exception.
24
+ limit = 255
25
+ unless r <= limit and r >= 0
26
+ # warn for red
27
+ STDERR.puts "WARNING: red value out of range... (#{r})"
28
+ end
29
+ unless g <= limit and g >= 0
30
+ # warn for green
31
+ STDERR.puts "WARNING: green value out of range... (#{g})"
32
+ end
33
+ unless b <= limit and b >= 0
34
+ # warn for blue
35
+ STDERR.puts "WARNING: blue value out of range... (#{b})"
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
data/lib/artsy/menu.rb ADDED
@@ -0,0 +1,72 @@
1
+ ########################################################################
2
+ ### menu.rb ###
3
+ ########################################################################
4
+ ### Allows you to easily create menus in your terminal programs. ###
5
+ ### Developed by Brett. (https://github.com/notronaldmcdonald) ###
6
+ ########################################################################
7
+
8
+ class Menu
9
+ def initialize(title, opts = 3, selectMode = "int")
10
+ # you can set title and no. of opts for your menu object
11
+ @menutitle = title
12
+ @optstrings = Array.new(opts) # create an array for each option
13
+ @optids = Array.new(opts)
14
+ maxopts = opts - 1 # for use in the loop
15
+ for i in 0..maxopts
16
+ # cycle through the number of options, generating a default string for each
17
+ @optstrings[i] = "Option #{i}"
18
+ @optids[i] = i + 1
19
+ end # end of loop
20
+ @methodLoader = Linkage.new(@optids.count) # create an object, passing all necessary arguments to it
21
+ end # end of method
22
+
23
+ def setOpt(index, value)
24
+ # set the value of an option
25
+ index_real = index - 1
26
+ @optstrings[index_real] = "#{value}"
27
+ end
28
+
29
+ def show()
30
+ # print menu
31
+ puts "#{@menutitle}"
32
+ puts ""
33
+ for i in 1..@optids.count
34
+ # print each option
35
+ n = i - 1 # to access strings via index
36
+ puts "#{i}) #{@optstrings[n]}" # print the option
37
+ end
38
+ puts ""
39
+ puts "Enter your selection: "
40
+ @choice = gets.chomp
41
+ puts "You entered #{@choice}."
42
+ # now, we'll run some logic to ensure the choice was in @optids, then we can run the user's code!
43
+ @choice = Integer(@choice) # make sure it is int
44
+ @choice = @choice - 1 # convert to real
45
+ @methodLoader.run(@choice)
46
+ end # end of method
47
+
48
+ def assign(option, mname)
49
+ # a wrapper function to assign a method to an option
50
+ trueOpt = option - 1 # do some math
51
+ @methodLoader.bindMethod(trueOpt, mname)
52
+ end
53
+
54
+ class Linkage
55
+ # this private class contains functions for binding functions to an optid
56
+ def initialize(optsize)
57
+ # initializing the object, setting up the callbackMethods instance array based on optsize
58
+ @callbackMethods = Array.new(optsize)
59
+ end # simple method
60
+
61
+ def bindMethod(toOpt, mname)
62
+ # binding methods to a value in an array, to be called back later
63
+ @callbackMethods[toOpt] = mname
64
+ end
65
+
66
+ def run(target, *args)
67
+ # run the method assigned to the integer value in the array
68
+ @callbackMethods[target].call(*args) # taking any arguments, as they are sent to the attached method
69
+ end
70
+ end # end of private class
71
+ private_constant :Linkage # set as private constant
72
+ end # end of class
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: artsy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-12 00:00:00.000000000 Z
11
+ date: 2021-05-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Enables you to make good looking terminal programs with colors and text
14
14
  formatting.
@@ -18,6 +18,8 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/artsy.rb
21
+ - lib/artsy/devel/handler.rb
22
+ - lib/artsy/menu.rb
21
23
  homepage: https://github.com/notronaldmcdonald/artsy
22
24
  licenses:
23
25
  - MIT