Giter Site home page Giter Site logo

Comments (7)

zhiburt avatar zhiburt commented on June 10, 2024

Hey @mzanrosso,

Thank you for opening the issue.

As I understand you basically have a structure with a huge list of fields and you need to rename them correct?

You could use a Format modifier to achieve this. though it would be as convenient.
I could create an util function for this I ?hope?.

Didn't test it but it got to work I guess.

fn change_header(table: &mut Table, mut data: Vec<String>) -> &mut Table {
   table.with(Modify::new(Row(..1)).with(|s: &str| {
      match data.pop() {
          Some(new_s) => s,
          None => s.to_string(),
      }
   }))
}

from tabled.

zhiburt avatar zhiburt commented on June 10, 2024

The following example works.

fn main() {
    let data = [
        ["Hello World", "123123123231"],
        ["Hello World", "zxczczxcxczxczxc"],
        ["Hello World", "[[[[[[[[[[[[[[[[["],
    ];

    let mut table = Table::new(&data).with(Style::github_markdown());

    let headers = vec!["HEADER_1".to_string(), "HEADER_2".to_string()];

    let count_columns = <[&str; 2] as Tabled>::headers().len();
    for i in 0..count_columns {
        let header = headers[i].clone();
        table = table.with(Modify::new(tabled::Cell(0, i)).with(move |_: &str| header.clone()));
    }

    println!("{}", table);
}

Overall I agree we need better way.
I'll tackle it tommorow.

Good night! :)

from tabled.

mzanrosso avatar mzanrosso commented on June 10, 2024

@zhiburt - Thanks for the answer. Yeah I saw that with some trick you can achieve this result but it's not the simplest and the smartest way to do it, since I need to replicate this code a lot of times, this header change has to be one line maximum.

from tabled.

zhiburt avatar zhiburt commented on June 10, 2024

I've added a FormatFrom which you can use.

let table = Table::new(&data)
.with(Style::github_markdown())
.with(Modify::new(Row(..1)).with(FormatWithIndex(|_, _, column| column.to_string())))
.with(Modify::new(Row(1..2).not(Column(..1))).with(FormatFrom(vec!["qwe", "asd"])))
.with(Modify::new(Column(..1).not(Row(..1))).with(Format(|s| format!("{}...", s))));

But after a good sleep I kind of started to think you may better follow another approach.
Could you show actually how you compose your structure?

from tabled.

zhiburt avatar zhiburt commented on June 10, 2024

I was just wondering if a #[header(inline)] help you?

from tabled.

mzanrosso avatar mzanrosso commented on June 10, 2024

Hey @zhiburt thanks for the examples; even if I think it can be more clean and simple, this is fair enough for my use-case:

println!(
        "{}",
        Table::new(testtable)
            .with(Modify::new(Row(0..1)).with(FormatFrom(vec![
                "test1", "test2", "test3", "test4", "test5", "test6"
            ])))
            .to_string()
    );

from tabled.

zhiburt avatar zhiburt commented on June 10, 2024

Great.

even if I think it can be more clean and simple,

I've done that in such way because it can be used in a more broad list of cases.

Could you show your vision on how it would be more simple and clean?
As you showed here?

println!("{}", Table::new(testtable).with_headers(headers).to_string());

And ones again could you show your data structure? 😄
I just tend to think that #[header(inline)] is a good fit in your case.

from tabled.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.