Skip to content

Commit

Permalink
Split lib from core application, added examples
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-rt committed Feb 12, 2022
1 parent 033c020 commit 0d87abc
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 52 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ homepage = "https://github.com/ltgr/asciiframe"
authors = ["Luke"]
license = "MIT"
edition = "2018"
exclude = ["ci/benchmarks/*"]
exclude = ["ci/*", "examples/*"]

[[bin]]
name = "asc"
path = "src/main.rs"
path = "src/bin/asc/main.rs"

[dependencies]
clap = { version = "3.0.14", features = ["derive", "cargo"] }
Expand Down
2 changes: 2 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
Cargo.lock
7 changes: 7 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "asc-examples"
version = "0.1.0"
edition = "2021"

[dependencies]
asciiframe = "0.1.4"
12 changes: 12 additions & 0 deletions examples/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::path::Path;

fn main() -> asciiframe::Result<()> {
let strategy = asciiframe::Strategy::Ascii;
let fin = Path::new("test.mov");
let fout = Path::new("test.sh");

asciiframe::render_to_stdout(&fin, strategy)?;
asciiframe::render_to_file(&fin, &fout, strategy)?;

Ok(())
}
File renamed without changes.
39 changes: 39 additions & 0 deletions src/bin/asc/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#![forbid(unsafe_code)]
#![warn(warnings, rust_2018_idioms)]
#![warn(clippy::all, clippy::pedantic)]
#![allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
use clap::Parser;
use std::process;

mod cli;
mod validators;

fn run(strategy: asciiframe::Strategy) -> asciiframe::Result<()> {
let opts = cli::Opts::parse();

if let Some(p) = opts.output {
asciiframe::render_to_file(&opts.file, &p, strategy)?;
} else {
asciiframe::render_to_stdout(&opts.file, strategy)?;
}

Ok(())
}

pub fn main() {
// TODO: implement colors opt
let strategy = asciiframe::Strategy::Ascii;

let result = run(strategy);

match result {
Err(error) => {
let stderr = std::io::stderr();
asciiframe::default_error_handler(&mut stderr.lock(), &error);
process::exit(1);
}
Ok(()) => {
process::exit(0);
}
}
}
2 changes: 1 addition & 1 deletion src/validators.rs → src/bin/asc/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use ::std::fs::{metadata, File};
use ::std::path::Path;

use crate::error::{Error, Result};
use asciiframe::{Error, Result};

pub fn path_is_readable_file<P: AsRef<Path> + ?Sized>(p: &P) -> Result<()> {
let path = p.as_ref();
Expand Down
5 changes: 4 additions & 1 deletion src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pub enum Strategy {
ColorAscii,
}

/// # Errors
///
/// Will return `Err` if grabbing RGB values from `frame` fail
pub fn convert_frame(frame: &Mat, strategy: Strategy) -> Result<String> {
let mut res = String::default();

Expand Down Expand Up @@ -50,6 +53,6 @@ fn to_ascii(r: u8, g: u8, b: u8, strategy: Strategy) -> char {
CHARS[(10.0 * brightness / 255.0) as usize]
}

fn to_color_ascii(r: u8, g: u8, b: u8) -> String {
fn to_color_ascii(_r: u8, _g: u8, _b: u8) -> String {
String::from("hello world")
}
12 changes: 12 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use ansi_term::Colour::Red;
use std::fmt::Display;

#[derive(Debug)]

pub enum Error {
Io(::std::io::Error),
Opencv(::opencv::Error),
Expand Down Expand Up @@ -44,3 +46,13 @@ impl From<String> for Error {
impl std::error::Error for Error {}

pub type Result<T> = std::result::Result<T, Error>;

pub fn default_error_handler(handle: &mut dyn std::io::Write, error: &Error) {
writeln!(
handle,
"{} {}\n\nFor more information go to docs.rs/asciiframe",
Red.paint("error:"),
error
)
.unwrap();
}
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![forbid(unsafe_code)]
#![warn(warnings, rust_2018_idioms)]
#![warn(clippy::all, clippy::pedantic)]
#![allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
mod converter;
mod error;
mod renderer;

pub use converter::*;
pub use error::*;
pub use renderer::*;
35 changes: 0 additions & 35 deletions src/main.rs

This file was deleted.

28 changes: 15 additions & 13 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@ use terminal_size::{terminal_size, Height, Width};
use crate::converter;
use crate::error::{Error, Result};

pub fn render(filename: &Path, output: Option<&Path>, strategy: converter::Strategy) -> Result<()> {
if let Some(p) = output {
render_to_file(filename, p, strategy)?;
} else {
render_to_stdout(filename, strategy)?;
}

Ok(())
}

fn render_to_file(fin: &Path, fout: &Path, strategy: converter::Strategy) -> Result<()> {
/// # Errors
///
/// Will return `Err` if video capture from file fails OR
/// accessing frame count fails OR
/// reading frame data fails OR
/// writing ascii data to a file fails
pub fn render_to_file(fin: &Path, fout: &Path, strategy: converter::Strategy) -> Result<()> {
let mut capture = videoio::VideoCapture::from_file(fin.to_str().unwrap(), 0)?;
let frame_count: u64 = capture.get(videoio::CAP_PROP_FRAME_COUNT)? as u64;
let pb = ProgressBar::new(frame_count);
Expand Down Expand Up @@ -64,8 +60,14 @@ fn render_to_file(fin: &Path, fout: &Path, strategy: converter::Strategy) -> Res
Ok(())
}

fn render_to_stdout(filename: &Path, strategy: converter::Strategy) -> Result<()> {
let mut capture = videoio::VideoCapture::from_file(filename.to_str().unwrap(), 0)?;
/// # Errors
///
/// Will return `Err` if video capture from file fails OR
/// accessing frame count fails OR
/// reading frame data fails OR
/// writing ascii to stdout fails
pub fn render_to_stdout(fin: &Path, strategy: converter::Strategy) -> Result<()> {
let mut capture = videoio::VideoCapture::from_file(fin.to_str().unwrap(), 0)?;
let frame_count: u64 = capture.get(videoio::CAP_PROP_FRAME_COUNT)? as u64;
let time_d: f32 = (1.0 / capture.get(videoio::CAP_PROP_FPS)?) as f32;
let stdout = io::stdout();
Expand Down

0 comments on commit 0d87abc

Please sign in to comment.