Skip to content

Commit

Permalink
Add option to change directory to load dependant asset files (transit…
Browse files Browse the repository at this point in the history
…ion images)
  • Loading branch information
ollej committed Dec 21, 2022
1 parent 870ae09 commit 3a915c8
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "rusty-slider"
version = "0.22.0"
version = "0.23.0"
authors = ["Olle Wreede <olle@wreede.se>"]
edition = "2021"
license = "MIT"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -197,13 +197,14 @@ A small tool to display markdown files as a slideshow.
Usage: rusty_slider [OPTIONS]
Options:
-d, --directory <DIRECTORY> Path to directory to load files from [default: assets]
-d, --directory <DIRECTORY> Path to directory to load slideshow files from [default: assets]
-s, --slides <SLIDES> Markdown files with slides text [default: rusty-slider.md]
-t, --theme <THEME> File with theme options [default: default-theme.json]
-a, --automatic <AUTOMATIC> Automatically switch slides every N seconds [default: 0]
--demo-transitions Switch transitions for every slide
-S, --screenshot <SCREENSHOT> When taking screenshot, store PNG at this path [default: screenshot.png]
--enable-code-execution Enable executing code in code blocks
-A, --assets <ASSETS> Path to directory where application files are loaded from [default: assets]
-h, --help Print help information
```

Expand Down
51 changes: 51 additions & 0 deletions src/app_options.rs
@@ -0,0 +1,51 @@
use std::path::PathBuf;

use clap::{command, Parser};

use crate::prelude::Duration;

#[derive(Parser, Debug, Clone)]
#[command(
name = "rusty-slider",
about = "A small tool to display markdown files as a slideshow."
)]
pub struct AppOptions {
/// Path to directory to load slideshow files from
#[arg(short, long, default_value = "assets")]
pub directory: PathBuf,
/// Markdown files with slides text.
#[arg(short, long, default_value = "rusty-slider.md")]
pub slides: PathBuf,
/// File with theme options.
#[arg(short, long, default_value = "default-theme.json")]
pub theme: PathBuf,
/// Automatically switch slides every N seconds.
#[arg(short, long, default_value = "0")]
pub automatic: Duration,
/// Switch transitions for every slide
#[arg(long)]
pub demo_transitions: bool,
/// When taking screenshot, store PNG at this path
#[arg(short = 'S', long, default_value = "screenshot.png")]
pub screenshot: PathBuf,
/// Enable executing code in code blocks
#[arg(long)]
pub enable_code_execution: bool,
/// Path to directory where application files are loaded from
#[arg(short = 'A', long, default_value = "assets")]
pub assets: PathBuf,
}

impl AppOptions {
pub fn slides_path(&self) -> PathBuf {
let mut path = self.directory.clone();
path.push(self.slides.clone());
path
}

pub fn theme_path(&self) -> PathBuf {
let mut path = self.directory.clone();
path.push(self.theme.clone());
path
}
}
1 change: 1 addition & 0 deletions src/lib.rs
@@ -1,3 +1,4 @@
pub mod app_options;
pub mod clipboard;
pub mod code_box_builder;
pub mod codebox;
Expand Down
61 changes: 5 additions & 56 deletions src/main.rs
Expand Up @@ -2,54 +2,10 @@

extern crate markdown;

use std::path::PathBuf;
use {clap::Parser, macroquad::prelude::*, quad_url::get_program_parameters};

use rusty_slider::prelude::*;

#[derive(Parser, Debug)]
#[command(
name = "rusty-slider",
about = "A small tool to display markdown files as a slideshow."
)]
struct CliOptions {
/// Path to directory to load files from
#[arg(short, long, default_value = "assets")]
pub directory: PathBuf,
/// Markdown files with slides text.
#[arg(short, long, default_value = "rusty-slider.md")]
pub slides: PathBuf,
/// File with theme options.
#[arg(short, long, default_value = "default-theme.json")]
pub theme: PathBuf,
/// Automatically switch slides every N seconds.
#[arg(short, long, default_value = "0")]
pub automatic: Duration,
/// Switch transitions for every slide
#[arg(long)]
pub demo_transitions: bool,
/// When taking screenshot, store PNG at this path
#[arg(short = 'S', long, default_value = "screenshot.png")]
pub screenshot: PathBuf,
/// Enable executing code in code blocks
#[arg(long)]
pub enable_code_execution: bool,
}

impl CliOptions {
fn slides_path(&self) -> PathBuf {
let mut path = self.directory.clone();
path.push(self.slides.clone());
path
}

fn theme_path(&self) -> PathBuf {
let mut path = self.directory.clone();
path.push(self.theme.clone());
path
}
}

fn window_conf() -> Conf {
Conf {
window_title: "Rusty Slider".to_owned(),
Expand All @@ -60,22 +16,15 @@ fn window_conf() -> Conf {

#[macroquad::main(window_conf())]
async fn main() {
let opt = CliOptions::parse_from(get_program_parameters().iter());
let options = AppOptions::parse_from(get_program_parameters().iter());

let theme = Theme::load(opt.theme_path()).await;
let theme = Theme::load(options.theme_path()).await;
debug!(
"background_color: {:?} text_color: {:?} heading_color{:?}",
theme.background_color, theme.text_color, theme.heading_color,
);
let mut shader_activated = theme.shader;
let mut slides = Slides::load(
opt.slides_path(),
theme,
opt.automatic,
opt.demo_transitions,
opt.directory,
)
.await;
let mut slides = Slides::load(options.clone(), theme).await;
let mut show_help = ShowHelp::new();
let shader_material = load_material(crt::VERTEX, crt::FRAGMENT, Default::default()).unwrap();

Expand Down Expand Up @@ -107,7 +56,7 @@ async fn main() {
_ => (),
}
#[cfg(not(target_arch = "wasm32"))]
if opt.enable_code_execution && is_key_pressed(KeyCode::Enter) {
if options.enable_code_execution && is_key_pressed(KeyCode::Enter) {
slides.run_code_block();
}

Expand Down Expand Up @@ -137,7 +86,7 @@ async fn main() {
show_help.draw();

if is_key_pressed(KeyCode::S) {
get_screen_data().export_png(&opt.screenshot.to_string_lossy());
get_screen_data().export_png(&options.screenshot.to_string_lossy());
}

next_frame().await
Expand Down
1 change: 1 addition & 0 deletions src/prelude.rs
@@ -1,3 +1,4 @@
pub use crate::app_options::*;
pub use crate::clipboard::*;
pub use crate::code_box_builder::*;
pub use crate::codebox::*;
Expand Down
24 changes: 7 additions & 17 deletions src/slider.rs
Expand Up @@ -3,7 +3,6 @@ use crate::prelude::*;
use macroquad::prelude::*;
use nanoserde::DeJson;
use regex::Regex;
use std::path::{Path, PathBuf};

#[derive(Copy, Clone, Debug, DeJson)]
#[allow(non_camel_case_types)]
Expand Down Expand Up @@ -152,21 +151,12 @@ impl Slides {
}
}

pub async fn load<P>(
slides_path: PathBuf,
theme: Theme,
automatic: Duration,
demo_transitions: bool,
assets_dir: P,
) -> Self
where
P: AsRef<Path>,
{
let path = slides_path.as_path().to_str().unwrap().to_owned();
let markdown = match load_string(&path).await {
pub async fn load(options: AppOptions, theme: Theme) -> Self {
let path = options.slides_path();
let markdown = match load_string(&path.to_str().unwrap()).await {
Ok(text) => Self::sanitize_markdown(text),
Err(_) => {
eprintln!("Couldn't parse markdown document: {}", path);
eprintln!("Couldn't parse markdown document: {:?}", path);
std::process::exit(1);
}
};
Expand Down Expand Up @@ -206,7 +196,7 @@ impl Slides {
CodeBoxBuilder::new(theme.clone(), font_code, font_bold, font_italic);

let transitioner = match theme.transition {
Some(transition) => Some(Transitioner::load(assets_dir, transition, 0.1).await),
Some(transition) => Some(Transitioner::load(options.assets, transition, 0.1).await),
None => None,
};

Expand All @@ -215,8 +205,8 @@ impl Slides {
theme.clone(),
code_box_builder,
background,
automatic,
demo_transitions,
options.automatic,
options.demo_transitions,
transitioner,
)
}
Expand Down

0 comments on commit 3a915c8

Please sign in to comment.