Skip to content

Instantly share code, notes, and snippets.

@BartMassey
Last active June 7, 2022 04:29
Show Gist options
  • Save BartMassey/94f4dffdbad1434804a56880e372566a to your computer and use it in GitHub Desktop.
Save BartMassey/94f4dffdbad1434804a56880e372566a to your computer and use it in GitHub Desktop.
// egui hello world example, modified per
// https://www.reddit.com/r/learnrust/comments/v64f0q/egui_label_not_appearing/
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use csv::{self};
use eframe::egui;
struct MyApp {
filename: String,
headers: Vec<String>,
rows: Vec<Vec<String>>,
}
fn main() {
let filename = std::env::args().nth(1).unwrap();
let mut rdr = csv::Reader::from_path(&filename).unwrap();
let headers: Vec<String> = rdr.headers().unwrap().iter().map(str::to_owned).collect();
let rows: Vec<Vec<String>> = rdr
.records()
.map(|row| row.unwrap().iter().map(str::to_owned).collect())
.collect();
let app = MyApp {
filename,
headers,
rows,
};
let options = eframe::NativeOptions::default();
eframe::run_native("CSV Viewer", options, Box::new(|_cc| Box::new(app)));
}
// https://www.reddit.com/r/learnrust/comments/v64f0q/egui_label_not_appearing/
fn build_csv_grid(ui: &mut egui::Ui, app: &MyApp) {
egui::Grid::new(&app.filename).show(ui, |ui| {
for header in &app.headers {
ui.label(header);
}
ui.end_row();
for row in &app.rows {
for f in row {
ui.label(f);
}
ui.end_row();
}
});
}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading(&self.filename);
build_csv_grid(ui, self);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment