Skip to content
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

helix-term/commands: implement cquit #1096

Merged
merged 1 commit into from
Nov 15, 2021
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
4 changes: 2 additions & 2 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ impl Application {
Ok(())
}

pub async fn run(&mut self) -> Result<(), Error> {
pub async fn run(&mut self) -> Result<i32, Error> {
self.claim_term().await?;

// Exit the alternate screen and disable raw mode before panicking
Expand All @@ -622,6 +622,6 @@ impl Application {

self.restore_term()?;

Ok(())
Ok(self.editor.exit_code)
}
}
26 changes: 26 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,25 @@ mod cmd {
quit_all_impl(&mut cx.editor, args, event, true)
}

fn cquit(
cx: &mut compositor::Context,
args: &[&str],
_event: PromptEvent,
) -> anyhow::Result<()> {
let exit_code = args
.first()
.and_then(|code| code.parse::<i32>().ok())
.unwrap_or(1);
cx.editor.exit_code = exit_code;

let views: Vec<_> = cx.editor.tree.views().map(|(view, _)| view.id).collect();
for view_id in views {
cx.editor.close(view_id, false);
}

Ok(())
}

fn theme(
cx: &mut compositor::Context,
args: &[&str],
Expand Down Expand Up @@ -2374,6 +2393,13 @@ mod cmd {
fun: force_quit_all,
completer: None,
},
TypableCommand {
name: "cquit",
aliases: &["cq"],
doc: "Quit with exit code (default 1). Accepts an optional integer exit code (:cq 2).",
fun: cquit,
completer: None,
},
TypableCommand {
name: "theme",
aliases: &[],
Expand Down
12 changes: 9 additions & 3 deletions helix-term/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> {
Ok(())
}

fn main() -> Result<()> {
let exit_code = main_impl()?;
std::process::exit(exit_code);
}

#[tokio::main]
async fn main() -> Result<()> {
async fn main_impl() -> Result<i32> {
let cache_dir = helix_core::cache_dir();
if !cache_dir.exists() {
std::fs::create_dir_all(&cache_dir).ok();
Expand Down Expand Up @@ -109,7 +114,8 @@ FLAGS:

// TODO: use the thread local executor to spawn the application task separately from the work pool
let mut app = Application::new(args, config).context("unable to create new application")?;
app.run().await.unwrap();

Ok(())
let exit_code = app.run().await?;

Ok(exit_code)
}
3 changes: 3 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ pub struct Editor {

pub idle_timer: Pin<Box<Sleep>>,
pub last_motion: Option<Motion>,

pub exit_code: i32,
}

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -167,6 +169,7 @@ impl Editor {
idle_timer: Box::pin(sleep(config.idle_timeout)),
last_motion: None,
config,
exit_code: 0,
}
}

Expand Down