Skip to content

Commit eaad1a9

Browse files
committed
fix: Open editor handling
1 parent 35c769c commit eaad1a9

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/app_ui/widgets/question_list.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::deserializers;
1818
use crate::deserializers::editor_data::CodeSnippet;
1919
use crate::deserializers::run_submit::{ParsedResponse, Success};
2020
use crate::entities::TopicTagModel;
21-
use crate::errors::AppResult;
21+
use crate::errors::{AppResult, LcAppError};
2222
use crate::graphql::run_code::RunCode;
2323
use crate::graphql::submit_code::SubmitCode;
2424
use crate::graphql::{Language, RunOrSubmitCode};
@@ -323,22 +323,44 @@ impl QuestionListWidget {
323323
self.pending_event_actions.contains(key)
324324
}
325325

326-
fn open_vim_editor(&mut self, file_name: &Path) {
327-
let vim_cmd = format!("nvim {}", file_name.display());
326+
fn open_vim_like_editor(&mut self, file_name: &Path, editor: &str) -> AppResult<()> {
328327
let mut output = std::process::Command::new("sh")
329328
.arg("-c")
330-
.arg(&vim_cmd)
329+
.arg(&format!("{} {}", editor, file_name.display()))
331330
.spawn()
332-
.expect("Can't run vim cmd");
331+
.map_err(|e| LcAppError::EditorOpen(format!("Can't spawn {} editor: {e}", editor)))?;
333332
self.vim_running
334333
.store(true, std::sync::atomic::Ordering::Relaxed);
335-
let vim_cmd_result = output.wait().expect("Run exits ok");
334+
let vim_cmd_result = output
335+
.wait()
336+
.map_err(|e| LcAppError::EditorOpen(format!("Editor Error: {e}")))?;
336337
self.vim_running
337338
.store(false, std::sync::atomic::Ordering::Relaxed);
338339
self.vim_tx.blocking_send(1).unwrap();
339340
if !vim_cmd_result.success() {
340-
println!("error vim");
341+
return Err(LcAppError::EditorOpen(
342+
"Cannot open editor, Reason: Unknown".to_string(),
343+
));
341344
}
345+
Ok(())
346+
}
347+
348+
fn open_editor(&mut self, file_name: &Path) -> AppResult<()> {
349+
if let Ok(editor) = std::env::var("EDITOR") {
350+
if editor.contains("vim") || editor.contains("nano") {
351+
self.open_vim_like_editor(file_name, editor.as_str())?;
352+
} else {
353+
std::process::Command::new("sh")
354+
.arg("-c")
355+
.arg(&format!("{} {}", editor, file_name.display()))
356+
.spawn()?
357+
.wait()?;
358+
}
359+
} else {
360+
// try open vim
361+
self.open_vim_like_editor(file_name, "vim")?;
362+
}
363+
Ok(())
342364
}
343365

344366
fn popup_list_notification(
@@ -957,12 +979,20 @@ impl super::Widget for QuestionListWidget {
957979
Some(selected_snippet),
958980
question_id.to_string(),
959981
);
982+
let save_path = sf.get_save_path(&dir);
960983
sf.create_if_not_exists(&dir)?;
961-
self.open_vim_editor(&sf.get_save_path(&dir));
962984
self.files
963985
.entry(sf.question_id.parse().unwrap())
964986
.or_default()
965987
.insert(sf);
988+
989+
if let Err(e) = self.open_editor(&save_path) {
990+
return Ok(Some(self.popup_paragraph_notification(
991+
e.to_string(),
992+
"Error opening editor".to_string(),
993+
IndexSet::new(),
994+
)));
995+
};
966996
}
967997
(question, tt) => {
968998
self.solution_file_popup_action(question, tt, index)?;

src/errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ pub enum LcAppError {
5959
#[error("Key Combination already exists")]
6060
KeyCombiExist(String),
6161

62+
#[error("Editor open error: {0}")]
63+
EditorOpen(String),
64+
6265
#[error("unknown lc app error")]
6366
Unknown,
6467
}

0 commit comments

Comments
 (0)