Skip to content

Commit

Permalink
v0.1.7
Browse files Browse the repository at this point in the history
testing
  • Loading branch information
Localghost385 committed May 28, 2024
1 parent 3ff4f72 commit 13084fe
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 28 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

### v0.1.7
- minor changes and improved test coverage.

### v0.1.6
- added option to have a string preceding each line, for example to comment it out.

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "glyphrs"
version = "0.1.6"
version = "0.1.7"
edition = "2021"
license = "MIT"
description = "A text art generator written in Rust"
Expand Down
48 changes: 42 additions & 6 deletions src/fonts/font_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct Outer {
#[derive(Debug, Deserialize)]
struct Font {
name: String,
character: Vec<char>,
character: Vec<String>,
key_values: Vec<Vec<String>>,
}

Expand All @@ -21,6 +21,9 @@ pub fn get_fonts() -> Vec<String> {
font_list.push(keys);
}

// make sure the fonts are in alphabetical order
font_list.sort();

font_list
}

Expand All @@ -31,20 +34,53 @@ fn get_font_file() -> &'static str {
contents
}

pub fn define_fonts() -> HashMap<String, HashMap<char, Vec<String>>> {
let mut fonts: HashMap<String, HashMap<char, Vec<String>>> = HashMap::new();
pub fn define_fonts() -> HashMap<String, HashMap<String, Vec<String>>> {
let mut fonts: HashMap<String, HashMap<String, Vec<String>>> = HashMap::new();
let contents = get_font_file();
let toml_value: Outer = toml::from_str(contents).expect("Failed to parse config.toml");

for font in toml_value.fonts {
let mut font_map: HashMap<char, Vec<String>> = HashMap::new();
let mut font_map: HashMap<String, Vec<String>> = HashMap::new();
for i in 0..font.character.len() {
let key = font.character[i];
let key = &font.character[i];
let value = &font.key_values[i];
font_map.insert(key, value.to_vec());
font_map.insert(key.to_string(), value.to_vec());
}

fonts.insert(font.name, font_map);
}
fonts
}

#[cfg(test)]
pub mod tests {
use std::collections::HashMap;

use crate::fonts::font_handling::{define_fonts, get_font_file, get_fonts, Outer};

#[test]
fn test_get_fonts() {
let expected = vec!["blocks_in_two_lines".to_string(), "pipes".to_string()];
assert_eq!(get_fonts(), expected);
}

#[test]
fn test_define_fonts() {
let contents = get_font_file();
let toml_value: Outer = toml::from_str(contents).expect("Failed to parse config.toml");

let mut expected: HashMap<String, HashMap<String, Vec<String>>> = HashMap::new();
for font in toml_value.fonts {
let mut font_map: HashMap<String, Vec<String>> = HashMap::new();
for i in 0..font.character.len() {
let key = &font.character[i];
let value = &font.key_values[i];
font_map.insert(key.to_string(), value.to_vec());
}

expected.insert(font.name, font_map);
}

assert_eq!(define_fonts(), expected);
}
}
81 changes: 61 additions & 20 deletions src/message_gen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use crate::fonts::font_handling::define_fonts;

pub fn sanitize_input(input: String) -> String {
Expand All @@ -11,7 +10,7 @@ pub fn sanitize_input(input: String) -> String {
output
}

pub fn map_search(key: char, font: &str) -> Vec<String> {
pub fn map_search(key: String, font: &str) -> Vec<String> {
let fonts = define_fonts();

let map = fonts.get(font).unwrap().clone();
Expand All @@ -21,7 +20,7 @@ pub fn map_search(key: char, font: &str) -> Vec<String> {

value
}
fn conserve_spaces(input: Vec<String>) -> Vec<String> {
fn remove_blank_lines(input: Vec<String>) -> Vec<String> {
let mut output: Vec<String> = vec![];

for line in input {
Expand All @@ -43,8 +42,8 @@ pub fn string_composite(characters: Vec<Vec<String>>, prefix: String) -> Vec<Str
output.push(String::new());
}

for i in 0..output.len() {
output[i] = prefix.to_string();
for line in output.iter_mut() {
line.clone_from(&prefix);
}

for character in characters {
Expand All @@ -53,7 +52,7 @@ pub fn string_composite(characters: Vec<Vec<String>>, prefix: String) -> Vec<Str
}
}

output = conserve_spaces(output);
output = remove_blank_lines(output);

output
}
Expand All @@ -63,7 +62,7 @@ pub fn convert_input(mut input: String, font: String, prefix: String) -> Vec<Str

let mut map_values: Vec<Vec<String>> = vec![];
for c in input.chars() {
map_values.push(map_search(c, &font));
map_values.push(map_search(c.to_string(), &font));
}
let output: Vec<String> = string_composite(map_values, prefix);

Expand All @@ -76,35 +75,77 @@ pub mod tests {
use std::collections::HashMap;

#[test]
fn test_string_composite() {
fn test_remove_blank_lines() {
let input: Vec<String> = vec![
" ╔╗ ╔╗ ".to_string(),
"╔╝╚╗ ╔╝╚╗".to_string(),
"╚╗╔╝╔══╗╔══╗╚╗╔╝".to_string(),
" ║║ ║╔╗║║══╣ ║║ ".to_string(),
" ║╚╗║║═╣╠══║ ║╚╗".to_string(),
" ╚═╝╚══╝╚══╝ ╚═╝".to_string(),
" ".to_string(),
" ".to_string(),
];
let expected: Vec<String> = vec![
" ╔╗ ╔╗ ".to_string(),
"╔╝╚╗ ╔╝╚╗".to_string(),
"╚╗╔╝╔══╗╔══╗╚╗╔╝".to_string(),
" ║║ ║╔╗║║══╣ ║║ ".to_string(),
" ║╚╗║║═╣╠══║ ║╚╗".to_string(),
" ╚═╝╚══╝╚══╝ ╚═╝".to_string(),
];
let output = remove_blank_lines(input);
assert_eq!(output, expected);
}

#[test]
fn test_prefix() {
let input = "abc";
let mut map_values: Vec<Vec<String>> = vec![];
for c in input.chars() {
map_values.push(map_search(c.to_string(), "blocks_in_two_lines"));
}

assert_eq!(
string_composite(map_values.clone(), "".to_string()),
vec!["▄▀▄ ██▄ ▄▀▀ ".to_string(), "█▀█ █▄█ ▀▄▄ ".to_string()]
);
assert_eq!(
string_composite(map_values.clone(), "# ".to_string()),
vec!["# ▄▀▄ ██▄ ▄▀▀ ".to_string(), "# █▀█ █▄█ ▀▄▄ ".to_string()]
);
}

#[test]
fn test_message_gen() {
let expected_map: HashMap<String, Vec<String>> = HashMap::from([
(
"blocks_in_two_lines".to_string(),
vec![
"▀█▀ ██▀ ▄▀▀ ▀█▀ ▄▀▀ ▀█▀ █▀▄ █ █ █ ▄▀ ▄█ ▀█ ▀██ ".to_string(),
" █▄▄ ▄██ ▄██ █ █▀▄ █ █ ▀█ ▀▄█ █ █▄ ▄▄█ ".to_string(),
"▄▀▄ ██▄ ▄▀▀ █▀▄ ██▀ █▀ ▄▀ █▄█ █ █ █▄▀ █ █▄ ▄█ █▄ █ ▄▀▄ █▀▄ ▄▀▄ █▀▄ ▄▀▀ ▀█▀ █ █ █ █ █ █ ▀▄▀ ▀▄▀ ▀█▀ ▄█ ▀█ ▀██ █▄ █▄ █▀ ▀█ █▄█ ██ █▀█ █ ▀ ▀ ▀ ▄▀ ▀▄ ".to_string(),
"█▀█ █▄█ ▀▄▄ █▄▀ █▄▄ █▀ ▀▄█ █ █▀▄█ █ █ █▄▄ █ ▀ █ █ ▀█ ▀▄▀ █▀ ▀▄█ █▀▄ ▄██ █ ▀▄█ ▀▄▀ ▀▄▀▄▀ █ █ █ █▄▄ █ █▄ ▄▄█ █ ▄█ ██ █ █▄█ ▄█ █▄█ ▄ ▄ ▀▄ ▄▀ ".to_string(),
],
),
(
"pipes".to_string(),
vec![
" ╔╗ ╗ ╔╗ ╔╗ ═══╗╔═══╗".to_string(),
"╔╝╚╗ ╔╝╚╗ ╔╝╚╗ ╔╝║ ║╔═╗║║╔═╗║".to_string(),
"╚╗╔╝╔══╗╔══╗╚╗╔╝ ╔══╗╚╗╔╝╔╗╔╗╔═╗ ╔══╗ ╚╗║ ╚╝╔╝║╚╝╔╝║".to_string(),
" ║║ ║╔╗║║══╣ ║║ ║══╣ ║║ ║╔╝╠╣║╔╗╗║╔╗║ ║║ ╔═╝╔╝╔╗╚╗║".to_string(),
" ║╚╗║║═╣╠══║ ║╚╠══║ ║╚╗║║ ║║║║║║║╚╝║ ╔╝╚╗║║╚═╗║╚═╝║".to_string(),
" ╚═╝╚══╝╚══╝ ╚═╝ ╚══╝ ╚═╝╚╝ ╚╝╚╝╚╝╚═╗║ ╚══╝╚═══╝═══╝".to_string(),
" ╔═╝║ ".to_string(),
" ══╝ ".to_string(),
" ╔╗ ╔╗ ╔═╔╗ ╔╗ ╔╗ ╔╗ ╔╗ ╔═══╗╔═══╗╔╗ ╔╗╔═══╗╔═══╗╔═══╗╔═══╗╔═══╗╔═══╗ ╔╗╔╗╔╗╔╗ ╔═╗╔═╗ ".to_string(),
" ║║ ║║ ║╔╝ ║║ ╔╗║║ ║║ ╔╝╚╗ ╔╝║ ║╔═╗║║╔═╗║║║ ║║║╔══╝║╔══╝║╔═╗║║╔═╗║║╔═╗║║╔═╗║ ║║║║║║║║ ╔╝╔╝╚╗╚╗ ".to_string(),
"╔══╗ ║╚═╗╔══╗╔═╝║╔══╗╔╝╚╗╔══╗║╚═╗╔╗ ╚╝║║╔╗║║ ╔╗╔╗╔═╗ ╔══╗╔══╗╔══╗╔═╗╔══╗╚╗╔╝╔╗╔╗╔╗╔╗╔╗╔╗╔╗╔╗╔╗╔╗ ╔╗╔═══╗╚╗║ ╚╝╔╝║╚╝╔╝║║╚═╝║║╚══╗║╚══╗╚╝╔╝║║╚═╝║║╚═╝║║║ ║║ ║║╚╝╚╝╚╝╔╝╔╝ ╚╗╚╗".to_string(),
"╚ ╗║ ║╔╗║║╔═╝║╔╗║║╔╗║╚╗╔╝║╔╗║║╔╗║╠╣ ╔╗║╚╝╝║║ ║╚╝║║╔╗╗║╔╗║║╔╗║║╔╗║║╔╝║══╣ ║║ ║║║║║╚╝║║╚╝╚╝║╚╬╬╝║║ ║║╠══║║ ║║ ╔═╝╔╝╔╗╚╗║╚══╗║╚══╗║║╔═╗║ ║╔╝║╔═╗║╚══╗║║║ ║║ ╚╝ ║║║ ║║║".to_string(),
"║╚╝╚╗║╚╝║║╚═╗║╚╝║║║═╣ ║║ ║╚╝║║║║║║║ ║║║╔╗╗║╚╗║║║║║║║║║╚╝║║╚╝║║╚╝║║║ ╠══║ ║╚╗║╚╝║╚╗╔╝╚╗╔╗╔╝╔╬╬╗║╚═╝║║║══╣╔╝╚╗║║╚═╗║╚═╝║ ║║╔══╝║║╚═╝║ ║║ ║╚═╝║╔══╝║║╚═╝║╔╗╔╗ ║║║ ║║║".to_string(),
"╚═══╝╚══╝╚══╝╚══╝╚══╝ ╚╝ ╚═╗║╚╝╚╝╚╝ ║║╚╝╚╝╚═╝╚╩╩╝╚╝╚╝╚══╝║╔═╝╚═╗║╚╝ ╚══╝ ╚═╝╚══╝ ╚╝ ╚╝╚╝ ╚╝╚╝╚═╗╔╝╚═══╝╚══╝╚═══╝╚═══╝ ╚╝╚═══╝╚═══╝ ╚╝ ╚═══╝╚═══╝╚═══╝╚╝╚╝ ╚╗╚╗ ╔╝╔╝".to_string(),
" ╔═╝║ ╔╝║ ║║ ║║ ╔═╝║ ╚╗╚╗╔╝╔╝ ".to_string(),
" ╚══╝ ╚═╝ ╚╝ ╚╝ ╚══╝ ╚═╝╚═╝ ".to_string(),
],
),
]);

for font in get_fonts() {
let input = "test string 123";
let input = "abcdefghijklmnopqrstuvwxyz1234567890.!\"\'()";
let mut map_values: Vec<Vec<String>> = vec![];
for c in input.chars() {
map_values.push(map_search(c, &font.to_string()));
map_values.push(map_search(c.to_string(), &font.to_string()));
}
let output: Vec<String> = string_composite(map_values, "".to_string());
let expected: Vec<String> = expected_map.get(&font).unwrap().clone();
Expand Down

0 comments on commit 13084fe

Please sign in to comment.