-
Notifications
You must be signed in to change notification settings - Fork 65
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
Is it possible to code the header at the end? #28
Comments
Hi! // in c++ api
std::string some_function()
{
fort::char_table tbl;
// populate tbl with data
// add header if some data were really added
if (!tbl.is_empty()) {
tbl.set_cur_cell(0, 0);
tbl << fort::header << "hdr1" << "hdr2" << "hdr3";
}
return tbl.to_string();
} So as a generalization: you want somehow change (or be able to change) current strategy "overwrite old content with the new one" with the new strategy "insert new content, old content should be preserved and should be below" for content adding functions. Do I understand your problem correctly? |
yes the snippet you posted is what I want to do.. For me as an user, the header line should always be the at the top of the table; irrespective of the time when it is added/coded.
please note that to add the header we need to do insert a special attribute to the able i.e. So the table knows which is the header row. So why must we add the header the very beginning? Why can we just add it whenever we want to? |
In my view all these code samples should produce the same output, but they don't:
|
btw what does |
I got your idea. |
well, spreadsheet applications for example treat headers as special entities, and allow to customize the header in many ways, for example put it in every page in a printout. :) |
Yes, I understand what you mean but as I said this approach with treating headers and ordinary rows differently is more complicated and also I believe current approach is more flexible (e.g. you can have several headers in one table easily: int main()
{
fort::char_table table;
table << fort::header
<< "N" << "Driver" << "Time" << "Avg Speed" << fort::endr
<< "1" << "Ricciardo" << "1:25.945" << "47.362" << fort::endr
<< "2" << "Hamilton" << "1:26.373" << "35.02" << fort::endr
<< "3" << "Verstappen" << "1:26.469" << "29.78" << fort::endr;
table << fort::header
<< "Some field" << "" << "" << "Avg Speed" << fort::endr
<< "fld1" << "" << "" << "47.362" << fort::endr
<< "fld2" << "" << "" << "29.78" << fort::endr;
table.cell(4, 0).set_cell_span(3);
table.cell(5, 0).set_cell_span(3);
table.cell(6, 0).set_cell_span(3);
std::cout << table.to_string() << std::endl;
} output:
All that logic with some special behavior can be added externally in some kind of a wrapper(maybe I am wrong) or user functions. So I prefer to keep things simple and implement the most basic functionality. Returning to you initial question. I can suggest adding new property to the tables (lets call it CONTENT_ADDING_STRATEGY (with 2 possible values REPLACE(default one) and INSERT). I think this approach is rather flexible. Also it will fit well for some other scenarios. In this case your code will look like this. // in c++ api
std::string some_function()
{
fort::char_table tbl;
// populate tbl with data
...
// add header at the top if some data were really added
if (!tbl.is_empty()) {
// you explicitly add header in place of the 0th row,
// all other rows will be shifted
tbl.set_adding_strategy(fort::strategy::INSERT);
tbl.set_cur_cell(0, 0);
tbl << fort::header << "hdr1" << "hdr2" << "hdr3";
}
return tbl.to_string();
} In case you don't like to set this property ( fort::default_props().set_adding_strategy(fort::strategy::INSERT); Does it suit you? Or maybe you don't like it? |
Hi. I implemented int main()
{
fort::char_table table;
table.set_adding_strategy(fort::add_strategy::insert);
// Fill table with data
table << "1" << "2" << fort::endr;
table << "3" << "4" << fort::endr;
// explicitly add header as the top row
table.set_cur_cell(0, 0);
table << fort::header
<< "hdr1" << "hdr2" << fort::endr;
std::cout << table.to_string() << std::endl;
} Output:
The syntax is rather verbose but it is simple and straightforward. You have to explicitly specify If this solution doesn't solve your problems fill free to write here your suggestions or problems that you encounter. |
thanks, so now we can code the header either at the start or at the very end. I tried the following
and this was the output
adding more data after adding the header gives counter-intuitive results. |
Pressed the wrong button and the issue got closed... |
That's expected behavior. However, maybe what you want can be implemented with a little bit different API. Something like that: int main()
{
fort::char_table table;
table.set_adding_strategy(fort::add_strategy::insert);
// Fill table with data
table << "1" << "2" << fort::endr;
// Note that I explicitly specify 0th row (table[0]) where I will insert the header
table[0] << fort::header << "hdr1" << "hdr2" << fort::endr;
table << "3" << "4" << fort::endr;
std::cout << table.to_string() << std::endl;
} So the idea of this API is that we can apply modifiing operations not on the table but on a particular row and that in this case this operations won't affect position of the current cell. I need to think about this possibility thoroughly and also how it will look with current modifying operation on distinct cells (like |
Wouldn't a header at the end typically be called a footer? :) Since it is just decorative, perhaps renaming it as a cell/row border bolding or callout property? Something like: tables[123] << fort::emphasize << "Total:" << "123" << fort::endr; |
appending the table at the end will be a footer, but pre-pending the table at the end will still be a header! :) |
I am dynamically populating a table in a function which returns ta string by calling
to_string()
in the end. My logic is such that I may end up in not adding any data at all to the table. In such cases I don't want to add any header to the table, so thatto_string()
returns an empty string.Hence a feature to add the header at the end after knowing the number of rows (or whenever we wish) will be helpful. Currently I use workarounds to return an empty string from my function, as adding the header afterwards adds it after all the data added till then.
The text was updated successfully, but these errors were encountered: