Skip to content

Commit ef4630a

Browse files
authored
Merge pull request #931 from sandr01d/clear-history-on-start
feat: allow clearing the history on startup
2 parents 4a76dd3 + 630f807 commit ef4630a

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ daemonize = true
177177
# Maximum number of clips in history.
178178
max_history = 50
179179

180+
# Clears the history on startup when set to true
181+
clear_history_on_start = false
182+
180183
# File path for clip history.
181184
# If this value is omitted, `clipcatd` will persist history in `$XDG_CACHE_HOME/clipcat/clipcatd-history`.
182185
history_file_path = "/home/<username>/.cache/clipcat/clipcatd-history"

clipcatd/src/config/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ pub struct Config {
3232
#[serde(default = "Config::default_max_history")]
3333
pub max_history: usize,
3434

35+
#[serde(default)]
36+
pub clear_history_on_start: bool,
37+
3538
#[serde(default = "Config::default_synchronize_selection_with_clipboard")]
3639
pub synchronize_selection_with_clipboard: bool,
3740

@@ -67,6 +70,7 @@ impl Default for Config {
6770
pid_file: Self::default_pid_file_path(),
6871
primary_threshold_ms: Self::default_primary_threshold_ms(),
6972
max_history: Self::default_max_history(),
73+
clear_history_on_start: false,
7074
history_file_path: Self::default_history_file_path(),
7175
synchronize_selection_with_clipboard:
7276
Self::default_synchronize_selection_with_clipboard(),
@@ -208,6 +212,7 @@ impl From<Config> for clipcat_server::Config {
208212
grpc,
209213
primary_threshold_ms,
210214
max_history,
215+
clear_history_on_start,
211216
synchronize_selection_with_clipboard,
212217
history_file_path,
213218
watcher,
@@ -244,6 +249,7 @@ impl From<Config> for clipcat_server::Config {
244249
grpc_access_token,
245250
primary_threshold,
246251
max_history,
252+
clear_history_on_start,
247253
synchronize_selection_with_clipboard,
248254
history_file_path,
249255
watcher,

crates/server/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub struct Config {
1414

1515
pub max_history: usize,
1616

17+
pub clear_history_on_start: bool,
18+
1719
pub synchronize_selection_with_clipboard: bool,
1820

1921
pub history_file_path: PathBuf,

crates/server/src/lib.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub async fn serve_with_shutdown(
5858
grpc_access_token,
5959
primary_threshold,
6060
max_history,
61+
clear_history_on_start,
6162
history_file_path,
6263
synchronize_selection_with_clipboard,
6364
watcher: watcher_opts,
@@ -92,18 +93,27 @@ pub async fn serve_with_shutdown(
9293
.await
9394
.context(error::CreateHistoryManagerSnafu)?;
9495

95-
tracing::info!("Load history from `{path}`", path = history_manager.path().display());
96-
let history_clips = history_manager
97-
.load()
98-
.await
99-
.map_err(|err| {
100-
tracing::error!(
101-
"Could not load history, data might be corrupted, please remove `{path}`, \
102-
error: {err}",
103-
path = history_manager.path().display()
104-
);
105-
})
106-
.unwrap_or_default();
96+
let history_clips = if clear_history_on_start {
97+
tracing::info!("Clearing history at `{path}`", path = history_manager.path().display());
98+
if let Err(err) = history_manager.clear().await {
99+
tracing::error!("Failed to clear clipboard history, error: {err}");
100+
}
101+
Vec::new()
102+
} else {
103+
tracing::info!("Load history from `{path}`", path = history_manager.path().display());
104+
history_manager
105+
.load()
106+
.await
107+
.map_err(|err| {
108+
tracing::error!(
109+
"Could not load history, data might be corrupted, please remove `{path}`, \
110+
error: {err}",
111+
path = history_manager.path().display()
112+
);
113+
})
114+
.unwrap_or_default()
115+
};
116+
107117
let clip_count = history_clips.len();
108118
if clip_count > 0 {
109119
tracing::info!("{clip_count} clip(s) loaded");

0 commit comments

Comments
 (0)