termtable icon indicating copy to clipboard operation
termtable copied to clipboard

Dynamic row updates

Open jsimnz opened this issue 11 years ago • 13 comments

It would be nice to have the ability to continously add rows to a table after Render() is called, and have it still print formatted like the rest of the rows in the table, and without printing the header again.

This could possibly done via a cursor the keep tracks of the last printed row, and a new method, possibly RenderCursor(). Which keeps printing from the previous cursor point

Here's an example of how it might look:

t := termtable.NewTable(nil, nil)
t.SetHeader([]string{"LOWERCASE", "UPPERCASE", "NUMBERS"})
t.AddRow([]string{"abc", "ABCD", "12345"})
fmt.Println(t.Render())    //  Prints the table as expected
t.AddRow([]string{"def", "DEFG", "67890"})
fmt.Println(t.RenderCursor())    // Appends the new row to the table, formatted appropriately

jsimnz avatar Feb 14 '14 01:02 jsimnz

One problem however I see after reviewing the code, is that when adding a row, recalculate() is called to find the appropriate width for all the columns and what not. This is fine when the entire table is rendered, however if it would be possible to add rows after the table is initially printed. Then the new rows might not comply with the previous widths. Resulting in larger mis-formatted rows.

So I guess it would be ideal to determine whether this behavior is acceptable?

jsimnz avatar Feb 14 '14 01:02 jsimnz

Hi John-Alan,

Sorry it took me so long to answer, I actually went in holiday the day you submitted this issue. I think I would prefer it if the new row would have the same width as the previous, with the text being wrapped. What do you think?

stevedomin avatar Feb 24 '14 15:02 stevedomin

In any case thanks for getting back to me.

Yeah the text wrap sounds like a good solution. Is the RenderCursor (maybe RenderFromCursor) an appropriate method for this functionality, or should it just be a part of the Render function.

Adding it to the Render func might break backwards compatibility, hence my suggestion for a new function.

jsimnz avatar Feb 24 '14 18:02 jsimnz

No worries, that's the least I should do! Adding a new function is probably better yes. Do you want to have a shot at it?

stevedomin avatar Feb 25 '14 22:02 stevedomin

Sure, happy to contribute!

jsimnz avatar Feb 25 '14 22:02 jsimnz

@jsimnz @stevedomin Any traction on this?, I also am needing this for a use case of mine.

bscott avatar Jun 10 '14 06:06 bscott

It looks like @jsimnz has been working on some stuff. Any PR coming soon?

stevedomin avatar Jun 10 '14 19:06 stevedomin

Yupp, PR will be coming today. Would've done long time ago but the laptop I was working on died, with the work on it. So I just rewrote things last night.

On Tue, Jun 10, 2014 at 3:38 PM, Steve D. [email protected] wrote:

It looks like @jsimnz https://github.com/jsimnz has been working on some stuff. Any PR coming soon?

— Reply to this email directly or view it on GitHub https://github.com/stevedomin/termtable/issues/2#issuecomment-45661262.

jsimnz avatar Jun 10 '14 22:06 jsimnz

@jsimnz Thanks, How hard is it to implement row numbers?, I've been trying to do it but still a Baby Gopher, learning quickly tho. ( I come from the Ruby world)

I am basically looking to have my users select which row they want and I will invoke a function based on the row that they select. makes sense?

bscott avatar Jun 10 '14 22:06 bscott

@jsimnz Awesome news, looking forward to it

@bscott You mean as in updating specific row number?

stevedomin avatar Jun 10 '14 22:06 stevedomin

@stevedomin, I was going to squash all my commits on the feature branch feature/dynamic-rows so its a single commit (makes for cleaner PR). But if you're fine with the way it is now, ill go ahead with the PR

jsimnz avatar Jun 10 '14 22:06 jsimnz

@jsimnz I'm fine with it, but feel free to squash if you want to :)

stevedomin avatar Jun 10 '14 23:06 stevedomin

I mean each row should lost its row number in a index column.

On Tuesday, June 10, 2014, Steve D. [email protected] wrote:

@jsimnz https://github.com/jsimnz Awesome news, looking forward to it

@bscott https://github.com/bscott You mean as in updating specific row number?

— Reply to this email directly or view it on GitHub https://github.com/stevedomin/termtable/issues/2#issuecomment-45680459.

Brian Scott - DevOps and Automation Guy, Ruby/Rails Guy, all around Good Guy. Email: [email protected] / [email protected] Github: http://github.com/bscott http://github.com/bscott

bscott avatar Jun 10 '14 23:06 bscott