Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"note_save_on_close": false,
"note_always_overwrite": false,
"note_images_as_links": false,
"note_external_editor": "wezterm",
"note_more_limit": 5,
"enable_toasts": true,
"toast_duration": 3.0,
Expand Down
14 changes: 3 additions & 11 deletions src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ pub use fav_dialog::FavDialog;
pub use image_panel::ImagePanel;
pub use macro_dialog::MacroDialog;
pub use note_panel::{
build_nvim_command, build_wezterm_command, extract_links, show_wiki_link, NotePanel,
build_nvim_command, build_wezterm_command, extract_links, show_wiki_link, spawn_external,
NotePanel,
};
pub use notes_dialog::NotesDialog;
pub use screenshot_editor::ScreenshotEditor;
Expand Down Expand Up @@ -368,7 +369,6 @@ pub struct LauncherApp {
pub note_save_on_close: bool,
pub note_always_overwrite: bool,
pub note_images_as_links: bool,
pub note_external_editor: Option<String>,
pub note_external_open: NoteExternalOpen,
pub note_font_size: f32,
pub note_more_limit: usize,
Expand Down Expand Up @@ -514,7 +514,6 @@ impl LauncherApp {
note_always_overwrite: Option<bool>,
note_images_as_links: Option<bool>,
note_more_limit: Option<usize>,
note_external_editor: Option<String>,
) {
self.plugin_dirs = plugin_dirs;
self.index_paths = index_paths;
Expand Down Expand Up @@ -609,9 +608,6 @@ impl LauncherApp {
if let Some(v) = note_more_limit {
self.note_more_limit = v;
}
if note_external_editor.is_some() {
self.note_external_editor = note_external_editor;
}
}

pub fn new(
Expand Down Expand Up @@ -872,7 +868,6 @@ impl LauncherApp {
note_save_on_close: settings.note_save_on_close,
note_always_overwrite: settings.note_always_overwrite,
note_images_as_links: settings.note_images_as_links,
note_external_editor: settings.note_external_editor.clone(),
note_external_open,
note_font_size: 16.0,
note_more_limit: settings.note_more_limit,
Expand Down Expand Up @@ -2901,10 +2896,7 @@ impl eframe::App for LauncherApp {
if self.open_note_in_neovim(
&slug,
crate::plugins::note::load_notes,
|path| {
let (mut cmd, _cmd_str) = build_nvim_command(path);
cmd.spawn().map(|_| ())
},
|path| spawn_external(path, NoteExternalOpen::Wezterm),
) {
ui.close_menu();
}
Expand Down
42 changes: 25 additions & 17 deletions src/gui/note_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use once_cell::sync::Lazy;
use regex::Regex;
use rfd::FileDialog;
use std::collections::HashMap;
use std::process::Command;
#[cfg(windows)]
use std::os::windows::process::CommandExt;
use std::process::Command;
use std::{
env,
path::{Path, PathBuf},
Expand Down Expand Up @@ -965,25 +965,33 @@ impl NotePanel {

fn open_external(&self, app: &mut LauncherApp, choice: NoteExternalOpen) {
let path = self.note.path.clone();
let result = match choice {
NoteExternalOpen::Powershell => {
let (mut cmd, _cmd_str) = build_nvim_command(&path);
cmd.spawn()
}
NoteExternalOpen::Wezterm => {
let editor = app.note_external_editor.as_deref().unwrap_or("nvim");
let (mut cmd, _cmd_str) = build_wezterm_command(&path, editor);
cmd.spawn()
}
NoteExternalOpen::Notepad => Command::new("notepad.exe").arg(&path).spawn(),
NoteExternalOpen::Neither => return,
};
if let Err(e) = result {
if let Err(e) = spawn_external(&path, choice) {
app.set_error(format!("Failed to open note externally: {e}"));
}
}
}

pub fn spawn_external(path: &Path, choice: NoteExternalOpen) -> std::io::Result<()> {
match choice {
NoteExternalOpen::Powershell => {
let (mut cmd, _cmd_str) = build_nvim_command(path);
cmd.spawn().map(|_| ())
}
NoteExternalOpen::Wezterm => {
let (mut cmd, _cmd_str) = build_wezterm_command(path);
match cmd.spawn() {
Ok(_) => Ok(()),
Err(_) => {
let (mut cmd, _cmd_str) = build_nvim_command(path);
cmd.spawn().map(|_| ())
}
}
}
NoteExternalOpen::Notepad => Command::new("notepad.exe").arg(path).spawn().map(|_| ()),
NoteExternalOpen::Neither => Ok(()),
}
}

pub fn show_wiki_link(ui: &mut egui::Ui, app: &mut LauncherApp, l: &str) -> egui::Response {
// Display wiki style links with brackets and allow clicking to
// navigate to the referenced note. Missing targets are colored red.
Expand Down Expand Up @@ -1086,9 +1094,9 @@ pub fn build_nvim_command(note_path: &Path) -> (Command, String) {
(cmd, cmd_str)
}

pub fn build_wezterm_command(note_path: &Path, editor: &str) -> (Command, String) {
pub fn build_wezterm_command(note_path: &Path) -> (Command, String) {
let mut cmd = Command::new("wezterm");
cmd.arg("start").arg("--").arg(editor).arg(note_path);
cmd.arg("start").arg("--").arg("nvim").arg(note_path);
#[cfg(windows)]
{
cmd.creation_flags(0x08000000); // CREATE_NO_WINDOW
Expand Down
1 change: 0 additions & 1 deletion src/plugin_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ impl PluginEditor {
Some(s.note_always_overwrite),
Some(s.note_images_as_links),
Some(s.note_more_limit),
s.note_external_editor.clone(),
);
let dirs = s.plugin_dirs.clone().unwrap_or_default();
let actions_arc = Arc::clone(&app.actions);
Expand Down
5 changes: 0 additions & 5 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ pub struct Settings {
/// textures directly in the preview.
#[serde(default)]
pub note_images_as_links: bool,
/// External editor command used to open notes. If `None`, a platform
/// specific fallback is used.
#[serde(default)]
pub note_external_editor: Option<String>,
/// Number of tags or links shown before an expandable "... (more)" control
/// appears in the note panel.
#[serde(default = "default_note_more_limit")]
Expand Down Expand Up @@ -276,7 +272,6 @@ impl Default for Settings {
note_save_on_close: default_note_save_on_close(),
note_always_overwrite: false,
note_images_as_links: false,
note_external_editor: Some("wezterm".to_string()),
note_more_limit: default_note_more_limit(),
enable_toasts: true,
toast_duration: default_toast_duration(),
Expand Down
18 changes: 0 additions & 18 deletions src/settings_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::plugins::screenshot::ScreenshotPluginSettings;
use crate::settings::Settings;
use eframe::egui;
use egui_toast::{Toast, ToastKind, ToastOptions};
use rfd::FileDialog;
use std::sync::Arc;

#[derive(Default)]
Expand Down Expand Up @@ -34,7 +33,6 @@ pub struct SettingsEditor {
note_always_overwrite: bool,
note_images_as_links: bool,
note_more_limit: usize,
note_external_editor: String,
query_scale: f32,
list_scale: f32,
history_limit: usize,
Expand Down Expand Up @@ -124,7 +122,6 @@ impl SettingsEditor {
note_always_overwrite: settings.note_always_overwrite,
note_images_as_links: settings.note_images_as_links,
note_more_limit: settings.note_more_limit,
note_external_editor: settings.note_external_editor.clone().unwrap_or_default(),
query_scale: settings.query_scale.unwrap_or(1.0),
list_scale: settings.list_scale.unwrap_or(1.0),
history_limit: settings.history_limit,
Expand Down Expand Up @@ -239,11 +236,6 @@ impl SettingsEditor {
note_always_overwrite: self.note_always_overwrite,
note_images_as_links: self.note_images_as_links,
note_more_limit: self.note_more_limit,
note_external_editor: if self.note_external_editor.trim().is_empty() {
None
} else {
Some(self.note_external_editor.clone())
},
query_scale: Some(self.query_scale),
list_scale: Some(self.list_scale),
history_limit: self.history_limit,
Expand Down Expand Up @@ -535,15 +527,6 @@ impl SettingsEditor {
.clamp_range(1..=usize::MAX),
);
});
ui.horizontal(|ui| {
ui.label("External editor");
ui.text_edit_singleline(&mut self.note_external_editor);
if ui.button("Browse").clicked() {
if let Some(file) = FileDialog::new().pick_file() {
self.note_external_editor = file.display().to_string();
}
}
});
let mut cfg = self
.plugin_settings
.get("note")
Expand Down Expand Up @@ -676,7 +659,6 @@ impl SettingsEditor {
Some(new_settings.note_always_overwrite),
Some(new_settings.note_images_as_links),
Some(new_settings.note_more_limit),
new_settings.note_external_editor.clone(),
);
ctx.send_viewport_cmd(
egui::ViewportCommand::WindowLevel(
Expand Down
34 changes: 31 additions & 3 deletions tests/hide_after_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,37 @@ fn run_action(action: &str) -> bool {
}];
let (mut app, flag) = new_app_with_settings(&ctx, actions, Settings::default());
app.update_paths(
None, None, None, None, None, None, None, None, None, None, None, None,
None, Some(true), None, None, None, None, None, None, None, None, None,
None, None, None, None, None, None, None, None, None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
Some(true),
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
);
flag.store(true, Ordering::SeqCst);
let a = app.results[0].clone();
Expand Down
50 changes: 40 additions & 10 deletions tests/note_panel_shell.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![cfg(windows)]
use multi_launcher::gui::build_nvim_command;
use multi_launcher::gui::{build_nvim_command, build_wezterm_command};
use once_cell::sync::Lazy;
use std::env;
use std::fs;
Expand All @@ -25,10 +25,7 @@ fn prefers_powershell7_then_powershell_then_cmd() {
env::set_var("PATH", "");
}
let (cmd, _) = build_nvim_command(note);
assert!(cmd
.get_program()
.to_string_lossy()
.ends_with("pwsh.exe"));
assert!(cmd.get_program().to_string_lossy().ends_with("pwsh.exe"));
let args: Vec<_> = cmd
.get_args()
.map(|a| a.to_string_lossy().into_owned())
Expand All @@ -51,12 +48,11 @@ fn prefers_powershell7_then_powershell_then_cmd() {
.ends_with("powershell.exe"));

// Fallback to cmd.exe
unsafe { env::set_var("PATH", ""); }
unsafe {
env::set_var("PATH", "");
}
let (cmd, _) = build_nvim_command(note);
assert!(cmd
.get_program()
.to_string_lossy()
.ends_with("cmd.exe"));
assert!(cmd.get_program().to_string_lossy().ends_with("cmd.exe"));
let args: Vec<_> = cmd
.get_args()
.map(|a| a.to_string_lossy().into_owned())
Expand All @@ -74,3 +70,37 @@ fn prefers_powershell7_then_powershell_then_cmd() {
}
}
}

#[test]
fn wezterm_fallbacks_to_powershell_when_missing() {
let _lock = TEST_MUTEX.lock().unwrap();
let note = Path::new("note.txt");
let orig_ps7 = env::var_os("ML_PWSH7_PATH");
let orig_path = env::var_os("PATH");

let dir = tempdir().unwrap();
let pwsh = dir.path().join("pwsh.exe");
fs::write(&pwsh, "").unwrap();
unsafe {
env::set_var("ML_PWSH7_PATH", &pwsh);
env::set_var("PATH", "");
}

let (mut cmd, _) = build_wezterm_command(note);
let res = cmd.spawn();
assert!(res.is_err());

let (cmd, _) = build_nvim_command(note);
assert!(cmd.get_program().to_string_lossy().ends_with("pwsh.exe"));

unsafe {
match orig_ps7 {
Some(v) => env::set_var("ML_PWSH7_PATH", v),
None => env::remove_var("ML_PWSH7_PATH"),
}
match orig_path {
Some(v) => env::set_var("PATH", v),
None => env::remove_var("PATH"),
}
}
}
4 changes: 2 additions & 2 deletions tests/wezterm_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use std::path::Path;
#[test]
fn builds_wezterm_command() {
let note = Path::new("note.txt");
let (cmd, _cmd_str) = build_wezterm_command(note, "wezterm");
let (cmd, _cmd_str) = build_wezterm_command(note);
assert_eq!(cmd.get_program().to_string_lossy(), "wezterm");
let args: Vec<_> = cmd
.get_args()
.map(|a| a.to_string_lossy().into_owned())
.collect();
assert_eq!(args, ["start", "--", "wezterm", "note.txt"]);
assert_eq!(args, ["start", "--", "nvim", "note.txt"]);
}
Loading