-
Notifications
You must be signed in to change notification settings - Fork 11.2k
feat: open current exercise via --editor
#2204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ use crossterm::{ | |
| QueueableCommand, | ||
| }; | ||
| use std::io::{self, StdoutLock, Write}; | ||
| use std::process::Command; | ||
|
|
||
| use crate::{ | ||
| cmd::CmdRunner, | ||
|
|
@@ -79,6 +80,26 @@ impl Exercise { | |
|
|
||
| writer.write_str(self.path) | ||
| } | ||
|
|
||
| /// Open the exercise file in the specified editor | ||
| pub fn open_in_editor(&self, editor: &str) -> io::Result<bool> { | ||
| let parts: Vec<&str> = editor.split_whitespace().collect(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use shlex instead of |
||
| if parts.is_empty() { | ||
| return Ok(false); | ||
| } | ||
|
|
||
| let mut cmd = Command::new(parts[0]); | ||
|
|
||
| // If the editor command has arguments, add them to the command | ||
| if parts.len() > 1 { | ||
| cmd.args(&parts[1..]); | ||
| } | ||
|
|
||
| cmd.arg(self.path); | ||
|
|
||
| let status = cmd.status()?; | ||
| Ok(status.success()) | ||
| } | ||
| } | ||
|
|
||
| pub trait RunnableExercise { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -199,6 +199,7 @@ impl<'a> WatchState<'a> { | |
| show_key(b'l', b":list / ")?; | ||
| show_key(b'c', b":check all / ")?; | ||
| show_key(b'x', b":reset / ")?; | ||
| show_key(b'e', b":edit / ")?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should only be shown if |
||
| show_key(b'q', b":quit ? ")?; | ||
|
|
||
| stdout.flush() | ||
|
|
@@ -268,6 +269,24 @@ impl<'a> WatchState<'a> { | |
| Ok(()) | ||
| } | ||
|
|
||
| pub fn edit_exercise( | ||
| &mut self, | ||
| stdout: &mut StdoutLock, | ||
| editor: Option<&str>, | ||
| ) -> io::Result<()> { | ||
| if let Some(editor) = editor { | ||
| if let Err(e) = self.app_state.current_exercise().open_in_editor(editor) { | ||
| writeln!(stdout, "Failed to open editor: {}", e)?; | ||
| } | ||
| } else { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
| writeln!( | ||
| stdout, | ||
| "No editor command specified. Use --editor to specify an editor." | ||
| )?; | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub fn check_all_exercises(&mut self, stdout: &mut StdoutLock) -> Result<ExercisesProgress> { | ||
| // Ignore any input until checking all exercises is done. | ||
| let _input_pause_guard = InputPauseGuard::scoped_pause(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ pub enum InputEvent { | |
| CheckAll, | ||
| Reset, | ||
| Quit, | ||
| Edit, | ||
| } | ||
|
|
||
| pub fn terminal_event_handler( | ||
|
|
@@ -51,6 +52,7 @@ pub fn terminal_event_handler( | |
|
|
||
| continue; | ||
| } | ||
| KeyCode::Char('e') => InputEvent::Edit, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ignoring |
||
| KeyCode::Char('q') => break WatchEvent::Input(InputEvent::Quit), | ||
| _ => continue, | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to advertise using a terminal editor with
--editorbecause it would cover Rustlings and eliminate its interactive design.