Sounds like a normal progression. It should be super handy when you want to visually compare two open sheets by quickly switching between them.
maybe something like
def resize_cols(cols, rows, cursorCol):
'Change width of all columns to typed width, defaults to width of current column.'
w=int(vd.input("set width= ", value=cursorCol.width))
for c in cols:
c.setWidth(w)
and then
`Sheet.addCommand('gz_', 'resize-cols', 'resize_cols(visibleCols, visibleRows, cursorCol)'),
```
This totally works, @sfranky!
Here is the only bit of style feedback that I have.
We limit the usage of the name cursorCol to the execstr. Within the execstr, it will get passed into the function as a parameter. Then, within the public function itself, col is the preferred local/parameter name for a given single Column.
def resize_cols(cols, col):
'Change width of all columns to typed width, defaults to width of a given column.'
w=int(vd.input("set width= ", value=col.width))
for c in cols:
c.setWidth(w)
Now, this function is a bit more general, and is location independent. We can send it any column, and it does not need to know about the cursor or the current sheet.
I am maybe going refactor this so that resize-cols-max (g_) also uses this function.... (not sure if it will make things uglier or neater quite yet), and add it to core with your author credit!
This is the final design I came up with!
Sheet.addComand('gz_', 'resize-cols-input', resize_cols(visibleCols, int(input("set width= ", value=cursorCol.width)))')
def resize_cols(cols, width):
'resize cols to given width'
for c in cols:
c.setWidth(width)
And then resize-col can also be refactored to use this. Regrettably, resize-cols-max has a bit more nuance that was too messy to incorporate. =)
Thanks very much @anjakefala for taking the time to explain the line of thought, it is very clear !
I also appreciated seeing the progression to the final version.
BTW this is what Fanout is for. Fanout(visibleCols).width = input(...)
@saulpw Ohhhh! You are totally right!
@sfranky check this out:
class Fanout(list):
'Fan out attribute changes to every element in a list.'
def __getattr__(self, k):
return Fanout([getattr(o, k) for o in self])
def __setattr__(self, k, v):
vd.addUndo(undoAttrFunc(self, k))
for o in self:
setattr(o, k, v)
def __call__(self, *args, **kwargs):
return Fanout([o(*args, **kwargs) for o in self])
This spell fans out attributes to every element in a list. So we can pass it a list of columns, and it will set the attribute to every column in that list.
Additionally, you might notice that it will also handle the undo for the command in question, so we would not even need to think about that. :blush: I keep forgetting about this little ditty, but it is fun.
It is in develop. =D Many thanks. ^^
Thanks very much @anjakefala !
Most helpful comment
This is the final design I came up with!
And then
resize-colcan also be refactored to use this. Regrettably,resize-cols-maxhas a bit more nuance that was too messy to incorporate. =)