diff options
author | boasting-squirrel <boasting.squirrel@gmail.com> | 2019-04-03 18:07:33 +0000 |
---|---|---|
committer | boasting-squirrel <boasting.squirrel@gmail.com> | 2019-04-03 18:07:33 +0000 |
commit | 0304ea92390c06ccbbd7d29a88ecedc9718d72c2 (patch) | |
tree | 670c3b12843187e042ca3981960b0cf56230985e /src | |
parent | Added light theme (diff) | |
download | miniserve-0304ea92390c06ccbbd7d29a88ecedc9718d72c2.tar.gz miniserve-0304ea92390c06ccbbd7d29a88ecedc9718d72c2.zip |
Added CLI argument to set default theme
Diffstat (limited to '')
-rw-r--r-- | src/args.rs | 16 | ||||
-rw-r--r-- | src/listing.rs | 6 | ||||
-rw-r--r-- | src/main.rs | 12 | ||||
-rw-r--r-- | src/themes.rs | 23 |
4 files changed, 39 insertions, 18 deletions
diff --git a/src/args.rs b/src/args.rs index 4f0dbf7..bcee0bc 100644 --- a/src/args.rs +++ b/src/args.rs @@ -3,6 +3,7 @@ use std::path::PathBuf; use structopt::StructOpt; use crate::auth; +use crate::themes; /// Possible characters for random routes const ROUTE_ALPHABET: [char; 16] = [ @@ -47,6 +48,18 @@ struct CLIArgs { /// Do not follow symbolic links #[structopt(short = "P", long = "no-symlinks")] no_symlinks: bool, + + /// Default color scheme + #[structopt( + short = "c", + long = "color-scheme", + default_value = "Squirrel", + raw( + possible_values = "&themes::ColorScheme::variants()", + case_insensitive = "true", + ) + )] + color_scheme: themes::ColorScheme, } /// Checks wether an interface is valid, i.e. it can be parsed into an IP address @@ -89,6 +102,8 @@ pub fn parse_args() -> crate::MiniserveConfig { None }; + let default_color_scheme = args.color_scheme; + let path_explicitly_chosen = args.path.is_some(); crate::MiniserveConfig { @@ -100,5 +115,6 @@ pub fn parse_args() -> crate::MiniserveConfig { path_explicitly_chosen, no_symlinks: args.no_symlinks, random_route, + default_color_scheme, } } diff --git a/src/listing.rs b/src/listing.rs index 4a20db2..64eb575 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -13,9 +13,6 @@ use crate::errors; use crate::renderer; use crate::themes; -/// Default color scheme, when none is set through query parameters -const DEFAULT_COLORSCHEME: themes::ColorScheme = themes::ColorScheme::Archlinux; - /// Query parameters #[derive(Debug, Deserialize)] struct QueryParameters { @@ -150,6 +147,7 @@ pub fn directory_listing<S>( req: &HttpRequest<S>, skip_symlinks: bool, random_route: Option<String>, + default_color_scheme: themes::ColorScheme, ) -> Result<HttpResponse, io::Error> { let title = format!("Index of {}", req.path()); let base = Path::new(req.path()); @@ -255,7 +253,7 @@ pub fn directory_listing<S>( } } - let color_scheme = color_scheme.unwrap_or(DEFAULT_COLORSCHEME); + let color_scheme = color_scheme.unwrap_or(default_color_scheme); if let Some(compression_method) = &download { log::info!( diff --git a/src/main.rs b/src/main.rs index 1a4a68f..79a5db2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,6 +43,9 @@ pub struct MiniserveConfig { /// Enable random route generation pub random_route: Option<String>, + + /// Default color scheme + pub default_color_scheme: themes::ColorScheme, } fn main() { @@ -179,6 +182,7 @@ fn configure_app(app: App<MiniserveConfig>) -> App<MiniserveConfig> { let path = &app.state().path; let no_symlinks = app.state().no_symlinks; let random_route = app.state().random_route.clone(); + let default_color_scheme = app.state().default_color_scheme.clone(); if path.is_file() { None } else { @@ -187,7 +191,13 @@ fn configure_app(app: App<MiniserveConfig>) -> App<MiniserveConfig> { .expect("Couldn't create path") .show_files_listing() .files_listing_renderer(move |dir, req| { - listing::directory_listing(dir, req, no_symlinks, random_route.clone()) + listing::directory_listing( + dir, + req, + no_symlinks, + random_route.clone(), + default_color_scheme.clone(), + ) }), ) } diff --git a/src/themes.rs b/src/themes.rs index f38814e..d04eab2 100644 --- a/src/themes.rs +++ b/src/themes.rs @@ -1,18 +1,15 @@ use serde::Deserialize; +use structopt::clap::{_clap_count_exprs, arg_enum}; -#[derive(Debug, Deserialize, Clone)] -pub enum ColorScheme { - #[serde(alias = "archlinux")] - Archlinux, - - #[serde(alias = "zenburn")] - Zenburn, - - #[serde(alias = "monokai")] - Monokai, - - #[serde(alias = "squirrel")] - Squirrel, +arg_enum! { + #[derive(Debug, Deserialize, Clone)] + #[serde(rename_all = "lowercase")] + pub enum ColorScheme { + Archlinux, + Zenburn, + Monokai, + Squirrel, + } } impl ColorScheme { |