Skip to content

Commit e73f673

Browse files
committed
added: custom background color option
1 parent d78a85d commit e73f673

File tree

3 files changed

+175
-122
lines changed

3 files changed

+175
-122
lines changed

src/args.rs

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
use std::fmt::Display;
2+
3+
use bevy::color::Srgba;
4+
use clap::{Parser, Subcommand};
5+
6+
#[derive(Parser)]
7+
#[command(author, version, about, long_about = None)]
8+
#[command(propagate_version = true)]
9+
pub struct Args {
10+
#[command(subcommand)]
11+
pub variant: Option<Variant>,
12+
13+
#[arg(
14+
short,
15+
long = "bg",
16+
global = true,
17+
name = "HEX COLOR",
18+
help = "Set screensaver background to provided HEX COLOR, if applicable to variant."
19+
)]
20+
pub background: Option<ColorPreference>,
21+
22+
#[arg(
23+
short,
24+
long,
25+
global = true,
26+
name = "DELAY",
27+
help = "Prints command for initiating ttysvr in DELAY seconds."
28+
)]
29+
pub init: Option<u32>,
30+
31+
#[arg(
32+
short,
33+
long,
34+
global = true,
35+
help = "Prints command for cancelling ttysvr in current shell."
36+
)]
37+
pub cancel: bool,
38+
}
39+
40+
#[derive(Subcommand)]
41+
pub enum Variant {
42+
Bubbles,
43+
Logo {
44+
#[command(subcommand)]
45+
variant: Option<LogoVariant>,
46+
},
47+
Maze {
48+
#[command(subcommand)]
49+
variant: Option<MazeVariant>,
50+
},
51+
}
52+
53+
#[derive(Subcommand)]
54+
pub enum LogoVariant {
55+
Dvd,
56+
Tty,
57+
}
58+
59+
#[derive(Subcommand)]
60+
pub enum MazeVariant {
61+
Brick,
62+
Hedge,
63+
}
64+
65+
#[derive(Clone)]
66+
pub struct ColorPreference(pub Srgba);
67+
68+
impl From<String> for ColorPreference {
69+
fn from(value: String) -> Self {
70+
if let Ok(hex_color) = Srgba::hex(value) {
71+
return ColorPreference(hex_color);
72+
}
73+
74+
ColorPreference(Srgba::NONE)
75+
}
76+
}
77+
78+
impl Display for Variant {
79+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80+
match self {
81+
Variant::Bubbles => write!(f, "bubbles"),
82+
Variant::Logo { variant } => {
83+
if let Some(variant) = variant {
84+
write!(f, "logo {variant}")
85+
} else {
86+
write!(f, "logo")
87+
}
88+
}
89+
Variant::Maze { variant } => {
90+
if let Some(variant) = variant {
91+
write!(f, "maze {variant}")
92+
} else {
93+
write!(f, "maze")
94+
}
95+
}
96+
}
97+
}
98+
}
99+
100+
impl Display for LogoVariant {
101+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102+
match self {
103+
LogoVariant::Dvd => write!(f, "dvd"),
104+
LogoVariant::Tty => write!(f, "tty"),
105+
}
106+
}
107+
}
108+
109+
impl Display for MazeVariant {
110+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111+
match self {
112+
MazeVariant::Brick => write!(f, "brick"),
113+
MazeVariant::Hedge => write!(f, "hedge"),
114+
}
115+
}
116+
}
117+
118+
impl Display for ColorPreference {
119+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120+
write!(f, "{}", self.0.to_hex())
121+
}
122+
}

src/lib.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mod common;
1919
mod logo;
2020
mod maze;
2121

22-
pub struct AppPlugin(pub SaverVariant);
22+
pub struct AppPlugin(pub Settings);
2323

