Skip to content

Commit

Permalink
Finished connecting to new UI. Added an event for updating the tasks …
Browse files Browse the repository at this point in the history
…in the table
  • Loading branch information
NicholasLYang committed May 10, 2024
1 parent 78937d6 commit 19a6a64
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 23 deletions.
5 changes: 1 addition & 4 deletions crates/turborepo-lib/src/commands/run.rs
Expand Up @@ -44,10 +44,7 @@ pub async fn run(base: CommandBase, telemetry: CommandEventBuilder) -> Result<i3
.build(&handler, telemetry)
.await?;

let (sender, handle) = run
.has_experimental_ui()
.then(|| run.start_experimental_ui())
.unzip();
let (sender, handle) = run.start_experimental_ui().unzip();

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

Expand Down
8 changes: 6 additions & 2 deletions crates/turborepo-lib/src/run/mod.rs
Expand Up @@ -122,12 +122,16 @@ impl Run {
self.experimental_ui
}

pub fn start_experimental_ui(&self) -> (AppSender, JoinHandle<Result<(), tui::Error>>) {
pub fn start_experimental_ui(&self) -> Option<(AppSender, JoinHandle<Result<(), tui::Error>>)> {
if !self.experimental_ui {
return 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));

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

pub async fn run(&mut self, experimental_ui_sender: Option<AppSender>) -> Result<i32, Error> {
Expand Down
24 changes: 8 additions & 16 deletions crates/turborepo-lib/src/run/watch.rs
Expand Up @@ -91,6 +91,8 @@ pub enum Error {
SignalInterrupt,
#[error("package change error")]
PackageChange(#[from] tonic::Status),
#[error("could not connect to UI thread")]
UISendError(String),

Check failure on line 95 in crates/turborepo-lib/src/run/watch.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust clippy

variant name ends with the enum's name
}

impl WatchClient {
Expand All @@ -112,10 +114,7 @@ impl WatchClient {
.build(&handler, telemetry.clone())
.await?;

let (sender, handle) = run
.has_experimental_ui()
.then(|| run.start_experimental_ui())
.unzip();
let (sender, handle) = run.start_experimental_ui().unzip();

let connector = DaemonConnector {
can_start_server: true,
Expand Down Expand Up @@ -298,18 +297,11 @@ impl WatchClient {
.build(&self.handler, self.telemetry.clone())
.await?;

if self.run.has_experimental_ui() {
if let Some(handle) = &self.ui_handle {
handle.abort();
}

let (sender, handle) = self
.run
.has_experimental_ui()
.then(|| self.run.start_experimental_ui())
.unzip();
self.ui_sender = sender;
self.ui_handle = handle;
if let Some(sender) = &self.ui_sender {
let task_names = self.run.engine.tasks_with_command(&self.run.pkg_dep_graph);
sender
.update_tasks(task_names)
.map_err(|err| Error::UISendError(err.to_string()))?;
}

if self.run.has_persistent_tasks() {
Expand Down
8 changes: 8 additions & 0 deletions crates/turborepo-ui/src/tui/app.rs
Expand Up @@ -77,6 +77,10 @@ impl<I> App<I> {
pub fn term_size(&self) -> (u16, u16) {
self.pane.term_size()
}

pub fn update_tasks(&mut self, tasks: Vec<String>) {
self.table = TaskTable::new(tasks.clone());
}
}

impl<I: std::io::Write> App<I> {
Expand Down Expand Up @@ -238,6 +242,10 @@ fn update(
Event::SetStdin { task, stdin } => {
app.pane.insert_stdin(&task, Some(stdin))?;
}
Event::UpdateTasks { tasks } => {
app.update_tasks(tasks);
app.table.tick();
}
}
Ok(None)
}
Expand Down
3 changes: 3 additions & 0 deletions crates/turborepo-ui/src/tui/event.rs
Expand Up @@ -29,6 +29,9 @@ pub enum Event {
Input {
bytes: Vec<u8>,
},
UpdateTasks {
tasks: Vec<String>,
},
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
Expand Down
5 changes: 5 additions & 0 deletions crates/turborepo-ui/src/tui/handle.rs
Expand Up @@ -56,6 +56,11 @@ impl AppSender {
// it'll be a no-op.
self.primary.send(Event::Stop).ok();
}

/// Update the list of tasks displayed in the TUI
pub fn update_tasks(&self, tasks: Vec<String>) -> Result<(), mpsc::SendError<Event>> {
self.primary.send(Event::UpdateTasks { tasks })
}
}

impl AppReceiver {
Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-ui/src/tui/input.rs
Expand Up @@ -23,7 +23,7 @@ pub fn input(interact: bool) -> Result<Option<Event>, Error> {
}
}

/// Converts a crostterm key event into a TUI interaction event
/// Converts a crossterm key event into a TUI interaction event
fn translate_key_event(interact: bool, key_event: KeyEvent) -> Option<Event> {
// On Windows events for releasing a key are produced
// We skip these to avoid emitting 2 events per key press.
Expand Down

0 comments on commit 19a6a64

Please sign in to comment.