Skip to content

Commit

Permalink
Scaffold: Auto open in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Nov 27, 2023
1 parent 9666711 commit 4630d2c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
30 changes: 30 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions scaffold/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ humantime = "2.1.0"
once_cell = "1.18.0"
regex = "1.10.2"
scraper = "0.18.1"
shell-words = "1.1.0"
ureq = "2.9.1"
url = { version = "2.5.0", features = ["serde"] }
which = "5.0.0"
9 changes: 8 additions & 1 deletion scaffold/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,15 @@ pub struct InitArgs {
/// Useful if you want to use this command with a different language or organization.
#[arg(short, long)]
pub no_scaffold: bool,
/// Automatically open the solution file in your editor.
/// Only works if you are not using `--no-scaffold`.
/// Configure the editor with the `--editor` argument.
#[arg(short, long)]
pub auto_open: bool,
/// Command to open a file in your editor.
#[arg(short, long, default_value = "code {{file}}")]
pub editor: String,

// auto_open: bool,
/// The day to fetch the input for.
pub day: u8,
/// The year to fetch the input for.
Expand Down
34 changes: 27 additions & 7 deletions scaffold/src/commands/init.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{
fs::{self, File},
io::Write,
path::Path,
path::{Path, PathBuf},
process::Command,
};

use anyhow::{Context, Result};
Expand All @@ -25,16 +26,35 @@ pub fn init(session: &Session, cmd: &InitArgs, args: &Args) -> Result<()> {
write_input(cmd, input, formats)?;

if !cmd.no_scaffold {
write_scaffold(cmd, formats)?;
let path = write_scaffold(cmd, formats)?;
modify_module(cmd, formats)?;

if cmd.auto_open {
let command =
Formatter::new(&cmd.editor)?.format::<&[_]>(&[("file", path.to_string_lossy())])?;
let args = shell_words::split(&command)?;
let executable = which::which(&args[0])?;
println!("[*] Opening solution file");
Command::new(&executable)
.args(&args[1..])
.spawn()
.with_context(|| {
format!(
"Opening editor with `{}` [{:?}]",
executable.to_string_lossy(),
&args[1..]
)
})?;
}
}

Ok(())
}

fn write_scaffold(cmd: &InitArgs, formats: &[(&str, String)]) -> Result<()> {
let file_location = Formatter::new(&cmd.solution_location)?.format(formats)?;
let mut file = create_file(&Path::new(&file_location))?;
fn write_scaffold(cmd: &InitArgs, formats: &[(&str, String)]) -> Result<PathBuf> {
let location = Formatter::new(&cmd.solution_location)?.format(formats)?;
let file_location = Path::new(&location);
let mut file = create_file(&file_location)?;

println!("[*] Loading template");
let template = match cmd.solution_template {
Expand All @@ -44,8 +64,8 @@ fn write_scaffold(cmd: &InitArgs, formats: &[(&str, String)]) -> Result<()> {
let template = Formatter::new(&template)?.format(formats)?;

file.write_all(template.as_bytes())?;
println!("[*] Wrote scaffold to {file_location}");
Ok(())
println!("[*] Wrote scaffold to {location}");
Ok(file_location.to_path_buf())
}

// todo: Pass args from main func
Expand Down

0 comments on commit 4630d2c

Please sign in to comment.