EeshanBembi commented on code in PR #17867:
URL: https://github.com/apache/datafusion/pull/17867#discussion_r2399585157


##########
datafusion-cli/src/progress/display.rs:
##########
@@ -0,0 +1,262 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Progress bar display functionality
+
+use crate::progress::{ProgressInfo, ProgressStyle, ProgressUnit};
+use datafusion_common::instant::Instant;
+use std::io::{self, Write};
+use std::time::Duration;
+
+/// Displays progress information to the terminal
+pub struct ProgressDisplay {
+    style: ProgressStyle,
+    start_time: Instant,
+    last_display: Option<String>,
+}
+
+impl ProgressDisplay {
+    pub fn new(style: ProgressStyle) -> Self {
+        Self {
+            style,
+            start_time: Instant::now(),
+            last_display: None,
+        }
+    }
+
+    /// Update the progress display
+    pub fn update(&mut self, progress: &ProgressInfo, eta: Option<Duration>) {
+        let display_text =
+            match progress.percent.is_some() && self.style == 
ProgressStyle::Bar {
+                true => self.format_progress_bar(progress, eta),
+                false => self.format_spinner(progress),
+            };
+
+        // Only update if the display text has changed
+        if self.last_display.as_ref() != Some(&display_text) {
+            self.print_line(&display_text);
+            self.last_display = Some(display_text);
+        }
+    }
+
+    /// Finish the progress display and clean up
+    pub fn finish(&mut self) {
+        if self.last_display.is_some() {
+            // Clear the progress line and move to next line
+            print!("\r\x1b[K");
+            let _ = io::stdout().flush();
+            self.last_display = None;
+        }
+    }
+
+    /// Format a progress bar with percentage
+    fn format_progress_bar(
+        &self,
+        progress: &ProgressInfo,
+        eta: Option<Duration>,
+    ) -> String {
+        let percent = progress.percent.unwrap_or(0.0);
+        let bar = self.create_bar(percent);
+
+        let current_formatted = progress.unit.format_value(progress.current);
+        let total_formatted = progress
+            .total
+            .map(|t| progress.unit.format_value(t))
+            .unwrap_or_else(|| "?".to_string());
+
+        let throughput = self.calculate_throughput(progress);
+        let eta_text = eta
+            .map(format_duration)
+            .unwrap_or_else(|| "??:??".to_string());
+        let elapsed = format_duration(self.start_time.elapsed());
+
+        format!(
+            "\r{}  {:5.1}%  {} / {}  •  {}  •  ETA {} / {}",
+            bar,
+            percent,
+            current_formatted,
+            total_formatted,
+            throughput,
+            eta_text,
+            elapsed
+        )
+    }
+
+    /// Format a spinner without percentage
+    fn format_spinner(&self, progress: &ProgressInfo) -> String {
+        let spinner = self.get_spinner_char();
+        let current_formatted = progress.unit.format_value(progress.current);
+        let elapsed = format_duration(self.start_time.elapsed());
+
+        format!(
+            "\r{}  {}: {}  elapsed: {}",
+            spinner,
+            match progress.unit {
+                ProgressUnit::Bytes => "bytes",
+                ProgressUnit::Rows => "rows",
+            },
+            current_formatted,
+            elapsed
+        )
+    }
+
+    /// Create a visual progress bar
+    fn create_bar(&self, percent: f64) -> String {

Review Comment:
   @xudong963 Thanks for the progress bar crate suggestion! I did take a look 
at indicatif and found that it's pretty much the standard for progress bars in 
Rust.
   
     Ultimately decided to keep our custom implementation for a few reasons:
     - It's working really well (tested it with massive queries processing 
500M+ rows!)
     - Zero extra dependencies to worry about
     - We get exactly the database-specific formatting we want (rows/bytes/etc.)
     - The whole thing is only ~200 lines, so pretty manageable
   
   I figured the custom route made sense here but I'm open to changing it if 
that makes more sense



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to