From 04004a612e2854397e5aa1ddca796484d190746a Mon Sep 17 00:00:00 2001 From: Linus-Mussmaecher Date: Sun, 16 Jun 2024 02:41:07 +0200 Subject: [PATCH] Added more various editor/viewer configurability --- README.md | 8 ++++++-- src/config.rs | 4 ++-- src/io/file_manager.rs | 23 ++++++++++++++++++++--- src/io/html_builder.rs | 23 ++++++++++++++++++++--- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6c89f6e..4d33190 100644 --- a/README.md +++ b/README.md @@ -194,8 +194,12 @@ Here is a list of all possible configuration settings: Otherwise, local stats are shown. This setting therefore avoids showing duplicating stats at all times. - `editor` configures the command to edit your notes. - This can be a terminal application or an external application. - - `viewer` configures the command for your HTML viewing application (I use `google-chrome-stable`). If unconfigured, tries to use your systems default application for HTML files. + This is a list of string, containg the programm in the first position and all arguments in the further entries, wherby `%p` will be replaced by the path of the note to be openend. + For example, if you want to run `my_editor --theme --path `, put `["my_editor", "--theme", "", "--path", "%p"]`. + If unconfigured, tries to use your `EDITOR` environment variable or system default application. + - `viewer` configures the command for your HTML viewing application, see `editor` for details. + I use `["firefox", "-P", "", "%p"]. + If unconfigured, tries to use your systems default application for HTML files. The following configuration options manage the HTML files created by rucola: - `enable_html` is set to `true` by default, causing all your notes to be converted to HTML files on program start and for those HTMLs to be continuously kept up-to-date in case of file changes. Set to `false` to never create HTMLs in the background. diff --git a/src/config.rs b/src/config.rs index 391f918..b135d7d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -18,9 +18,9 @@ pub struct Config { /// When to show the global stats area pub(crate) stats_show: ui::screen::StatsShow, /// The editor to use for notes - pub(crate) editor: Option, + pub(crate) editor: Option>, /// Viewer to open html files with - pub(crate) viewer: Option, + pub(crate) viewer: Option>, /// When set to true, HTML files are mass-created on start and continuously kept up to date with file changes instead of being created on-demand. pub(crate) enable_html: bool, /// Path to .css file to style htmls with. diff --git a/src/io/file_manager.rs b/src/io/file_manager.rs index bb669ff..197bf96 100644 --- a/src/io/file_manager.rs +++ b/src/io/file_manager.rs @@ -1,5 +1,5 @@ use crate::{data, error}; -use std::{fs, io::Write, path}; +use std::{fs, io::Write, path, process}; /// Saves configurations to manipulate the file system the notes are stored in. #[derive(Debug, Clone)] @@ -9,7 +9,7 @@ pub struct FileManager { /// Default file ending for newly created notes default_extension: String, /// The editor to use for notes - editor: Option, + editor: Option>, } impl Default for FileManager { fn default() -> Self { @@ -228,7 +228,24 @@ impl FileManager { self.editor .as_ref() // create a command from it - .map(|editor_string| open::with_command(path, editor_string)) + .and_then(|editor_arg_list| { + let mut iter = editor_arg_list.iter(); + if let Some(programm) = iter.next() { + let mut cmd = process::Command::new(programm); + for arg in iter { + if arg == "%p" { + // special argument for the user to indicate where to put the path + cmd.arg(&path); + } else { + // all other arguments are appended in order + cmd.arg(arg); + } + } + Some(cmd) + } else { + None + } + }) // Try the $EDITOR variable .or_else(|| { std::env::var("EDITOR") diff --git a/src/io/html_builder.rs b/src/io/html_builder.rs index 256b176..5ace809 100644 --- a/src/io/html_builder.rs +++ b/src/io/html_builder.rs @@ -1,4 +1,4 @@ -use std::{fs, io::Write, path}; +use std::{fs, io::Write, path, process}; use crate::{data, error}; @@ -18,7 +18,7 @@ pub struct HtmlBuilder { /// A list of strings to replace in math mode to mimic latex commands math_replacements: Vec<(String, String)>, /// Viewer to open html files with - viewer: Option, + viewer: Option>, } impl Default for HtmlBuilder { @@ -199,7 +199,24 @@ impl HtmlBuilder { self.viewer .as_ref() // create a command from it - .map(|viewer_string| open::with_command(&path, viewer_string)) + .and_then(|viewer_arg_list| { + let mut iter = viewer_arg_list.iter(); + if let Some(programm) = iter.next() { + let mut cmd = process::Command::new(programm); + for arg in iter { + if arg == "%p" { + // special argument for the user to indicate where to put the path + cmd.arg(&path); + } else { + // all other arguments are appended in order + cmd.arg(arg); + } + } + Some(cmd) + } else { + None + } + }) // if it was not there, take the default command .or_else(|| open::commands(&path).pop()) // if it was also not there, throw an error