rename to --editor

This commit is contained in:
pacexy 2025-01-29 18:53:33 +08:00
parent d55d2adef7
commit 6c99c00a51
4 changed files with 16 additions and 16 deletions

View File

@ -82,10 +82,10 @@ impl Exercise {
} }
/// Open the exercise file in the specified editor /// Open the exercise file in the specified editor
pub fn open_in_editor(&self, editor_cmd: &str) -> io::Result<bool> { pub fn open_in_editor(&self, editor: &str) -> io::Result<bool> {
dbg!(editor_cmd); dbg!(editor);
dbg!(self.path); dbg!(self.path);
let status = Command::new(editor_cmd).arg(self.path).status()?; let status = Command::new(editor).arg(self.path).status()?;
Ok(status.success()) Ok(status.success())
} }
} }

View File

@ -37,7 +37,7 @@ struct Args {
manual_run: bool, manual_run: bool,
/// Command to open exercise files in an editor (e.g. "code" for VS Code) /// Command to open exercise files in an editor (e.g. "code" for VS Code)
#[arg(long)] #[arg(long)]
edit_cmd: Option<String>, editor: Option<String>,
} }
#[derive(Subcommand)] #[derive(Subcommand)]
@ -141,7 +141,7 @@ fn main() -> Result<ExitCode> {
watch::watch( watch::watch(
&mut app_state, &mut app_state,
notify_exercise_names, notify_exercise_names,
args.edit_cmd.as_deref(), args.editor.as_deref(),
)?; )?;
} }
Some(Subcommands::Run { name }) => { Some(Subcommands::Run { name }) => {

View File

@ -62,7 +62,7 @@ enum WatchExit {
fn run_watch( fn run_watch(
app_state: &mut AppState, app_state: &mut AppState,
notify_exercise_names: Option<&'static [&'static [u8]]>, notify_exercise_names: Option<&'static [&'static [u8]]>,
edit_cmd: Option<&str>, editor: Option<&str>,
) -> Result<WatchExit> { ) -> Result<WatchExit> {
let (watch_event_sender, watch_event_receiver) = channel(); let (watch_event_sender, watch_event_receiver) = channel();
@ -115,7 +115,7 @@ fn run_watch(
}, },
WatchEvent::Input(InputEvent::Reset) => watch_state.reset_exercise(&mut stdout)?, WatchEvent::Input(InputEvent::Reset) => watch_state.reset_exercise(&mut stdout)?,
WatchEvent::Input(InputEvent::Edit) => { WatchEvent::Input(InputEvent::Edit) => {
watch_state.edit_exercise(&mut stdout, edit_cmd)? watch_state.edit_exercise(&mut stdout, editor)?
} }
WatchEvent::Input(InputEvent::Quit) => { WatchEvent::Input(InputEvent::Quit) => {
stdout.write_all(QUIT_MSG)?; stdout.write_all(QUIT_MSG)?;
@ -140,10 +140,10 @@ fn run_watch(
fn watch_list_loop( fn watch_list_loop(
app_state: &mut AppState, app_state: &mut AppState,
notify_exercise_names: Option<&'static [&'static [u8]]>, notify_exercise_names: Option<&'static [&'static [u8]]>,
edit_cmd: Option<&str>, editor: Option<&str>,
) -> Result<()> { ) -> Result<()> {
loop { loop {
match run_watch(app_state, notify_exercise_names, edit_cmd)? { match run_watch(app_state, notify_exercise_names, editor)? {
WatchExit::Shutdown => break Ok(()), WatchExit::Shutdown => break Ok(()),
// It is much easier to exit the watch mode, launch the list mode and then restart // It is much easier to exit the watch mode, launch the list mode and then restart
// the watch mode instead of trying to pause the watch threads and correct the // the watch mode instead of trying to pause the watch threads and correct the
@ -157,7 +157,7 @@ fn watch_list_loop(
pub fn watch( pub fn watch(
app_state: &mut AppState, app_state: &mut AppState,
notify_exercise_names: Option<&'static [&'static [u8]]>, notify_exercise_names: Option<&'static [&'static [u8]]>,
edit_cmd: Option<&str>, editor: Option<&str>,
) -> Result<()> { ) -> Result<()> {
#[cfg(not(windows))] #[cfg(not(windows))]
{ {
@ -169,7 +169,7 @@ pub fn watch(
rustix::termios::LocalModes::ICANON | rustix::termios::LocalModes::ECHO; rustix::termios::LocalModes::ICANON | rustix::termios::LocalModes::ECHO;
rustix::termios::tcsetattr(stdin_fd, rustix::termios::OptionalActions::Now, &termios)?; rustix::termios::tcsetattr(stdin_fd, rustix::termios::OptionalActions::Now, &termios)?;
let res = watch_list_loop(app_state, notify_exercise_names, edit_cmd); let res = watch_list_loop(app_state, notify_exercise_names, editor);
termios.local_modes = original_local_modes; termios.local_modes = original_local_modes;
rustix::termios::tcsetattr(stdin_fd, rustix::termios::OptionalActions::Now, &termios)?; rustix::termios::tcsetattr(stdin_fd, rustix::termios::OptionalActions::Now, &termios)?;
@ -178,7 +178,7 @@ pub fn watch(
} }
#[cfg(windows)] #[cfg(windows)]
watch_list_loop(app_state, notify_exercise_names, edit_cmd) watch_list_loop(app_state, notify_exercise_names, editor)
} }
const QUIT_MSG: &[u8] = b" const QUIT_MSG: &[u8] = b"

View File

@ -272,16 +272,16 @@ impl<'a> WatchState<'a> {
pub fn edit_exercise( pub fn edit_exercise(
&mut self, &mut self,
stdout: &mut StdoutLock, stdout: &mut StdoutLock,
editor_cmd: Option<&str>, editor: Option<&str>,
) -> io::Result<()> { ) -> io::Result<()> {
if let Some(editor_cmd) = editor_cmd { if let Some(editor) = editor {
if let Err(e) = self.app_state.current_exercise().open_in_editor(editor_cmd) { if let Err(e) = self.app_state.current_exercise().open_in_editor(editor) {
writeln!(stdout, "Failed to open editor: {}", e)?; writeln!(stdout, "Failed to open editor: {}", e)?;
} }
} else { } else {
writeln!( writeln!(
stdout, stdout,
"No editor command specified. Use --edit-cmd to specify an editor." "No editor command specified. Use --editor to specify an editor."
)?; )?;
} }
Ok(()) Ok(())