Skip to content
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

Open
realtica opened this issue Jan 28, 2022 · 6 comments
Open

Dynamic table #34

realtica opened this issue Jan 28, 2022 · 6 comments

Comments

@realtica
Copy link

realtica commented Jan 28, 2022

Hello, is it possible to create a table at running time dynamically?
or create columns at runtime??

@Builditluc
Copy link

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.

@Builditluc
Copy link

@gyscos, it seems like you maintain(ed) this library so do know if dynamic tables are possible?

@gyscos
Copy link
Collaborator

gyscos commented Apr 26, 2022

Hi,

Nothing forces you to use an enum as column type. You could use really any type that implements the required traits. u32 would work too.

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 Copy constraint on the key might be removed eventually.

@Builditluc
Copy link

Hey,
thanks for the quick response. Okay, didn't know you could use any type (because there aren't any examples without enums).

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 Cargo.toml files are missing. So if you want to run the example locally, you have to write the dependencies yourself. Is this by design? If not, I can, if you want, open a PR and make these quick changes (basically move each example into its rust project)

@gyscos
Copy link
Collaborator

gyscos commented Apr 27, 2022

You can run examples using cargo:

cargo run --example basic

@Builditluc
Copy link

Okay, didn't know that.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants