Skip to content

Commit

Permalink
fix(turborepo): avoid starting ui on too small terminals (#8457)
Browse files Browse the repository at this point in the history
### Description

Avoids starting the UI when the terminal is too small. Also bumps
crossterm because apparently we get an incorrect terminal size on older
versions.

### Testing Instructions

Tested with `lefthook` on create-turbo
  • Loading branch information
NicholasLYang committed Jun 12, 2024
1 parent 53393f5 commit 632953e
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/turborepo-lib/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub async fn run(base: CommandBase, telemetry: CommandEventBuilder) -> Result<i3
.build(&handler, telemetry)
.await?;

let (sender, handle) = run.start_experimental_ui().unzip();
let (sender, handle) = run.start_experimental_ui()?.unzip();

let result = run.run(sender.clone()).await;

Expand Down
3 changes: 3 additions & 0 deletions crates/turborepo-lib/src/run/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use miette::Diagnostic;
use thiserror::Error;
use turborepo_repository::package_graph;
use turborepo_ui::tui;

use super::graph_visualizer;
use crate::{
Expand Down Expand Up @@ -51,4 +52,6 @@ pub enum Error {
SignalHandler(std::io::Error),
#[error(transparent)]
Daemon(#[from] daemon::DaemonError),
#[error(transparent)]
Tui(#[from] tui::Error),
}
19 changes: 14 additions & 5 deletions crates/turborepo-lib/src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,30 @@ impl Run {
self.experimental_ui
}

pub fn start_experimental_ui(&self) -> Option<(AppSender, JoinHandle<Result<(), tui::Error>>)> {
pub fn should_start_ui(&self) -> Result<bool, Error> {
Ok(self.experimental_ui
&& self.opts.run_opts.dry_run.is_none()
&& tui::terminal_big_enough()?)
}

#[allow(clippy::type_complexity)]
pub fn start_experimental_ui(
&self,
) -> Result<Option<(AppSender, JoinHandle<Result<(), tui::Error>>)>, Error> {
// Print prelude here as this needs to happen before the UI is started
if self.should_print_prelude {
self.print_run_prelude();
}
// Don't start UI if doing a dry run
if !self.experimental_ui || self.opts.run_opts.dry_run.is_some() {
return None;

if !self.should_start_ui()? {
return Ok(None);
}

let task_names = self.engine.tasks_with_command(&self.pkg_dep_graph);
let (sender, receiver) = AppSender::new();
let handle = tokio::task::spawn_blocking(move || tui::run_app(task_names, receiver));

Some((sender, handle))
Ok(Some((sender, handle)))
}

pub async fn run(&mut self, experimental_ui_sender: Option<AppSender>) -> Result<i32, Error> {
Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-lib/src/run/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl WatchClient {

let watched_packages = run.get_relevant_packages();

let (sender, handle) = run.start_experimental_ui().unzip();
let (sender, handle) = run.start_experimental_ui()?.unzip();

let connector = DaemonConnector {
can_start_server: true,
Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ workspace = true
[dependencies]
atty = { workspace = true }
console = { workspace = true }
crossterm = "0.26.1"
crossterm = "0.27.0"
dialoguer = { workspace = true }
indicatif = { workspace = true }
lazy_static = { workspace = true }
Expand Down
9 changes: 8 additions & 1 deletion crates/turborepo-ui/src/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ impl<I: std::io::Write> App<I> {
pub fn run_app(tasks: Vec<String>, receiver: AppReceiver) -> Result<(), Error> {
let mut terminal = startup()?;
let size = terminal.size()?;

// Figure out pane width?
let task_width_hint = TaskTable::width_hint(tasks.iter().map(|s| s.as_str()));
// Want to maximize pane width
Expand Down Expand Up @@ -160,6 +159,14 @@ fn poll(input_options: InputOptions, receiver: &AppReceiver, deadline: Instant)
}
}

const MIN_HEIGHT: u16 = 10;
const MIN_WIDTH: u16 = 20;

pub fn terminal_big_enough() -> Result<bool, Error> {
let (width, height) = crossterm::terminal::size()?;
Ok(width >= MIN_WIDTH && height >= MIN_HEIGHT)
}

/// Configures terminal for rendering App
fn startup() -> io::Result<Terminal<CrosstermBackend<Stdout>>> {
crossterm::terminal::enable_raw_mode()?;
Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-ui/src/tui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod spinner;
mod table;
mod task;

pub use app::run_app;
pub use app::{run_app, terminal_big_enough};
use event::{Event, TaskResult};
pub use handle::{AppReceiver, AppSender, TuiTask};
use input::{input, InputOptions};
Expand Down

0 comments on commit 632953e

Please sign in to comment.