Skip to content

Commit

Permalink
[suiop][image] enable q or esc to quit watching (#19881)
Browse files Browse the repository at this point in the history
## Description 

Make it so the user can hit 'q' or 'esc' to exit the image watch
interface

## Test plan 



https://github.com/user-attachments/assets/578ab67e-763e-4cd8-a4a7-1ac6006a4004


---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
after-ephemera authored Oct 16, 2024
1 parent 6d87bd2 commit 8adfe73
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
7 changes: 5 additions & 2 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 crates/suiop-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ base64.workspace = true
chrono.workspace = true
clap.workspace = true
colored.workspace = true
crossterm = { workspace = true, features = ["event-stream"] }
dirs.workspace = true
docker-api.workspace = true
field_names.workspace = true
Expand Down Expand Up @@ -54,6 +55,7 @@ once_cell.workspace = true
futures.workspace = true
thiserror.workspace = true
strsim = "0.11.1"
futures-timer = "3.0.3"


[dev-dependencies]
Expand Down
57 changes: 48 additions & 9 deletions crates/suiop-cli/src/cli/ci/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ use anyhow::Result;
use chrono::{DateTime, Local, Utc};
use clap::{Parser, ValueEnum};
use colored::Colorize;
use crossterm::{
cursor::MoveTo,
event::{Event, EventStream, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode, Clear, ClearType},
ExecutableCommand,
};
use futures::StreamExt;
use futures::{select, FutureExt};
use serde::{self, Deserialize, Serialize};
use std::{fmt::Display, str::FromStr};
use std::{fmt::Display, str::FromStr, time::Duration};
use tabled::{settings::Style, Table, Tabled};
use tracing::debug;

Expand Down Expand Up @@ -370,16 +378,47 @@ async fn send_image_request(token: &str, action: &ImageAction) -> Result<()> {
let status_table = get_status_table(resp).await?.to_string();
println!("{}", status_table);
} else {
enable_raw_mode()?;
loop {
print!("{}[2J", 27 as char);
let req = generate_image_request(token, action);

let resp = req.send().await?;
let status_table = get_status_table(resp).await?.to_string();
println!("{}", status_table);
let half_sec = std::time::Duration::from_millis(500);
std::thread::sleep(half_sec);
let mut reader = EventStream::new();
let mut delay = futures_timer::Delay::new(Duration::from_secs(1)).fuse();
let mut event = reader.next().fuse();

select! {
_ = delay => {
let req = generate_image_request(token, action);

let resp = req.send().await?;
let status_table = get_status_table(resp).await?.to_string();
std::io::stdout().execute(Clear(ClearType::All))?.execute(MoveTo(0,0))?;
print!("press 'q' or 'esc' to quit");
for (i, line )in status_table.lines().enumerate() {
std::io::stdout().execute(MoveTo(0,(i + 1) as u16))?;
println!("{}", line);
}
},
maybe_event = event => {
println!("checking event");
match maybe_event {
Some(Ok(event)) => {
if event == Event::Key(KeyCode::Char('q').into()) {
std::io::stdout().execute(Clear(ClearType::All))?.execute(MoveTo(0,0))?;
println!("q pressed, quitting 🫡");
break
} else if event == Event::Key(KeyCode::Esc.into()) {
std::io::stdout().execute(Clear(ClearType::All))?.execute(MoveTo(0,0))?;
println!("esc pressed, quitting 🫡");
break;
}

}
Some(Err(e)) => println!("Error: {:?}\r", e),
None => println!("no event"),
}
}
};
}
disable_raw_mode()?;
}
}
ImageAction::Status {
Expand Down

0 comments on commit 8adfe73

Please sign in to comment.