Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyshew committed Nov 28, 2024
1 parent 191a5f8 commit 7fdd3c5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
69 changes: 56 additions & 13 deletions crates/turborepo-ui/src/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ pub struct App<W> {
}

impl<W> App<W> {
pub fn new(rows: u16, cols: u16, tasks: Vec<String>) -> Self {
pub fn new(
rows: u16,
cols: u16,
tasks: Vec<String>,
repo_root: &AbsoluteSystemPathBuf,
) -> Self {
debug!("tasks: {tasks:?}");
let size = SizeInfo::new(rows, cols, tasks.iter().map(|s| s.as_str()));

Expand Down Expand Up @@ -100,7 +105,9 @@ impl<W> App<W> {
tasks_by_status,
scroll: TableState::default().with_selected(selected_task_index),
selected_task_index,
has_sidebar: true,
has_sidebar: preferences::Preferences::read_preferences(repo_root)
.map(|prefs| prefs.is_task_list_visible)
.unwrap_or(false),
has_user_scrolled: has_user_interacted,
}
}
Expand Down Expand Up @@ -579,7 +586,8 @@ pub async fn run_app(
let mut terminal = startup(color_config)?;
let size = terminal.size()?;

let mut app: App<Box<dyn io::Write + Send>> = App::new(size.height, size.width, tasks);
let mut app: App<Box<dyn io::Write + Send>> =
App::new(size.height, size.width, tasks, &repo_root);
let (crossterm_tx, crossterm_rx) = mpsc::channel(1024);
input::start_crossterm_stream(crossterm_tx);

Expand Down Expand Up @@ -774,24 +782,24 @@ fn update(
app.finish_task(&task, result)?;
}
Event::Up => {
preferences::Preferences::write_preferences(
app.previous();
let _ = preferences::Preferences::write_preferences(
&Preferences {
is_task_list_visible: app.has_sidebar,
active_task: app.active_task()?.to_string(),
},
repo_root,
);
app.previous();
}
Event::Down => {
preferences::Preferences::write_preferences(
app.next();
let _ = preferences::Preferences::write_preferences(
&Preferences {
is_task_list_visible: app.has_sidebar,
active_task: app.active_task()?.to_string(),
},
repo_root,
);
app.next();
}
Event::ScrollUp => {
app.has_user_scrolled = true;
Expand All @@ -810,14 +818,14 @@ fn update(
app.interact()?;
}
Event::ToggleSidebar => {
preferences::Preferences::write_preferences(
app.has_sidebar = !app.has_sidebar;
let _ = preferences::Preferences::write_preferences(
&Preferences {
is_task_list_visible: app.has_sidebar,
active_task: app.active_task()?.to_string(),
},
repo_root,
);
app.has_sidebar = !app.has_sidebar;
}
Event::Input { bytes } => {
app.forward_input(&bytes)?;
Expand Down Expand Up @@ -900,6 +908,7 @@ mod test {
100,
100,
vec!["foo".to_string(), "bar".to_string(), "baz".to_string()],
&AbsoluteSystemPathBuf::default(),
);
assert_eq!(
app.scroll.selected(),
Expand Down Expand Up @@ -929,6 +938,7 @@ mod test {
100,
100,
vec!["a".to_string(), "b".to_string(), "c".to_string()],
&AbsoluteSystemPathBuf::default(),
);
app.next();
assert_eq!(app.scroll.selected(), Some(1), "selected b");
Expand All @@ -951,6 +961,7 @@ mod test {
100,
100,
vec!["a".to_string(), "b".to_string(), "c".to_string()],
&AbsoluteSystemPathBuf::default(),
);
app.next();
app.next();
Expand Down Expand Up @@ -1017,6 +1028,7 @@ mod test {
100,
100,
vec!["a".to_string(), "b".to_string(), "c".to_string()],
&AbsoluteSystemPathBuf::default(),
);
app.next();
app.next();
Expand Down Expand Up @@ -1051,7 +1063,12 @@ mod test {

#[test]
fn test_forward_stdin() -> Result<(), Error> {
let mut app: App<Vec<u8>> = App::new(100, 100, vec!["a".to_string(), "b".to_string()]);
let mut app: App<Vec<u8>> = App::new(
100,
100,
vec!["a".to_string(), "b".to_string()],
&AbsoluteSystemPathBuf::default(),
);
app.next();
assert_eq!(app.scroll.selected(), Some(1), "selected b");
assert_eq!(app.tasks_by_status.task_name(1)?, "b", "selected b");
Expand Down Expand Up @@ -1084,7 +1101,12 @@ mod test {

#[test]
fn test_interact() -> Result<(), Error> {
let mut app: App<Vec<u8>> = App::new(100, 100, vec!["a".to_string(), "b".to_string()]);
let mut app: App<Vec<u8>> = App::new(
100,
100,
vec!["a".to_string(), "b".to_string()],
&AbsoluteSystemPathBuf::default(),
);
assert!(!app.is_focusing_pane(), "app starts focused on table");
app.insert_stdin("a", Some(Vec::new()))?;

Expand All @@ -1106,7 +1128,12 @@ mod test {

#[test]
fn test_task_status() -> Result<(), Error> {
let mut app: App<Vec<u8>> = App::new(100, 100, vec!["a".to_string(), "b".to_string()]);
let mut app: App<Vec<u8>> = App::new(
100,
100,
vec!["a".to_string(), "b".to_string()],
&AbsoluteSystemPathBuf::default(),
);
app.next();
assert_eq!(app.scroll.selected(), Some(1), "selected b");
assert_eq!(app.tasks_by_status.task_name(1)?, "b", "selected b");
Expand All @@ -1127,6 +1154,7 @@ mod test {
100,
100,
vec!["a".to_string(), "b".to_string(), "c".to_string()],
&AbsoluteSystemPathBuf::default(),
);
assert_eq!(app.scroll.selected(), Some(0), "selected a");
assert_eq!(app.tasks_by_status.task_name(0)?, "a", "selected a");
Expand Down Expand Up @@ -1157,6 +1185,7 @@ mod test {
100,
100,
vec!["a".to_string(), "b".to_string(), "c".to_string()],
&AbsoluteSystemPathBuf::default(),
);
app.next();
assert_eq!(app.scroll.selected(), Some(1), "selected b");
Expand Down Expand Up @@ -1184,7 +1213,12 @@ mod test {

#[test]
fn test_resize() -> Result<(), Error> {
let mut app: App<Vec<u8>> = App::new(20, 24, vec!["a".to_string(), "b".to_string()]);
let mut app: App<Vec<u8>> = App::new(
20,
24,
vec!["a".to_string(), "b".to_string()],
&AbsoluteSystemPathBuf::default(),
);
let pane_rows = app.size.pane_rows();
let pane_cols = app.size.pane_cols();
for (name, task) in app.tasks.iter() {
Expand Down Expand Up @@ -1219,6 +1253,7 @@ mod test {
100,
100,
vec!["a".to_string(), "b".to_string(), "c".to_string()],
&AbsoluteSystemPathBuf::default(),
);
app.next();
app.update_tasks(Vec::new())?;
Expand All @@ -1233,6 +1268,7 @@ mod test {
100,
100,
vec!["a".to_string(), "b".to_string(), "c".to_string()],
&AbsoluteSystemPathBuf::default(),
);
app.next();
app.restart_tasks(vec!["d".to_string()])?;
Expand All @@ -1249,6 +1285,7 @@ mod test {
100,
100,
vec!["a".to_string(), "b".to_string(), "c".to_string()],
&AbsoluteSystemPathBuf::default(),
);
app.enter_search()?;
assert!(matches!(app.focus, LayoutSections::Search { .. }));
Expand All @@ -1270,6 +1307,7 @@ mod test {
100,
100,
vec!["a".to_string(), "ab".to_string(), "abc".to_string()],
&AbsoluteSystemPathBuf::default(),
);
app.enter_search()?;
app.search_enter_char('a')?;
Expand All @@ -1293,6 +1331,7 @@ mod test {
100,
100,
vec!["a".to_string(), "ab".to_string(), "abc".to_string()],
&AbsoluteSystemPathBuf::default(),
);
app.enter_search()?;
app.search_enter_char('b')?;
Expand Down Expand Up @@ -1320,6 +1359,7 @@ mod test {
100,
100,
vec!["a".to_string(), "abc".to_string(), "b".to_string()],
&AbsoluteSystemPathBuf::default(),
);
app.next();
assert_eq!(app.active_task()?, "abc");
Expand All @@ -1339,6 +1379,7 @@ mod test {
100,
100,
vec!["a".to_string(), "abc".to_string(), "b".to_string()],
&AbsoluteSystemPathBuf::default(),
);
app.next();
assert_eq!(app.active_task()?, "abc");
Expand All @@ -1358,6 +1399,7 @@ mod test {
100,
100,
vec!["a".to_string(), "ab".to_string(), "abc".to_string()],
&AbsoluteSystemPathBuf::default(),
);
app.enter_search()?;
app.search_enter_char('b')?;
Expand All @@ -1374,6 +1416,7 @@ mod test {
100,
100,
vec!["a".to_string(), "ab".to_string(), "abc".to_string()],
&AbsoluteSystemPathBuf::default(),
);
app.enter_search()?;
app.search_enter_char('b')?;
Expand Down
12 changes: 6 additions & 6 deletions crates/turborepo-ui/src/tui/preferences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ impl Preferences {
Ok(())
}

// pub fn read_preferences(
// repo_root: &AbsoluteSystemPathBuf,
// ) -> Result<Self, Box<dyn std::error::Error>> {
// let preferences_file = repo_root.join_components(&[".turbo",
// "preferences", "tui.json"]); read_from_json(preferences_file.
// as_std_path().to_str().unwrap()) }
pub fn read_preferences(
repo_root: &AbsoluteSystemPathBuf,
) -> Result<Self, Box<dyn std::error::Error>> {
let preferences_file = repo_root.join_components(&[".turbo", "preferences", "tui.json"]);
read_from_json(preferences_file.as_std_path().to_str().unwrap())
}
}

fn update_json_field(

Check warning on line 66 in crates/turborepo-ui/src/tui/preferences.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (ubuntu-latest)

function `update_json_field` is never used

Check warning on line 66 in crates/turborepo-ui/src/tui/preferences.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (macos-13)

function `update_json_field` is never used

Check warning on line 66 in crates/turborepo-ui/src/tui/preferences.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust check

function `update_json_field` is never used

Check warning on line 66 in crates/turborepo-ui/src/tui/preferences.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (windows-latest)

function `update_json_field` is never used

Check warning on line 66 in crates/turborepo-ui/src/tui/preferences.rs

View workflow job for this annotation

GitHub Actions / Rust lints

function `update_json_field` is never used

Check warning on line 66 in crates/turborepo-ui/src/tui/preferences.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on ubuntu

function `update_json_field` is never used

Check warning on line 66 in crates/turborepo-ui/src/tui/preferences.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on macos

function `update_json_field` is never used

Check warning on line 66 in crates/turborepo-ui/src/tui/preferences.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on windows

function `update_json_field` is never used
Expand Down

0 comments on commit 7fdd3c5

Please sign in to comment.