2424
impl Plugin for AppPlugin {
2525
fn build(&self, app: &mut App) {
@@ -37,11 +37,14 @@ impl Plugin for AppPlugin {
3737

3838
app.add_plugins((assets::plugin, common::plugin));
3939

40-
if let SaverVariant::Logo(ref logo_path) = self.0 {
41-
app.insert_resource(LogoPath(logo_path.into()));
42-
}
40+
let Settings {
41+
ref variant,
42+
ref background,
43+
} = self.0;
44+
45+
app.insert_resource(ClearColor(Color::Srgba(*background)));
4346

44-
match self.0 {
47+
match variant {
4548
SaverVariant::Logo(ref logo_path) => {
4649
app.insert_resource(LogoPath(logo_path.into()));
4750
}
@@ -51,7 +54,7 @@ impl Plugin for AppPlugin {
5154
_ => {}
5255
}
5356

54-
app.add_plugins(match self.0 {
57+
app.add_plugins(match variant {
5558
SaverVariant::Bubbles => bubbles::plugin,
5659
SaverVariant::Logo(_) => logo::plugin,
5760
SaverVariant::Maze(_, _) => maze::plugin,
@@ -65,6 +68,11 @@ pub struct Flags {
6568
_msgs: Vec<String>,
6669
}
6770

71+
pub struct Settings {
72+
pub variant: SaverVariant,
73+
pub background: Srgba,
74+
}
75+
6876
pub enum SaverVariant {
6977
Bubbles,
7078
Logo(String),

src/main.rs

+39-116
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,41 @@
1-
use std::{env, fmt::Display};
1+
use std::env;
22

3-
use bevy::app::App;
4-
use clap::{Parser, Subcommand};
3+
use args::{Args, LogoVariant, MazeVariant, Variant};
4+
use bevy::{app::App, color::Srgba};
5+
use clap::Parser;
56
use ttysvr::{
6-
AppPlugin, SaverVariant, LOGO_PATH_DVD, LOGO_PATH_TTY, MAZE_CEILING_PATH_BRICK,
7+
AppPlugin, SaverVariant, Settings, LOGO_PATH_DVD, LOGO_PATH_TTY, MAZE_CEILING_PATH_BRICK,
78
MAZE_CEILING_PATH_HEDGE, MAZE_WALL_PATH_BRICK, MAZE_WALL_PATH_HEDGE,
89
};
910

10-
#[derive(Parser)]
11-
#[command(author, version, about, long_about = None)]
12-
#[command(propagate_version = true)]
13-
struct Cli {
14-
#[command(subcommand)]
15-
variant: Option<Variant>,
16-
17-
#[arg(
18-
short,
19-
long,
20-
global = true,
21-
name = "DELAY",
22-
help = "Prints command for initiating ttysvr in DELAY seconds."
23-
)]
24-
init: Option<u32>,
25-
26-
#[arg(
27-
short,
28-
long,
29-
global = true,
30-
help = "Prints command for cancelling ttysvr in current shell."
31-
)]
32-
cancel: bool,
33-
}
34-
35-
#[derive(Subcommand)]
36-
pub enum Variant {
37-
Bubbles,
38-
Logo {
39-
#[command(subcommand)]
40-
variant: Option<LogoVariant>,
41-
},
42-
Maze {
43-
#[command(subcommand)]
44-
variant: Option<MazeVariant>,
45-
},
46-
}
47-
48-
#[derive(Subcommand)]
49-
pub enum LogoVariant {
50-
Dvd,
51-
Tty,
52-
}
53-
54-
#[derive(Subcommand)]
55-
pub enum MazeVariant {
56-
Brick,
57-
Hedge,
58-
}
59-
60-
impl Display for Variant {
61-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62-
match self {
63-
Variant::Bubbles => write!(f, "bubbles"),
64-
Variant::Logo { variant } => {
65-
if let Some(variant) = variant {
66-
write!(f, "logo {variant}")
67-
} else {
68-
write!(f, "logo")
69-
}
70-
}
71-
Variant::Maze { variant } => {
72-
if let Some(variant) = variant {
73-
write!(f, "maze {variant}")
74-
} else {
75-
write!(f, "maze")
76-
}
77-
}
78-
}
79-
}
80-
}
81-
82-
impl Display for LogoVariant {
83-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84-
match self {
85-
LogoVariant::Dvd => write!(f, "dvd"),
86-
LogoVariant::Tty => write!(f, "tty"),
87-
}
88-
}
89-
}
90-
91-
impl Display for MazeVariant {
92-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93-
match self {
94-
MazeVariant::Brick => write!(f, "brick"),
95-
MazeVariant::Hedge => write!(f, "hedge"),
96-
}
97-
}
98-
}
11+
mod args;
9912

10013
fn main() {
101-
let Cli {
14+
let Args {
10215
variant,
16+
background,
10317
init,
10418
cancel,
105-
} = Cli::parse();
106-
107-
let saver_variant = match variant {
108-
Some(Variant::Bubbles) => SaverVariant::Bubbles,
109-
Some(Variant::Logo { ref variant }) => match variant {
110-
Some(LogoVariant::Dvd) | None => SaverVariant::Logo(LOGO_PATH_DVD.into()),
111-
Some(LogoVariant::Tty) => SaverVariant::Logo(LOGO_PATH_TTY.into()),
112-
},
113-
Some(Variant::Maze { ref variant }) => match variant {
114-
Some(MazeVariant::Brick) | None => {
115-
SaverVariant::Maze(MAZE_WALL_PATH_BRICK.into(), MAZE_CEILING_PATH_BRICK.into())
116-
}
117-
Some(MazeVariant::Hedge) => {
118-
SaverVariant::Maze(MAZE_WALL_PATH_HEDGE.into(), MAZE_CEILING_PATH_HEDGE.into())
119-
}
120-
},
121-
None => rand::random(),
122-
};
19+
} = Args::parse();
12320

12421
if let Some(delay) = init {
12522
let executable_string = env::args().next().unwrap_or("ttysvr".into());
12623
let variant_string = match variant {
127-
None => "".into(),
12824
Some(variant) => format!(" {variant}"),
25+
None => "".into(),
26+
};
27+
let background_string = match background {
28+
Some(background) => format!(" --bg={background}"),
29+
None => "".into(),
12930
};
13031

13132
#[rustfmt::skip]
13233
println!(
13334
"
134-
TMOUT={delay}; trap \"{executable_string}{variant_string}; zle reset-prompt\" ALRM
35+
TMOUT={delay}; trap \"{executable_string}{variant_string}{background_string}; zle reset-prompt\" ALRM
13536
13637
# WRAP THIS COMMAND IN EVAL WITH BACKTICKS (ZSH ONLY)
137-
# EXAMPLE: eval `ttysvr{variant_string} --init {delay}`
38+
# EXAMPLE: eval `ttysvr{variant_string}{background_string} --init {delay}`
13839
"
13940
);
14041
return;
@@ -153,5 +54,27 @@ TMOUT=0
15354
return;
15455
}
15556

156-
App::new().add_plugins(AppPlugin(saver_variant)).run();
57+
let saver_variant = match variant {
58+
Some(Variant::Bubbles) => SaverVariant::Bubbles,
59+
Some(Variant::Logo { ref variant }) => match variant {
60+
Some(LogoVariant::Dvd) | None => SaverVariant::Logo(LOGO_PATH_DVD.into()),
61+
Some(LogoVariant::Tty) => SaverVariant::Logo(LOGO_PATH_TTY.into()),
62+
},
63+
Some(Variant::Maze { ref variant }) => match variant {
64+
Some(MazeVariant::Brick) | None => {
65+
SaverVariant::Maze(MAZE_WALL_PATH_BRICK.into(), MAZE_CEILING_PATH_BRICK.into())
66+
}
67+
Some(MazeVariant::Hedge) => {
68+
SaverVariant::Maze(MAZE_WALL_PATH_HEDGE.into(), MAZE_CEILING_PATH_HEDGE.into())
69+
}
70+
},
71+
None => rand::random(),
72+
};
73+
74+
let settings = Settings {
75+
variant: saver_variant,
76+
background: background.map_or(Srgba::NONE, |bg| bg.0),
77+
};
78+
79+
App::new().add_plugins(AppPlugin(settings)).run();
15780
}

0 commit comments

Comments
 (0)