Skip to content

Commit

Permalink
Text print mode
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 28, 2023
1 parent 2485df2 commit 143ed69
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
22 changes: 22 additions & 0 deletions printer/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub struct Args {
pub enum SubCommand {
/// Encode an image into a program for printing.
Image(ImageArgs),
/// Encode text into a program for printing.
Text(TextArgs),
}

#[derive(Parser)]
Expand All @@ -27,3 +29,23 @@ pub struct ImageArgs {
#[clap(long, default_value_t = 10)]
pub program_lines: usize,
}

#[derive(Parser)]
pub struct TextArgs {
/// The input text file.
/// Only chars in the [FOCAL character set](https://en.wikipedia.org/wiki/FOCAL_character_set) are allowed.
pub input: PathBuf,
/// The output program.
pub output: PathBuf,
/// Preview the output.
#[clap(long)]
pub preview: bool,
/// Don't automatically wrap lines.
/// Any lines longer than 22 characters will be truncated.
#[clap(long)]
pub no_wrap: bool,
/// Don't actually write the output.
/// Useful for previewing the output.
#[clap(long)]
pub dry_run: bool,
}
6 changes: 4 additions & 2 deletions printer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ mod args;
mod data;
mod image;
mod misc;
use args::Args;
mod text;
use args::{Args, SubCommand};

fn main() -> Result<()> {
let args = Args::parse();

match args.cmd {
args::SubCommand::Image(image_args) => image::encode(image_args)?,
SubCommand::Image(args) => image::encode(args)?,
SubCommand::Text(args) => text::encode(args)?,
}

Ok(())
Expand Down
53 changes: 53 additions & 0 deletions printer/src/text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::fs;

use anyhow::Result;

use crate::args::TextArgs;

pub fn encode(args: TextArgs) -> Result<()> {
let text = fs::read_to_string(args.input)?;
let lines = text
.lines()
.map(|l| truncate(l, 22).trim_end())
.collect::<Vec<_>>();

if args.preview {
for line in &lines {
println!("|{:<22}|", line);
}
}

if args.dry_run {
return Ok(());
}

let mut prg = String::new();

for line in lines {
prg.push_str(&encode_string(line));
prg.push_str("AVIEW\n");
}

fs::write(args.output, prg)?;
Ok(())
}

fn truncate(s: &str, len: usize) -> &str {
&s[..(len.min(s.len()))]
}

fn encode_string(s: &str) -> String {
let mut prg = format!("\"{}\"\n", truncate(s, 14));

if prg.len() <= 14 {
return prg;
}

let mut i = 14;
while i < s.len() {
prg.push_str(&format!("├\"{}\"\n", truncate(&s[i..], 14)));
i += 14;
}

prg
}

0 comments on commit 143ed69

Please sign in to comment.