Starting out and tooling around
Initial commit with some general finger painting. I'll be hacking on this in a terrible order and just doing things that seem interesting to me at the time. I kinda-sorta have a general idea of what the end product would look like; but this is just an exercise in messing around with Rust and trying to do some actual programming work that I find remotely interesting. This long commit message is here to remind me down the road of the origins and how far I've come. Hopefully future me is reading this as a reasonable skilled Rustacean with a fun little game to play.
This commit is contained in:
119
src/main.rs
Normal file
119
src/main.rs
Normal file
@@ -0,0 +1,119 @@
|
||||
use rand::prelude::*;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
roll_dice(6);
|
||||
|
||||
let content = vec![
|
||||
"This is some content here",
|
||||
"We'll have all kinds of stuff to write",
|
||||
"Many things will be here for you to see",
|
||||
"However, we need to make much more progress",
|
||||
"I'm sure this won't end well",
|
||||
];
|
||||
|
||||
draw_box(50, content);
|
||||
}
|
||||
|
||||
fn roll_dice(sides: u8) {
|
||||
let mut roll: u8 = random();
|
||||
roll = roll % sides;
|
||||
|
||||
// make sure we never roll a zero; that's not possible
|
||||
if roll < 1 {
|
||||
roll += 1;
|
||||
}
|
||||
|
||||
println!("Rolling dice with {} sides: {}", sides, roll);
|
||||
}
|
||||
|
||||
fn draw_box(width: u8, content: Vec<&str>) {
|
||||
let mut text_box: Vec<String> = Vec::new();
|
||||
|
||||
text_box.push(top_border(width, "HEALTH AND VITALS"));
|
||||
for line in content {
|
||||
text_box.push(create_row(width, '\u{2502}', line));
|
||||
}
|
||||
text_box.push(bottom_border(width));
|
||||
|
||||
for n in text_box {
|
||||
println!("{}", n);
|
||||
}
|
||||
}
|
||||
|
||||
fn create_row(width: u8, border: char, content: &str) -> String {
|
||||
let mut row_chars: Vec<char> = Vec::new();
|
||||
|
||||
row_chars.push(border);
|
||||
|
||||
let mut index = 1;
|
||||
for c in content.chars() {
|
||||
row_chars.push(c);
|
||||
index += 1;
|
||||
|
||||
if index == width - 1 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if index < width - 1 {
|
||||
for _c in index..width - 1 {
|
||||
row_chars.push(' ');
|
||||
}
|
||||
}
|
||||
|
||||
row_chars.push(border);
|
||||
|
||||
let row: String = row_chars.into_iter().collect();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
fn top_border(width: u8, title: &str) -> String {
|
||||
let mut row = String::new();
|
||||
|
||||
row.push('\u{250c}'); // top left corner
|
||||
let mut index = 1; // we have one char in the border now
|
||||
|
||||
if title != "" {
|
||||
// if the requested title is longer than the top border, truncate it
|
||||
// we specify "width - 2" here to account for the corner pieces
|
||||
if title.len() > (width - 2).into() {
|
||||
for c in title.chars() {
|
||||
row.push(c);
|
||||
index += 1;
|
||||
if index == width - 2 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for c in title.chars() {
|
||||
row.push(c);
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _n in index..width - 1 {
|
||||
row.push('\u{2500}');
|
||||
}
|
||||
|
||||
row.push('\u{2510}'); // top right corner
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
fn bottom_border(width: u8) -> String {
|
||||
let mut row = String::new();
|
||||
|
||||
row.push('\u{2514}'); // bottom left corner
|
||||
|
||||
for _n in 1..width - 1 {
|
||||
row.push('\u{2500}');
|
||||
}
|
||||
|
||||
row.push('\u{2518}'); // bottom right corner
|
||||
|
||||
return row;
|
||||
}
|
||||
Reference in New Issue
Block a user