Skip to content

Commit

Permalink
fix bars
Browse files Browse the repository at this point in the history
  • Loading branch information
viperML committed Jul 9, 2023
1 parent c959a49 commit 77e2548
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
12 changes: 7 additions & 5 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use color_eyre::Help;
use daggy::Walker;
use futures::stream::futures_unordered;
use futures::{StreamExt, TryStreamExt};
use indicatif::{MultiProgress, ProgressBar};
use indicatif::{MultiProgress, ProgressBar, MultiProgressAlignment};
use owo_colors::OwoColorize;
use tracing::{debug, instrument, span, trace, Level};

Expand Down Expand Up @@ -71,6 +71,8 @@ impl Args {
build_tasks.insert(&root_node, BuildTask::Waiting);

let bars = MultiProgress::new();
// bars.set_alignment(MultiProgressAlignment::Top);
// bars.set_move_cursor(false);

while !build_tasks.iter().all(|(_, task)| match task {
BuildTask::Finished => true,
Expand Down Expand Up @@ -140,8 +142,7 @@ impl Args {
Unit::PackageUnit(_) => unit.build(rebuild, &_db_conn, None),
Unit::FetchUnit(_) => {
let pb = ProgressBar::hidden();
bars.add(pb);
unit.build(rebuild, &_db_conn, None)
unit.build(rebuild, &_db_conn, Some(bars.add(pb)))
}
}
.await;
Expand Down Expand Up @@ -186,12 +187,13 @@ impl Args {
*t = BuildTask::Finished;

let u = format!("{unit:?}");
use owo_colors::OwoColorize;
eprintln!(
let msg =
format!(
"{} <- {}",
unit.result().store_path().to_string_lossy().bright_blue(),
&u.bright_black()
);
bars.println(msg)?;
}

trace!(?build_tasks);
Expand Down
34 changes: 25 additions & 9 deletions src/build_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Mutex;

use async_trait::async_trait;
use futures_util::{Stream, StreamExt};
use indicatif::ProgressBar;
use indicatif::{ProgressBar, ProgressStyle};
use tracing::{debug, trace, warn};

use crate::db::DbConnection;
Expand All @@ -14,7 +14,12 @@ use crate::*;
#[async_trait]
impl Build for Fetch {
#[tracing::instrument(skip(conn), ret, err, level = "debug")]
async fn build(&self, rebuild: bool, conn: &Mutex<DbConnection>, pb: Option<ProgressBar>) -> Result<()> {
async fn build(
&self,
rebuild: bool,
conn: &Mutex<DbConnection>,
pb: Option<ProgressBar>,
) -> Result<()> {
let path = self.result.store_path();
let path = path.as_path();

Expand All @@ -36,24 +41,35 @@ impl Build for Fetch {
bail!(status);
}

let total_length = response.content_length();
let pb = pb.wrap_err("Didn't receive a progress bar!")?;

let mut out = tokio::fs::File::create(path).await?;
if let Some(total_length) = response.content_length() {
pb.set_length(total_length);
pb.set_style(ProgressStyle::with_template(
"{msg:.blue}>> {percent}% {wide_bar}",
)?);
pb.set_message(self.name.clone());
};

let out_file = tokio::fs::File::create(path).await?;

let mut out_writer = pb.wrap_async_write(out_file);
let mut stream = response.bytes_stream();
while let Some(item) = stream.next().await {
let hint = stream.size_hint();
warn!(?hint);
tokio::io::copy(&mut item?.as_ref(), &mut out).await?;
let item = item?;
tokio::io::copy(&mut item.as_ref(), &mut out_writer).await?;
}

let perms = if self.executable {
let perm = if self.executable {
debug!("Setting as executable exec bit");
Permissions::from_mode(0o555)
} else {
Permissions::from_mode(0o444)
};

out.set_permissions(perms).await?;
tokio::fs::set_permissions(path, perm).await?;

pb.finish();

conn.lock().unwrap().add(&path)?;
Ok(())
Expand Down
7 changes: 6 additions & 1 deletion src/build_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ const BUILD_SCRIPT_LOC: &str = "/build-script";
#[async_trait]
impl Build for Package {
#[tracing::instrument(skip(conn), ret, err, level = "debug")]
async fn build(&self, rebuild: bool, conn: &Mutex<DbConnection>, pb: Option<ProgressBar>) -> Result<()> {
async fn build(
&self,
rebuild: bool,
conn: &Mutex<DbConnection>,
pb: Option<ProgressBar>,
) -> Result<()> {
let path = self.result.store_path();
let path = path.as_path();
let _path_str = path.to_str().unwrap();
Expand Down
7 changes: 6 additions & 1 deletion src/schema_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ impl crate::Main for Args {
#[async_trait]
#[delegatable_trait]
pub trait Build {
async fn build(&self, rebuild: bool, conn: &Mutex<DbConnection>, pb: Option<ProgressBar>) -> Result<()>;
async fn build(
&self,
rebuild: bool,
conn: &Mutex<DbConnection>,
pb: Option<ProgressBar>,
) -> Result<()>;
}

#[derive(Educe, PartialEq, Clone, Serialize, Deserialize, JsonSchema, Hash, Delegate, Eq)]
Expand Down

0 comments on commit 77e2548

Please sign in to comment.