Skip to content

Commit

Permalink
added pattern scheme and concatination
Browse files Browse the repository at this point in the history
  • Loading branch information
bresilla committed Jan 4, 2021
1 parent 6ea9cb6 commit fd61ca1
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ There is the old bash version in: https://github.com/warpwm/lule_bash

```
lule --image=/some/path/of/an/image.jpg create -- set
LULE_W=/some/dir/of/wallpapers; lule create -- set
```
<hr>

Expand Down
17 changes: 15 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod daemon;
pub mod test;
use colored::*;

use clap::{crate_description, crate_name, crate_version, App, Arg, SubCommand, AppSettings};
use clap::{crate_description, crate_name, crate_version, App, Arg, SubCommand, AppSettings, ArgSettings};

/////UNSAFE
fn string_to_unsafe_static_str(s: String) -> &'static str {
Expand Down Expand Up @@ -65,13 +65,15 @@ pub fn build_cli(show_logo: bool) -> App<'static, 'static> {
.value_name("PATH")
.help("specify a dir to load color configs from")
.takes_value(true)
.set(ArgSettings::RequireEquals)
)
.arg(
Arg::with_name("cache")
.long("cache")
.value_name("PATH")
.help("specify a dir where to dump color caches")
.takes_value(true)
.set(ArgSettings::RequireEquals)
)
.arg(
Arg::with_name("pattern")
Expand All @@ -80,14 +82,16 @@ pub fn build_cli(show_logo: bool) -> App<'static, 'static> {
.help("specify a path to substitute pattern colors")
.takes_value(true)
.multiple(true)
.set(ArgSettings::RequireEquals)
)
.arg(
Arg::with_name("script")
.long("script")
.value_name("PATH")
.value_name("PATH:PATH")
.help("specify a script to run afte colors are generated")
.takes_value(true)
.multiple(true)
.set(ArgSettings::RequireEquals)
)
.subcommand(
SubCommand::with_name("create")
Expand All @@ -100,6 +104,7 @@ pub fn build_cli(show_logo: bool) -> App<'static, 'static> {
.takes_value(true)
.value_name("DIRPATH")
.conflicts_with("image")
.set(ArgSettings::RequireEquals)
)
.arg(
Arg::with_name("palette")
Expand All @@ -109,13 +114,15 @@ pub fn build_cli(show_logo: bool) -> App<'static, 'static> {
.possible_values(&["schemer2", "pigment"])
.default_value("pigment")
.value_name("NAME")
.set(ArgSettings::RequireEquals)
)
.arg(
Arg::with_name("scheme")
.long("scheme")
.value_name("NAME")
.help("specify a color scheme from configs to use")
.takes_value(true)
.set(ArgSettings::RequireEquals)
)
.arg(
Arg::with_name("image")
Expand All @@ -125,6 +132,7 @@ pub fn build_cli(show_logo: bool) -> App<'static, 'static> {
.takes_value(true)
.value_name("FLEPATH")
.conflicts_with("wallpath")
.set(ArgSettings::RequireEquals)
)
.arg(
Arg::with_name("theme")
Expand All @@ -134,6 +142,7 @@ pub fn build_cli(show_logo: bool) -> App<'static, 'static> {
.value_name("THEME")
.possible_values(&["dark", "light"])
.default_value("dark")
.set(ArgSettings::RequireEquals)
)
.arg(
Arg::with_name("action")
Expand All @@ -153,6 +162,7 @@ pub fn build_cli(show_logo: bool) -> App<'static, 'static> {
.takes_value(true)
.default_value("300")
.value_name("SECONDS")
.set(ArgSettings::RequireEquals)
)
.arg(
Arg::with_name("action")
Expand Down Expand Up @@ -193,6 +203,7 @@ pub fn build_cli(show_logo: bool) -> App<'static, 'static> {
.possible_values(&["dark", "light"])
.default_value("dark")
.required(true)
.set(ArgSettings::RequireEquals)
)
)
.subcommand(
Expand All @@ -205,6 +216,7 @@ pub fn build_cli(show_logo: bool) -> App<'static, 'static> {
.visible_aliases(&["source"])
.takes_value(true)
.value_name("FLEPATH")
.set(ArgSettings::RequireEquals)
)
.arg(
Arg::with_name("pattern")
Expand All @@ -213,6 +225,7 @@ pub fn build_cli(show_logo: bool) -> App<'static, 'static> {
.help("specify a path to substitute pattern colors")
.takes_value(true)
.multiple(true)
.set(ArgSettings::RequireEquals)
)
)
}
2 changes: 1 addition & 1 deletion src/cli/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn run(app: &clap::ArgMatches, output: &mut WRITE, scheme: &mut SCHEME) -> R
var::pipe::concatinate(scheme);


scheme.set_script(None);
scheme.set_scripts(None);
if sub.is_present("gen") {
create::new_palette(output, scheme)?;
}
Expand Down
8 changes: 3 additions & 5 deletions src/cli/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ pub fn new_palette(output: &mut WRITE, scheme: &mut SCHEME) -> Result<()> {
if scheme.image().is_none() {
scheme.set_image(Some(helper::random_image(&wallpaper)));
}
println!("{}", &scheme.image().clone().unwrap());

let palette: Vec<String>;
if let Some(content) = scheme.palette() {
match content.as_str() {
"pigment" => {
palette = palette::palette_from_image(scheme.image().clone().unwrap());
println!("{}", &scheme.image().clone().unwrap());
helper::write_temp_file("lule_palette", palette.join("\n").as_bytes());
scheme.set_colors(Some(palette));
},
Expand All @@ -63,8 +61,8 @@ pub fn new_palette(output: &mut WRITE, scheme: &mut SCHEME) -> Result<()> {
write::write_temp(&output);
write::write_cache(&scheme);
write::write_cache_json(scheme, values);
if let Some(_) = scheme.script() {
execute::external_command();
if let Some(_) = scheme.scripts() {
execute::external_command(scheme);
}
scheme.set_image(None);
Ok(())
Expand All @@ -91,6 +89,6 @@ pub fn old_palette(output: &mut WRITE, scheme: &mut SCHEME) -> Result<()> {
write::write_temp(&output);
write::write_cache(&scheme);
write::write_cache_json(scheme, write::output_to_json(output, false));
execute::external_command();
execute::external_command(scheme);
Ok(())
}
9 changes: 7 additions & 2 deletions src/gen/execute.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use std::process::Command;
// use crate::scheme::scheme::*;
use crate::scheme::*;

pub fn external_command(){
pub fn external_command(scheme: &mut SCHEME){
if let Some(scripts) = scheme.scripts() {
for script in scripts.iter() {
println!("{}", script)
}
}
Command::new("bash")
.arg("-c")
.arg("/home/bresilla/code/proj/warp/lule/scripts/lule_colors")
Expand Down
27 changes: 14 additions & 13 deletions src/gen/templ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,35 @@ use crate::scheme::*;
use templar;
use templar::*;

pub fn generate_template(original: PathBuf, _replaced: PathBuf, output: &WRITE) -> Result<()> {
pub fn generate_template(original: PathBuf, replaced: PathBuf, output: &WRITE) -> Result<()> {

let mut content = String::new();
if let Ok(cont) = helper::file_to_string(original) {
content = cont;
}

if let Err(e) = templar::Templar::global().parse(&content) {
println!("{}", e);
};
// if let Err(e) = templar::Templar::global().parse(&content) {
// println!("{}", e);
// };

let template = templar::Templar::global().parse(&content)?;
println!("{}", content);
let mut data: templar::Document = templar::Document::default();
for (i, color) in output.colors().iter().enumerate() {
let name = "color".to_string() + &i.to_string();
data[name] = color.to_rgb_hex_string(true).into();
data[name] = color.to_rgb_hex_string(false).into();
}
data["background"] = output.colors()[0].to_rgb_hex_string(true).into();
data["foreground"] = output.colors()[15].to_rgb_hex_string(true).into();
data["cursor"] = output.colors()[1].to_rgb_hex_string(true).into();
data["accent"] = output.colors()[1].to_rgb_hex_string(true).into();
data["background"] = output.colors()[0].to_rgb_hex_string(false).into();
data["foreground"] = output.colors()[15].to_rgb_hex_string(false).into();
data["cursor"] = output.colors()[1].to_rgb_hex_string(false).into();
data["accent"] = output.colors()[1].to_rgb_hex_string(false).into();

data["wallpaper"] = output.wallpaper().into();
data["theme"] = output.theme().into();

let context = templar::StandardContext::new();
context.set(data)?;

println!("{}", template.render(&context)?);


let new_content = format!("{}", template.render(&context)?);
helper::write_to_file(replaced, new_content.as_bytes());
Ok(())
}
9 changes: 6 additions & 3 deletions src/scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ pub struct SCHEME {
walldir: Option<String>,
config: Option<String>,
cache: Option<String>,
script: Option<String>,
scripts: Option<Vec<String>>,
patterns: Option<Vec<(String, String)>>,
looop: Option<usize>,
theme: Option<String>,
palette: Option<String>,
Expand All @@ -60,7 +61,8 @@ impl SCHEME {
walldir: None,
config: None,
cache: None,
script: None,
scripts: None,
patterns: None,
looop: None,
theme: None,
palette: None,
Expand All @@ -80,7 +82,8 @@ impl SCHEME {
if let Some(value) = new.walldir() { self.walldir = Some(value.clone()); }
if let Some(value) = new.config() { self.config = Some(value.clone()); }
if let Some(value) = new.cache() { self.cache = Some(value.clone()); }
if let Some(value) = new.script() { self.script = Some(value.clone()); }
if let Some(value) = new.scripts() { self.scripts = Some(value.clone()); }
if let Some(value) = new.patterns() { self.patterns = Some(value.clone()); }
if let Some(value) = new.theme() { self.theme = Some(value.clone()); }
if let Some(value) = new.palette() { self.palette = Some(value.clone()); }
if let Some(value) = new.sort() { self.sort = Some(value.clone()); }
Expand Down
32 changes: 32 additions & 0 deletions src/var/args.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,40 @@
use clap;
use crate::scheme::*;
use crate::helper;
use std::fs;

pub fn concatinate(app: &clap::ArgMatches, scheme: &mut SCHEME) {

if let Some(_) = app.values_of("script") {
let vals: Vec<&str> = app.values_of("script").unwrap().collect();
let mut scripts = Vec::new();
if let Some(s) = scheme.scripts() {
scripts = s.to_vec();
}
for val in vals {
scripts.push(val.to_string())
}
scripts.retain(|x| fs::metadata(x).is_ok());
scheme.set_scripts(Some(scripts));
}

if let Some(_) = app.values_of("pattern") {
let vals: Vec<&str> = app.values_of("pattern").unwrap().collect();
let mut patterns = Vec::new();
for val in vals {
let s: Vec<&str> = val.split_terminator(':').collect();
// TODO: better error
if s.len() == 2 {
if fs::metadata(s[0]).is_ok() && fs::metadata(s[1]).is_ok() {
patterns.push((s[0].to_string(), s[1].to_string()));
};
}
}
scheme.set_patterns(Some(patterns));
}



if let Some(sub) = app.subcommand_matches("create"){
if let Some(arg) = sub.value_of("image") {
scheme.set_image(Some(helper::vaid_image(arg)));
Expand Down
12 changes: 11 additions & 1 deletion src/var/envi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ pub fn concatinate(scheme: &mut SCHEME) {

let env_lule_s = std::env::var("LULE_S");
if env_lule_s.is_ok(){
scheme.set_script(Some(env_lule_s.unwrap()));
let mut newvec = vec![env_lule_s.unwrap()];
match scheme.scripts() {
None => {
scheme.set_scripts(Some(newvec));
}
Some(vec) => {
newvec.append(&mut vec.clone());
newvec.retain(|x| std::fs::metadata(x).is_ok());
scheme.set_scripts(Some(newvec));
}
}
}

let env_lule_a = std::env::var("LULE_A");
Expand Down
34 changes: 34 additions & 0 deletions templates/colors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh
# Shell variables
# Generated by 'wal'
wallpaper="#{{ wallpaper }}"

# Special
background='#{{ background }}'
foreground='#{{ foreground }}'
cursor='#{{ cursor }}'

# Colors
color0='#{{ color0 }}'
color1='#{{ color1 }}'
color2='#{{ color2 }}'
color3='#{{ color3 }}'
color4='#{{ color4 }}'
color5='#{{ color5 }}'
color6='#{{ color6 }}'
color7='#{{ color7 }}'
color8='#{{ color8 }}'
color9='#{{ color9 }}'
color10='#{{ color10 }}'
color11='#{{ color11 }}'
color12='#{{ color12 }}'
color13='#{{ color13 }}'
color14='#{{ color14 }}'
color15='#{{ color15 }}'

# FZF colors
export FZF_DEFAULT_OPTS="
$FZF_DEFAULT_OPTS
--color fg:7,bg:0,hl:1,fg+:232,bg+:1,hl+:255
--color info:7,prompt:2,spinner:1,pointer:232,marker:1
"

0 comments on commit fd61ca1

Please sign in to comment.