polars_view/
file_info.rs

1use crate::DataContainer;
2
3use egui::{Color32, Frame, Grid, Stroke, Ui};
4use polars::prelude::*;
5
6/// Represents file information.
7pub struct FileInfo {
8    /// Number of rows in the dataset.
9    row_count: usize,
10    /// Number of columns in the dataset.
11    col_count: usize,
12    /// Schema of the dataset. Used for both Parquet and CSV.
13    schema: SchemaRef,
14}
15
16impl FileInfo {
17    /// Creates `FileInfo` from a `DataContainer`.
18    pub fn from_container(container: &DataContainer) -> Option<Self> {
19        let row_count = container.df.height();
20        let col_count = container.df.width();
21        let schema = container.df.schema().clone();
22
23        Some(FileInfo {
24            row_count,
25            col_count,
26            schema,
27        })
28    }
29
30    /// Renders the file file_info (row count, column count) to the UI.
31    pub fn render_metadata(&self, ui: &mut Ui) {
32        Frame::default()
33            .stroke(Stroke::new(1.0, Color32::GRAY))
34            .outer_margin(2.0)
35            .inner_margin(10.0)
36            .show(ui, |ui| {
37                Grid::new("metadata_grid")
38                    .num_columns(2)
39                    .spacing([10.0, 20.0])
40                    .striped(true)
41                    .show(ui, |ui| {
42                        ui.label("Columns:");
43                        ui.label(self.col_count.to_string());
44                        ui.end_row();
45
46                        ui.label("Rows:");
47                        ui.label(self.row_count.to_string());
48                        ui.end_row();
49                    });
50            });
51    }
52
53    /// Renders the file schema information to the UI.
54    /// Each column's name is displayed as a collapsing header,
55    /// and the column's index and data type are shown within the collapsed section.
56    /// Adds copy-to-clipboard functionality on right-click of the column name.
57    pub fn render_schema(&self, ui: &mut Ui) {
58        // Add a hint to inform the user about copy functionality.
59        ui.label("Tip: Right-click a column name to copy it to the clipboard.");
60
61        for (index, (name, dtype)) in self.schema.iter().enumerate() {
62            // Create a collapsing header for each column.  The header displays the column name.
63            let header_response = ui.collapsing(name.to_string(), |ui| {
64                // Inside the collapsing section, display the column index and data type.
65                ui.label(format!("index: {index}"));
66                ui.label(format!("type: {dtype}"));
67            });
68
69            // Check if the header was clicked (specifically with the right mouse button).
70            if header_response
71                .header_response
72                .clicked_by(egui::PointerButton::Secondary)
73            {
74                // If the right mouse button was clicked, copy the column name to the clipboard.
75                ui.ctx().copy_text(name.to_string());
76            }
77        }
78    }
79}