aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJacob Janzen <jacob.a.s.janzen@gmail.com>2022-08-23 12:05:47 -0500
committerJacob Janzen <jacob.a.s.janzen@gmail.com>2022-08-23 12:05:47 -0500
commitc463fb7d0ab36c44046a5a7edc88902f5b90e710 (patch)
tree532549c913d9bdcd49c6ef6804700441a49d1eb1 /src
parent1c08e24cabe887765d2d5de0c8ce20f63fa83792 (diff)
added proper cmd args
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs139
-rw-r--r--src/main.rs14
2 files changed, 66 insertions, 87 deletions
diff --git a/src/lib.rs b/src/lib.rs
index e662bba..626d775 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -15,53 +15,22 @@
use rand::Rng;
use std::vec;
-pub struct Config {
+use clap::Parser;
+
+pub struct Gif {
pub height: u16,
pub width: u16,
pub frames: u16,
- pub out_file: String,
+ pub pixels: Vec<u8>,
+ point_data: Vec<PointData>,
+ cross_distance: f64,
+ points: Vec<Point>,
}
-impl Config {
- pub fn build(mut args: impl Iterator<Item = String>) -> Result<Self, &'static str> {
- args.next();
-
- let width: u16 = match args.next() {
- Some(arg) => match arg.parse() {
- Ok(num) => num,
- Err(_) => return Err("Width is not a number"),
- },
- None => return Err("Didn't get a width"),
- };
-
- let height: u16 = match args.next() {
- Some(arg) => match arg.parse() {
- Ok(num) => num,
- Err(_) => return Err("Height is not a number"),
- },
- None => return Err("Didn't get a height"),
- };
-
- let frames: u16 = match args.next() {
- Some(arg) => match arg.parse() {
- Ok(num) => num,
- Err(_) => return Err("Frames is not a number"),
- },
- None => return Err("Didn't get a frame count"),
- };
-
- let out_file = match args.next() {
- Some(arg) => arg,
- None => return Err("Didn't get an output file"),
- };
-
- Ok(Config {
- height,
- width,
- frames,
- out_file,
- })
- }
+#[derive(Clone)]
+struct Point {
+ pub x: u16,
+ pub y: u16,
}
#[derive(Clone)]
@@ -70,65 +39,79 @@ struct PointData {
closest_point: Point,
}
-impl PointData {
- fn get_point_data(gif: &Gif, p: Point) -> Self {
- let mut pd = PointData {
- min_dist: gif.cross_distance,
- closest_point: Point { x: 0, y: 0 },
- };
+#[derive(Parser, Debug)]
+#[clap(author, version, about, long_about = None)]
+pub struct Args {
+ /// width of the image
+ #[clap(short, long, value_parser)]
+ pub width: u16,
- for point in &gif.points {
- let d = distance(&p, point);
- if d < pd.min_dist {
- pd.min_dist = d;
- pd.closest_point = point.clone();
- }
- }
+ /// height of the image
+ #[clap(short, long, value_parser)]
+ pub height: u16,
- pd
- }
+ /// number of gif frames
+ #[clap(short, long, value_parser)]
+ pub frames: u16,
+
+ /// number of cells to generate
+ #[clap(short, long, value_parser)]
+ pub num_cells: usize,
+
+ /// output file
+ #[clap(short, long, value_parser)]
+ pub out: String,
}
-pub struct Gif {
- pub height: u16,
- pub width: u16,
- pub frames: u16,
- pub pixels: Vec<u8>,
- point_data: Vec<PointData>,
- cross_distance: f64,
- points: Vec<Point>,
+impl Args {
+ pub fn read() -> Self {
+ Args::parse()
+ }
}
impl Gif {
- pub fn create_from_config(config: &Config, num_cells: usize) -> Self {
+ pub fn create_from_args(args: &Args) -> Self {
Gif {
- height: config.height,
- width: config.width,
- frames: config.frames,
- pixels: vec![0; config.height as usize * config.width as usize * 3],
+ height: args.height,
+ width: args.width,
+ frames: args.frames,
+ pixels: vec![0; args.height as usize * args.width as usize * 3],
point_data: vec![
PointData {
min_dist: 0.0,
closest_point: Point { x: 0, y: 0 }
};
- config.height as usize * config.width as usize
+ args.height as usize * args.width as usize
],
cross_distance: distance(
&Point { x: 0, y: 0 },
&Point {
- x: config.width - 1,
- y: config.height - 1,
+ x: args.width - 1,
+ y: args.height - 1,
},
),
- points: generate_points(config.width, config.height, num_cells),
+ points: generate_points(args.width, args.height, args.num_cells),
}
}
}
-#[derive(Clone)]
-struct Point {
- pub x: u16,
- pub y: u16,
+impl PointData {
+ fn get_point_data(gif: &Gif, p: Point) -> Self {
+ let mut pd = PointData {
+ min_dist: gif.cross_distance,
+ closest_point: Point { x: 0, y: 0 },
+ };
+
+ for point in &gif.points {
+ let d = distance(&p, point);
+ if d < pd.min_dist {
+ pd.min_dist = d;
+ pd.closest_point = point.clone();
+ }
+ }
+
+ pd
+ }
}
pub fn fill_canvas(gif: &mut Gif) {
diff --git a/src/main.rs b/src/main.rs
index 8fcac37..59a14c6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,25 +12,21 @@
You should have received a copy of the GNU General Public License along
with this program. If not, see https://www.gnu.org/licenses/.
*/
-use std::env;
use std::fs::File;
use std::process;
-use bubbles::Config;
+use bubbles::Args;
use bubbles::Gif;
fn main() {
- let config = Config::build(env::args()).unwrap_or_else(|err| {
- eprintln!("Error printing arguments: {err}");
- process::exit(1);
- });
+ let args = Args::read();
// create Gif data
- let mut gif = Gif::create_from_config(&config, 100);
+ let mut gif = Gif::create_from_args(&args);
// Create encoder
- let mut image = File::create(config.out_file).unwrap();
- let mut encoder = gif::Encoder::new(&mut image, config.width, config.height, &[]).unwrap();
+ let mut image = File::create(args.out).unwrap();
+ let mut encoder = gif::Encoder::new(&mut image, args.width, args.height, &[]).unwrap();
// Repeat infinitely
if let Err(_) = encoder.set_repeat(gif::Repeat::Infinite) {