Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
minauteur committed Mar 17, 2023
1 parent 1fcaf74 commit ce23eca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 42 deletions.
28 changes: 12 additions & 16 deletions src/gen.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
//!Gen Module
//!This file contains behaviors and functions critical to text generation


use crate::flavor;
use markov::Chain;
use crate::poems::Poem;
use crate::poems::{AuthorsList, WorksList};
use std::error::Error;
use crate::util;

use textwrap::termwidth;
use markov::Chain;
use std::error::Error;

use colored::*;

pub fn seed_and_generate(chain: &Chain<String>, lines_read: usize) -> &Chain<String> {

let width = termwidth() - 12;
let width = 68;

let mut poem_storage: Vec<String> = Vec::new();
let mut poem = Poem::new();
Expand All @@ -25,14 +21,14 @@ pub fn seed_and_generate(chain: &Chain<String>, lines_read: usize) -> &Chain<Str
middle: String::from("Erronaeus"),
last: String::from("The Unwrapp-ed None"),
};
let title_error: Work = Work { title: String::from("\"A Tale of Error and Woe\"") };
let title_error: Work = Work {
title: String::from("\"A Tale of Error and Woe\""),
};
let gen_name: Name = Name::new().from_file().unwrap_or(name_error);
let gen_work: Work = Work::new().from_file().unwrap_or(title_error);
let author_fmt = format!(
"{} {} {}",
&gen_name.first,
&gen_name.middle,
&gen_name.last
&gen_name.first, &gen_name.middle, &gen_name.last
);
poem.title = gen_work.title;
poem.author = author_fmt.clone();
Expand Down Expand Up @@ -105,7 +101,9 @@ pub struct Work {
}
impl Work {
pub fn new() -> Work {
Work { title: String::new() }
Work {
title: String::new(),
}
}
pub fn from_file(mut self: Self) -> Result<Work, Box<dyn Error>> {
let list = WorksList::new();
Expand Down Expand Up @@ -150,7 +148,6 @@ impl Name {
// println!("got last name! \n{}", l_n);
if let Some(l_n) = single_name.next() {
last_name.feed_str(l_n);

}
}
}
Expand Down Expand Up @@ -184,9 +181,8 @@ impl Name {
let mut name = Name::new();
let mut names = s.split(" ");
name.first.push_str(names.next().unwrap_or("Sir Error"));
name.last.push_str(
names.next().unwrap_or("The Unwrapped None"),
);
name.last
.push_str(names.next().unwrap_or("The Unwrapped None"));
return name;
}
}
54 changes: 28 additions & 26 deletions src/poems.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//!Poems houses the structs and implementations for deserializing and
//! storing Poem data retrieved from poetrydb API requests
use serde_json::{self, Value};
use textwrap::{ termwidth, fill};
use textwrap::{fill, termwidth, Options};
// use textwrap::wrap_iter;
use crate::util;
use colored::*;
Expand Down Expand Up @@ -35,20 +35,19 @@ impl Poem {
author: String::new(),
lines: Vec::new(),
linecount: "0".to_string(),
linenumber: 0
linenumber: 0,
}
}
pub fn from_value(mut self, json: &Value) -> Result<Poem, serde_json::Error> {
if let Some(lines) = json.get("lines") {
self.get_lines(&lines)?;
println!("got lines {}", &lines);
println!("got lines {}", &lines);
} else {
println!("no lines found!");
}
if let Some(author) = json.get("author") {
self.get_author(&author)?;
println!("got author: {}", &self.author);

println!("got author: {}", &self.author);
} else {
// println!("no author name found!");
}
Expand All @@ -71,37 +70,39 @@ impl Poem {
}
println!("poem: {:#?}", &self);
return Ok(self);

}
pub fn print(&self) -> Self {

let width = termwidth() - 12;
let author = format!(" author: {}", self.author.purple());
if termwidth() < 80 {
println!("you should resize your terminal to be wider than 80 columns");
return self.to_owned();
}
let width = Options::new(68)
.initial_indent(" ")
.subsequent_indent(" ");
let author = format!(" author: {}", self.author.purple());
let title = format!(" a poem: \"{}\"", self.title);
let poem = self.lines.join("\n");

println!(" |{:=<1$}|", "=", width + 6);
let t_fmt = format!("{}", fill(&title, width));
for t_l in t_fmt.lines() {
println!(" | {:<1$} |", &t_l, width);
}
println!(" |{:-<1$}|", "-", width + 6);
println!(" |{:=<1$}|", "=", 68 + 6);
let t_fmt = format!("{}", fill(&title, &width));
for t_l in t_fmt.lines() {
println!(" | {:<1$} |", &t_l, 68);
}
println!(" |{:-<1$}|", "-", 68 + 6);

for line in poem.lines() {
let formatted = format!("{}", fill(&line, width));
let formatted = format!("{}", fill(&line, &width));
// let formatted =
// println!(" | {:<1$} |", fill(&formatted, width), width);

// let formatted = format!("{:<1$}", fill(&line, width-9), width-9);
for line in formatted.lines() {
println!(" | {:<1$} |",
&line.bright_green(),
width
);
}}
println!(" |{:-<1$}|", "-", width + 6);
println!(" |{:<1$} |", fill(&author, width), width + 9);
println!(" |{:=<1$}|", "=", width + 6);
println!(" | {:<1$} |", &line.bright_green(), 68);
}
}
println!(" |{:-<1$}|", "-", 68 + 6);
println!(" |{:<1$} |", fill(&author, width), 68 + 9);
println!(" |{:=<1$}|", "=", 68 + 6);
return self.to_owned();
}
fn get_lines(&mut self, json: &Value) -> Result<Self, serde_json::Error> {
Expand All @@ -128,9 +129,10 @@ pub struct AuthorsList {
}
impl AuthorsList {
pub fn new() -> AuthorsList {
let default: AuthorsList = AuthorsList { authors: Vec::new() };
let default: AuthorsList = AuthorsList {
authors: Vec::new(),
};
let list: AuthorsList = util::read_authors_from_file().unwrap_or(default);
return list;

}
}

0 comments on commit ce23eca

Please sign in to comment.