mirror of
https://github.com/narrrl/proton-drive-linux.git
synced 2026-11-19 01:55:04 +01:00
feat(gui,cli,core): add excluded-file patterns from the Sync page, for all folders or one (#99)
The Sync page's Folders view gets an Excluded Files group under the folder list: one row for the patterns of all synced folders (opens Preferences → Sync) and one per folder kept on this computer with its count of own patterns (opens that folder's .pdfsignore editor). Add Pattern asks for a pattern and where it goes: all synced folders or one of them. pdfs-core gains add_folder_pattern/remove_folder_pattern, which edit a folder's rules line by line and read them again when sync changed the file in between, and AppDirs::change_ignore_patterns, now shared by the CLI and Preferences. `pdfs sync ignore list|add|remove` take --folder. Preferences → Sync repaints from the config each time it opens, so a pattern added elsewhere shows there.
This commit is contained in:
1 parent
38a8db0e3e
commit
8598aff7dd
23 files changed
+4396
-2526
No files matched your search
@@ -391,6 +391,27 @@ impl AppConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply `edit` to the ignore patterns in effect in the config at `path`,
|
||||
/// under the config lock, and return the saved config. A list equal to the
|
||||
/// built-in one is stored as unset, so it follows the built-in list in later
|
||||
/// releases.
|
||||
pub fn change_ignore_patterns_at(
|
||||
path: &Path,
|
||||
edit: impl FnOnce(&mut Vec<String>),
|
||||
) -> Result<AppConfig> {
|
||||
update_config_at(path, |config| {
|
||||
let mut list = config.resolved_ignore_patterns();
|
||||
edit(&mut list);
|
||||
let builtin = list.len() == DEFAULT_IGNORE_PATTERNS.len()
|
||||
&& list
|
||||
.iter()
|
||||
.zip(DEFAULT_IGNORE_PATTERNS)
|
||||
.all(|(a, b)| a == b);
|
||||
config.ignore_patterns = (!builtin).then_some(list);
|
||||
config.clone()
|
||||
})
|
||||
}
|
||||
|
||||
/// Fail when the config file at `path` exists but does not parse, so that
|
||||
/// [`AppDirs::save_config`] never writes over settings it could not read.
|
||||
fn refuse_to_replace_unreadable(path: &Path) -> Result<()> {
|
||||
@@ -565,6 +586,13 @@ impl AppDirs {
|
||||
update_config_at(&self.config_path(), change)
|
||||
}
|
||||
|
||||
/// Apply `edit` to the ignore patterns in effect for every synced folder
|
||||
/// and save them, returning the saved config. See
|
||||
/// [`change_ignore_patterns_at`].
|
||||
pub fn change_ignore_patterns(&self, edit: impl FnOnce(&mut Vec<String>)) -> Result<AppConfig> {
|
||||
change_ignore_patterns_at(&self.config_path(), edit)
|
||||
}
|
||||
|
||||
/// This machine's id (e.g. ~/.config/proton-drive-linux/machine-id). See
|
||||
/// [`machine_id`](Self::machine_id).
|
||||
pub fn machine_id_path(&self) -> PathBuf {
|
||||
@@ -798,6 +826,31 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignore_patterns_back_at_the_built_in_list_are_stored_as_unset() {
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"pdfs-cfg-ignore-{}-{:?}",
|
||||
std::process::id(),
|
||||
std::thread::current().id()
|
||||
));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("config.json");
|
||||
|
||||
let config = change_ignore_patterns_at(&path, |list| list.push("*.db".into())).unwrap();
|
||||
assert_eq!(
|
||||
config.ignore_patterns.as_ref().unwrap().last().unwrap(),
|
||||
"*.db"
|
||||
);
|
||||
let config = change_ignore_patterns_at(&path, |list| list.retain(|p| p != "*.db")).unwrap();
|
||||
assert_eq!(config.ignore_patterns, None);
|
||||
assert_eq!(load_config_at(&path).ignore_patterns, None);
|
||||
// An empty list is a choice, not the built-in one.
|
||||
let config = change_ignore_patterns_at(&path, Vec::clear).unwrap();
|
||||
assert_eq!(config.ignore_patterns, Some(Vec::new()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_change_is_never_applied_over_an_unreadable_config() {
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
|
||||
@@ -262,6 +262,82 @@ impl FolderRules {
|
||||
}
|
||||
Ok(written?)
|
||||
}
|
||||
|
||||
/// The patterns in the rules: every line that is not blank or a comment,
|
||||
/// trimmed of surrounding white space.
|
||||
pub fn patterns(&self) -> Vec<&str> {
|
||||
self.text
|
||||
.as_deref()
|
||||
.unwrap_or_default()
|
||||
.lines()
|
||||
.map(str::trim)
|
||||
.filter(|line| !line.is_empty() && !line.starts_with('#'))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// How often [`add_folder_pattern`] and [`remove_folder_pattern`] read the
|
||||
/// rules again when they changed between the read and the write.
|
||||
const RULES_RETRIES: usize = 3;
|
||||
|
||||
/// Add `pattern` at the end of the own rules of the folder at `root`, keeping
|
||||
/// every other line and comment. Says whether it added it: a pattern already
|
||||
/// there is left alone.
|
||||
pub fn add_folder_pattern(root: &Path, pattern: &str) -> std::io::Result<bool> {
|
||||
let pattern = pattern.trim();
|
||||
edit_folder_rules(root, |rules| {
|
||||
if rules.patterns().contains(&pattern) {
|
||||
return None;
|
||||
}
|
||||
let mut text = rules.text.clone().unwrap_or_default();
|
||||
if !text.is_empty() && !text.ends_with('\n') {
|
||||
text.push('\n');
|
||||
}
|
||||
text.push_str(pattern);
|
||||
text.push('\n');
|
||||
Some(text)
|
||||
})
|
||||
}
|
||||
|
||||
/// Remove every line that is `pattern` from the own rules of the folder at
|
||||
/// `root`. Says whether there was one.
|
||||
pub fn remove_folder_pattern(root: &Path, pattern: &str) -> std::io::Result<bool> {
|
||||
let pattern = pattern.trim();
|
||||
edit_folder_rules(root, |rules| {
|
||||
if !rules.patterns().contains(&pattern) {
|
||||
return None;
|
||||
}
|
||||
let text = rules.text.as_deref().unwrap_or_default();
|
||||
Some(
|
||||
text.lines()
|
||||
.filter(|line| line.trim() != pattern)
|
||||
.map(|line| format!("{line}\n"))
|
||||
.collect(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Read the folder's rules, let `edit` return their new text (or `None` to
|
||||
/// leave them), and write it. Rules that changed in between, such as by sync
|
||||
/// bringing in an edit from another computer, are read again and edited anew.
|
||||
fn edit_folder_rules(
|
||||
root: &Path,
|
||||
edit: impl Fn(&FolderRules) -> Option<String>,
|
||||
) -> std::io::Result<bool> {
|
||||
for _ in 0..RULES_RETRIES {
|
||||
let rules = FolderRules::read(root)?;
|
||||
let Some(text) = edit(&rules) else {
|
||||
return Ok(false);
|
||||
};
|
||||
match rules.write(&text) {
|
||||
Ok(()) => return Ok(true),
|
||||
Err(WriteRulesError::Changed) => continue,
|
||||
Err(WriteRulesError::Io(e)) => return Err(e),
|
||||
}
|
||||
}
|
||||
Err(std::io::Error::other(
|
||||
"the rules kept changing while they were being saved",
|
||||
))
|
||||
}
|
||||
|
||||
/// Where a rule that decided about a path came from.
|
||||
@@ -639,6 +715,44 @@ mod tests {
|
||||
assert!(matches!(rules.write("c/"), Err(WriteRulesError::Changed)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_pattern_added_to_a_folder_keeps_its_other_lines() {
|
||||
let dir = TempDir::new();
|
||||
assert!(add_folder_pattern(dir.path(), " *.db ").unwrap());
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(dir.path().join(".pdfsignore")).unwrap(),
|
||||
"*.db\n"
|
||||
);
|
||||
|
||||
write(dir.path(), ".pdfsignore", "# my notes\nbuild/");
|
||||
assert!(add_folder_pattern(dir.path(), "*.db").unwrap());
|
||||
assert!(!add_folder_pattern(dir.path(), "build/").unwrap());
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(dir.path().join(".pdfsignore")).unwrap(),
|
||||
"# my notes\nbuild/\n*.db\n"
|
||||
);
|
||||
assert_eq!(
|
||||
FolderRules::read(dir.path()).unwrap().patterns(),
|
||||
["build/", "*.db"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removing_a_folder_pattern_keeps_comments_and_drops_an_empty_file() {
|
||||
let dir = TempDir::new();
|
||||
write(dir.path(), ".pdfsignore", "# keep\nbuild/\n*.db\n");
|
||||
assert!(remove_folder_pattern(dir.path(), "build/").unwrap());
|
||||
assert!(!remove_folder_pattern(dir.path(), "build/").unwrap());
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(dir.path().join(".pdfsignore")).unwrap(),
|
||||
"# keep\n*.db\n"
|
||||
);
|
||||
|
||||
write(dir.path(), ".pdfsignore", "*.db\n");
|
||||
assert!(remove_folder_pattern(dir.path(), "*.db").unwrap());
|
||||
assert!(!dir.path().join(".pdfsignore").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_rules_at_all_ignores_nothing() {
|
||||
let dir = TempDir::new();
|
||||
|
||||
Reference in new issue
Block a user