boxer/
lib.rs

1//! A crate for automatically generating boxes.
2//!
3//! They are very cool, and very nice. I'm quite proud of it! This is licensed under the MPL and anyone can use it in their cool projects!
4//!
5//! An example of a box generated by boxer:
6//! ```text
7//! ▛▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▜
8//! ▌  Lorem ipsum dolor sit amet, consectetu  ▐
9//! ▌  r adipiscing elit, sed do eiusmod temp  ▐
10//! ▌  or incididunt ut labore et dolore magn  ▐
11//! ▌  a aliqua. Volutpat consequat mauris nu  ▐
12//! ▌  nc congue nisi vitae. Consectetur adip  ▐
13//! ▌  iscing elit pellentesque habitant. In   ▐
14//! ▌  hac habitasse platea dictumst quisque.  ▐
15//! ▌   Sed blandit libero volutpat sed cras   ▐
16//! ▌  ornare arcu. Metus vulputate eu sceler  ▐
17//! ▌  isque felis imperdiet proin fermentum   ▐
18//! ▌  leo vel. Ullamcorper eget nulla facili  ▐
19//! ▌  si etiam dignissim diam quis. Facilisi  ▐
20//! ▌   cras fermentum odio eu feugiat. Ac au  ▐
21//! ▌  ctor augue mauris augue neque gravida.  ▐
22//! ▌   Ullamcorper velit sed ullamcorper mor  ▐
23//! ▌  bi tincidunt ornare massa eget. Nibh n  ▐
24//! ▌  isl condimentum id venenatis a condime  ▐
25//! ▌  ntum vitae sapien pellentesque. Sollic  ▐
26//! ▌  itudin tempor id eu nisl nunc. Diam si  ▐
27//! ▌  t amet nisl suscipit. Vitae nunc sed v  ▐
28//! ▌  elit dignissim sodales ut eu.           ▐
29//! ▙▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▟
30//! ```
31use clippet::Clippet;
32
33/// A structure for containing border information for boxes.
34#[derive(Debug)]
35pub struct Border {
36	/// Top left glyph
37	pub tl: String, 
38
39	/// Top right glyph
40	pub tr: String,
41
42	/// Bottom left glyph
43	pub bl: String,
44
45	/// Bottom right glyph
46	pub br: String,
47
48	/// Side left glyph
49	pub sl: String,
50
51	/// Side right glyph
52	pub sr: String,
53
54	/// Side top glyph
55	pub st: String,
56
57	/// Side bottom glyph
58	pub sb: String,
59
60	/// Extra padding for top and bottom
61	pub xtra: u32, 
62
63	/// Extra padding for sides
64	pub over: u32, 
65}
66
67/// Generators for the Border struct.
68impl Border {
69	/// Takes in a category and spits out a templated Border structure.
70	/// Available templates include receipt, hashes, blocky and solid.
71	/// ```no_run
72	/// let x = Border::template("solid");
73	/// println!("{}", makebox(1, 1, 40, x, "Hello, world!"));
74	/// ```
75	pub fn template(cat: &str) -> Border {
76		match cat {
77			"empty" => Border { tl: "".to_string(), tr: "".to_string(), bl: "".to_string(), br: "".to_string(), sl: "".to_string(), sr: "".to_string(), st: "".to_string(), sb: "".to_string(), xtra: 0, over: 3 },
78			"hashes" => Border { tl: "#".to_string(), tr: "#".to_string(), bl: "#".to_string(), br: "#".to_string(), sl: "#".to_string(), sr: "#".to_string(), st: "#".to_string(), sb: "#".to_string(), xtra: 0, over: 1 },
79			"sshalert" | "ats" => Border { tl: "@".to_string(), tr: "@".to_string(), bl: "@".to_string(), br: "@".to_string(), sl: "@".to_string(), sr: "@".to_string(), st: "@".to_string(), sb: "@".to_string(), xtra: 0, over: 1 },
80			"stars" | "receipt" => Border { tl: "*".to_string(), tr: "*".to_string(), bl: "*".to_string(), br: "*".to_string(), sl: "*".to_string(), sr: "*".to_string(), st: "*".to_string(), sb: "*".to_string(), xtra: 0, over: 1 },
81			"solid" => Border { tl: "█".to_string(), tr: "█".to_string(), bl: "█".to_string(), br: "█".to_string(), sl: "█".to_string(), sr: "█".to_string(), st: "█".to_string(), sb: "█".to_string(), xtra: 0, over: 3 },
82			"lines" => Border { tl: "/".to_string(), tr: "\\".to_string(), bl: "\\".to_string(), br: "/".to_string(), sl: "|".to_string(), sr: "|".to_string(), st: "-".to_string(), sb: "-".to_string(), xtra: 2, over: 3 },
83			"blocky" | _ => Border { tl: "▛".to_string(), tr: "▜".to_string(), bl: "▙".to_string(), br: "▟".to_string(), sl: "▌".to_string(), sr: "▐".to_string(), st: "▀".to_string(), sb: "▄".to_string(), xtra: 0, over: 3 }, // Blocky
84		}
85	}
86	/// Outputs a blank Border structure for creating custom borders.
87	///
88	/// ```no_run
89	/// let x = Border::new();
90	/// ```
91	pub fn new() -> Border {
92		return Border::template("empty")
93	}
94}
95
96/// Takes in cosmetic information, a Border structure and some text and turns it into a box in a String.
97/// ```no_run
98/// let x = Border::template("receipt");
99/// let y = makebox(1, 1, 40, x, "Hello, world!");
100/// println!("{}", y);
101/// ```
102#[allow(unused_assignments)]
103pub fn makebox(spacing: u32, padding: u32, columns: u32, borders: Border, text: &str) -> String {
104	let txt = text.to_string();
105	let mut top = borders.tl;
106	let mut bot = borders.bl;
107	let mut i: i32 = spacing as i32 + columns as i32;
108	let mut pad = borders.sl.to_string();
109	let mut fins = "".to_string();
110	let over = borders.over;
111
112	if padding > 0 {
113		while i > -1 {
114			top = format!("{}{}", top, borders.st);
115			bot = format!("{}{}", bot, borders.sb);
116			pad = format!("{}{}", pad, " ");
117			i = i - 1
118		}
119	} else {
120		while i > -1 {
121			top = format!("{}{}", top, borders.st);
122			bot = format!("{}{}", bot, borders.sb);
123			i = i - 1
124		}
125	}
126	i = borders.xtra as i32;
127	while i > 0 {
128		top = format!("{}{}", top, borders.st);
129		bot = format!("{}{}", bot, borders.sb);
130		i = i - 1
131	}
132	top = format!("{}{}", top, borders.tr);
133	bot = format!("{}{}", bot, borders.br);
134
135	let mut pads: i32 = -1;
136	fins = top;
137
138	if padding > 0 {
139		while pad.len() < (spacing + columns + 1 + over) as usize {
140			pad = format!("{}{}", pad, " ");
141		}
142		while pad.len() > (spacing + columns + 1 + over) as usize {
143			pad.pop();
144		}
145		pad = format!("{}{}", pad, borders.sr);
146		pads = padding as i32;
147	}
148	while pads > 0 {
149		fins = format!("{}\n{}", fins, pad);
150		pads = pads - 1;
151	}
152
153	let spliz = Clippet::from_string(&txt, columns as usize - 1 as usize - spacing as usize);
154	let mut columnz = 0;
155	while spliz.len() > columnz {
156		let x = spliz.get_as_string(columnz as usize);
157		let mut y = borders.sl.to_string();
158		let mut s: i32 = spacing as i32;
159		while s > -1 {
160			y = format!("{}{}", y, " ");
161			s = s - 1
162		}
163		y = format!("{}{}", y, x);
164		let mut s: i32 = spacing as i32;
165		while s > -1 {
166			y = format!("{}{}", y, " ");
167			s = s - 1
168		}
169		while y.len() < (spacing + columns + 1 + over) as usize {
170			y = format!("{}{}", y, " ");
171		}
172		while y.len() > (spacing + columns + 1 + over) as usize {
173			y.pop();
174		}
175		fins = format!("{}\n{}{}", fins, y.to_string(), borders.sr.to_string());
176		columnz = columnz + 1;
177	}
178
179	if padding > 0 {
180		pads = padding as i32;
181	}
182	while pads > 0 {
183		fins = format!("{}\n{}", fins, pad);
184		pads = pads - 1;
185	}
186	fins = format!("{}\n{}", fins, bot);
187	return fins
188}
189
190/// Makes a new box with the blocky borders without having to choose the spacing, padding, columns or borders.
191/// ```no_run
192/// let y = rush("Hello, world!");
193/// println!("{}", y);
194/// ```
195pub fn rush(text: &str) -> String {
196	let borders = Border::template("blocky");
197	let x = makebox(1, 0, 40, borders, text);
198	return x
199}