-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.qdsd-svgtascii.erb
142 lines (118 loc) · 3.6 KB
/
ai.qdsd-svgtascii.erb
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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Require the ERB library
require 'erb'
# Define a class to represent a device
class Device
attr_accessor :name, :ip, :status
def initialize(name, ip, status)
@name = name
@ip = ip
@status = status
end
end
# Define a class to represent a server
class Server
attr_accessor :name, :ip, :load, :memory
def initialize(name, ip, load, memory)
@name = name
@ip = ip
@load = load
@memory = memory
end
end
# Create some sample devices and servers
devices = [
Device.new("Router", "192.168.1.1", "Online"),
Device.new("Switch", "192.168.1.2", "Offline"),
Device.new("Printer", "192.168.1.3", "Online")
]
servers = [
Server.new("Web", "10.0.0.1", "50%", "4 GB"),
Server.new("Database", "10.0.0.2", "80%", "8 GB"),
Server.new("Mail", "10.0.0.3", "20%", "2 GB")
]
# Define a method to draw a horizontal line with ASCII art
def draw_line(length)
"+" + "-" * length + "+"
end
# Define a method to draw a cell with ASCII art
def draw_cell(content, length)
"|" + content.ljust(length) + "|"
end
# Define a method to draw a table with ASCII art
def draw_table(data, headers, length)
# Draw the top line
puts draw_line(length)
# Draw the header row
headers.each do |header|
print draw_cell(header, length / headers.size)
end
puts
# Draw the separator line
puts draw_line(length)
# Draw the data rows
data.each do |row|
row.each do |cell|
print draw_cell(cell, length / row.size)
end
puts
end
# Draw the bottom line
puts draw_line(length)
end
# Define a method to create a cell with SVG
def create_cell(content, x, y, width, height)
# Create a rectangle element
rect = "<rect x='#{x}' y='#{y}' width='#{width}' height='#{height}' fill='white' stroke='black'/>"
# Create a text element
text = "<text x='#{x + width / 2}' y='#{y + height / 2}' text-anchor='middle' dominant-baseline='central'>#{content}</text>"
# Return the SVG elements
rect + text
end
# Define a method to create a table with SVG
def create_table(data, headers, width, height)
# Calculate the cell size
cell_width = width / headers.size
cell_height = height / (data.size + 1)
# Create an array to store the SVG elements
svg = []
# Create the header cells
headers.each_with_index do |header, i|
svg << create_cell(header, i * cell_width, 0, cell_width, cell_height)
end
# Create the data cells
data.each_with_index do |row, j|
row.each_with_index do |cell, i|
svg << create_cell(cell, i * cell_width, (j + 1) * cell_height, cell_width, cell_height)
end
end
# Return the SVG elements as a string
svg.join("\n")
end
# Define an ERB template to embed Ruby code in HTML
template = <<-HTML
<!DOCTYPE html>
<html>
<head>
<title>Device and Server Table</title>
</head>
<body>
<h1>Device and Server Table</h1>
<p>This is an example of using ERB to embed Ruby code in HTML.</p>
<p>The table below is generated by querying a device and server database and displaying the results with SVG and ASCII art.</p>
<div id="svg-table">
<svg width="500" height="200" xmlns="http://www.w3.org/2000/svg">
<%= create_table(devices.map { |d| [d.name, d.ip, d.status] } + servers.map { |s| [s.name, s.ip, s.load, s.memory] }, ["Name", "IP", "Status", "Memory"], 500, 200) %>
</svg>
</div>
<div id="ascii-table">
<pre>
<%= draw_table(devices.map { |d| [d.name, d.ip, d.status] } + servers.map { |s| [s.name, s.ip, s.load, s.memory] }, ["Name", "IP", "Status", "Memory"], 40) %>
</pre>
</div>
</body>
</html>
HTML
# Create an ERB object from the template
erb = ERB.new(template)
# Print the result of the ERB evaluation
puts erb.result