Skip to content

Commit d3e9ba6

Browse files
fix: only hide curser after clap is done parsing cli args
Clap exits the process directly, disregarding the drop impl of other structs, when invoked with --help for example. The other implementation solution would be to rely on a libc unsafe hook to execute code no matter what at process exit, which is a bit overkill for our usecase here.
1 parent 4860c11 commit d3e9ba6

2 files changed

Lines changed: 25 additions & 19 deletions

File tree

src/cli/mod.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,35 @@ use std::path::PathBuf;
1515
use crate::{
1616
api_client::CodSpeedAPIClient,
1717
config::CodSpeedConfig,
18-
local_logger::{CODSPEED_U8_COLOR_CODE, init_local_logger},
18+
local_logger::{CODSPEED_U8_COLOR_CODE, IS_TTY, init_local_logger},
1919
prelude::*,
2020
project_config::DiscoveredProjectConfig,
2121
};
2222
use clap::{
2323
Parser, Subcommand,
2424
builder::{Styles, styling},
2525
};
26+
use console::Term;
27+
28+
/// Guard that hides the terminal cursor on creation and restores it on drop.
29+
struct CursorGuard;
30+
31+
impl CursorGuard {
32+
fn new() -> Self {
33+
if *IS_TTY {
34+
let _ = Term::stderr().hide_cursor();
35+
}
36+
Self
37+
}
38+
}
39+
40+
impl Drop for CursorGuard {
41+
fn drop(&mut self) {
42+
if *IS_TTY {
43+
let _ = Term::stderr().show_cursor();
44+
}
45+
}
46+
}
2647

2748
fn create_styles() -> Styles {
2849
styling::Styles::styled()
@@ -99,6 +120,8 @@ enum Commands {
99120

100121
pub async fn run() -> Result<()> {
101122
let cli = Cli::parse();
123+
// Important: keep this after the Cli::parse() because the function can exit the process by itself, skipping the drop of the CursorGuard
124+
let _cursor_guard = CursorGuard::new();
102125
let codspeed_config =
103126
CodSpeedConfig::load_with_override(cli.config_name.as_deref(), cli.oauth_token.as_deref())?;
104127
let api_client = CodSpeedAPIClient::try_from((&cli, &codspeed_config))?;

src/main.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,9 @@
11
use codspeed_runner::{clean_logger, cli};
2-
use console::{Term, style};
2+
use console::style;
33
use log::log_enabled;
44

5-
struct HiddenCursor(Term);
6-
7-
impl HiddenCursor {
8-
fn new() -> Self {
9-
let term = Term::stderr();
10-
let _ = term.hide_cursor();
11-
Self(term)
12-
}
13-
}
14-
15-
impl Drop for HiddenCursor {
16-
fn drop(&mut self) {
17-
let _ = self.0.show_cursor();
18-
}
19-
}
20-
215
#[tokio::main(flavor = "current_thread")]
226
async fn main() {
23-
let _cursor = HiddenCursor::new();
247
let res = cli::run().await;
258
if let Err(err) = res {
269
// Show the primary error

0 commit comments

Comments
 (0)