-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic table #34
Comments
I think it's currently not possible to create a table at runtime because an enum is defining the columns. I'll probably rewrite the library in the following days/weeks to make that possible. |
@gyscos, it seems like you maintain(ed) this library so do know if dynamic tables are possible? |
Hi, Nothing forces you to use an enum as column type. You could use really any type that implements the required traits. You may need to re-create the table if the columns change though, I have not tested altering the columns when data is already present. Here is a simple example: use cursive::traits::{Resizable, With};
use cursive_table_view::{TableView, TableViewItem};
#[derive(Clone)]
struct Row(Vec<String>); 2 implementations
impl TableViewItem<usize> for Row {
fn to_column(&self, column: usize) -> String {
self.0[column].clone()
}
fn cmp(&self, other: &Self, column: usize) -> std::cmp::Ordering {
self.0[column].cmp(&other.0[column])
}
}
fn main() {
let n_cols = 6;
let n_rows = 30;
let cell_width = 20;
let table = TableView::<Row, usize>::new()
.with(|table| {
for i in 0..n_cols {
table.add_column(i, format!("Column {i}"), |c| c.width(cell_width));
}
})
.default_column(0)
.with(|table| {
for j in 0..30 {
table.insert_item(Row((0..n_cols)
.map(|i| format!("Row {j}, column {i}"))
.collect()));
}
})
.fixed_size((n_cols * (cell_width + 2), n_rows + 2)); // There is 2 cells of padding around each column
let mut siv = cursive::default();
siv.add_layer(cursive::views::Dialog::around(table));
siv.run();
} I do agree that it would be a good idea to include that as example. In addition, the current |
Hey, Making this an example would be nice, so it is made more obvious that you can use just about any type (that implements the required traits). One last thing, the examples are just normal rust files, and their corresponding |
You can run examples using cargo:
|
Okay, didn't know that. |
Hello, is it possible to create a table at running time dynamically?
or create columns at runtime??
The text was updated successfully, but these errors were encountered: