Skip to content

Commit

Permalink
except_mothers_day option
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Bargamon committed Feb 20, 2021
1 parent 2ae9600 commit 8b437a3
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 15 deletions.
43 changes: 43 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ url = "2.2.0"
rustop = "1.1.1"
rand = "0.8.3"
confy="0.4.0"
serde = { version = "1.0", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
chrono = "0.4"
6 changes: 4 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ In 0.4.0, we introduced the toml config. Clink will create a "clink.toml" file i
# You can find detail description of modes bellow
# one of: remove, your_mom, evil
mode = 'remove'
# text for your_mom mode
# Text for your_mom mode
your_mom = 'your_mom'
# If true in your_mom mode, Clink will automatically switch to the remove mode in Mother's Day.
except_mothers_day: true,
# How often Clink will check clipboard in milliseconds
sleep_duration = 150
# which GET params Clink should update
# Which GET params Clink should update
params = [
'fbclid', # Facebook click identifier
'gclid', # Google click identifier
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::time::Duration;
pub struct ClinkConfig {
mode: Mode,
your_mom: String,
except_mothers_day: bool,
sleep_duration: u64,
params: Vec<String>,
}
Expand All @@ -25,8 +26,9 @@ impl ::std::default::Default for ClinkConfig {
Self {
mode: Mode::Remove,
your_mom: "your_mom".to_string(),
params: get_default_params(),
except_mothers_day: true,
sleep_duration: 150,
params: get_default_params(),
}
}
}
Expand Down
63 changes: 52 additions & 11 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::mode::Mode;
use crate::params::{create_index, is_hit};
use crate::ClinkConfig;
use chrono::prelude::*;
use linkify::{LinkFinder, LinkKind};
use rand::Rng;
use std::collections::HashMap;
use url::Url;

#[cfg(test)]
Expand All @@ -18,6 +20,7 @@ mod find_and_replace {
&ClinkConfig {
mode: Mode::Remove,
your_mom: "your_mom".to_string(),
except_mothers_day: false,
sleep_duration: 150,
params: get_default_params(),
}
Expand All @@ -30,6 +33,7 @@ mod find_and_replace {
&ClinkConfig {
mode: Mode::YourMom,
your_mom: "your_mom".to_string(),
except_mothers_day: false,
sleep_duration: 150,
params: get_default_params()
}
Expand All @@ -42,6 +46,7 @@ mod find_and_replace {
&ClinkConfig {
mode: Mode::Evil,
your_mom: "your_mom".to_string(),
except_mothers_day: false,
sleep_duration: 150,
params: get_default_params()
}
Expand All @@ -57,6 +62,7 @@ mod find_and_replace {
&ClinkConfig {
mode: Mode::Remove,
your_mom: "your_mom".to_string(),
except_mothers_day: false,
sleep_duration: 150,
params: get_default_params()
}
Expand All @@ -69,6 +75,7 @@ mod find_and_replace {
&ClinkConfig {
mode: Mode::YourMom,
your_mom: "your_mom".to_string(),
except_mothers_day: false,
sleep_duration: 150,
params: get_default_params()
}
Expand All @@ -84,6 +91,7 @@ mod find_and_replace {
&ClinkConfig {
mode: Mode::Remove,
your_mom: "your_mom".to_string(),
except_mothers_day: false,
sleep_duration: 150,
params: get_default_params()
}
Expand All @@ -96,6 +104,7 @@ mod find_and_replace {
&ClinkConfig {
mode: Mode::YourMom,
your_mom: "your_mom".to_string(),
except_mothers_day: false,
sleep_duration: 150,
params: get_default_params()
}
Expand All @@ -111,6 +120,7 @@ mod find_and_replace {
&ClinkConfig {
mode: Mode::Remove,
your_mom: "your_mom".to_string(),
except_mothers_day: false,
sleep_duration: 150,
params: get_default_params()
}
Expand All @@ -123,6 +133,7 @@ mod find_and_replace {
&ClinkConfig {
mode: Mode::YourMom,
your_mom: "your_mom".to_string(),
except_mothers_day: false,
sleep_duration: 150,
params: get_default_params()
}
Expand All @@ -138,6 +149,7 @@ mod find_and_replace {
& ClinkConfig {
mode: Mode::Remove,
your_mom: "your_mom".to_string(),
except_mothers_day: false,
sleep_duration: 150,
params: get_default_params()
}
Expand All @@ -150,6 +162,7 @@ mod find_and_replace {
&ClinkConfig {
mode: Mode::YourMom,
your_mom: "your_mom".to_string(),
except_mothers_day: false,
sleep_duration: 150,
params: get_default_params()
}
Expand All @@ -165,6 +178,7 @@ mod find_and_replace {
&ClinkConfig {
mode: Mode::YourMom,
your_mom: "foo".to_string(),
except_mothers_day: false,
sleep_duration: 150,
params: get_default_params()
}
Expand All @@ -181,6 +195,7 @@ mod find_and_replace {
&ClinkConfig {
mode: Mode::YourMom,
your_mom: "your_mom".to_string(),
except_mothers_day: false,
sleep_duration: 150,
params: vec!["foo".to_string()]
}
Expand Down Expand Up @@ -219,19 +234,19 @@ fn process_query(
) -> Vec<(String, String)> {
let index = create_index(&config.params);
match config.mode {
Mode::Remove => query
.filter(|p| !is_hit(&p.0, &index))
.map(|p| (p.0.to_string(), p.1.to_string()))
.collect(),
Mode::YourMom => query
.map(|p| {
if is_hit(&p.0, &index) {
(p.0.to_string(), config.your_mom.clone())
Mode::Remove => filter(query, &index),
Mode::YourMom => {
if config.except_mothers_day {
let date = Utc::today();
if date.month() == 5 && date.day() == 9 {
filter(query, &index)
} else {
(p.0.to_string(), p.1.to_string())
your_mom(query, &index, config)
}
})
.collect(),
} else {
your_mom(query, &index, config)
}
}
Mode::Evil => {
let mut rng = rand::thread_rng();
query
Expand Down Expand Up @@ -269,3 +284,29 @@ fn swap_two_chars(s: &str, a: usize, b: usize) -> String {
char_vector.swap(a, b);
char_vector.iter().collect()
}

fn filter(
query: url::form_urlencoded::Parse<'_>,
index: &HashMap<String, bool>,
) -> Vec<(String, String)> {
query
.filter(|p| !is_hit(&p.0, index))
.map(|p| (p.0.to_string(), p.1.to_string()))
.collect()
}

fn your_mom(
query: url::form_urlencoded::Parse<'_>,
index: &HashMap<String, bool>,
config: &ClinkConfig,
) -> Vec<(String, String)> {
query
.map(|p| {
if is_hit(&p.0, &index) {
(p.0.to_string(), config.your_mom.clone())
} else {
(p.0.to_string(), p.1.to_string())
}
})
.collect()
}

0 comments on commit 8b437a3

Please sign in to comment.