Skip to content

Commit

Permalink
[paste] universal paste with positional columns #1377
Browse files Browse the repository at this point in the history
  • Loading branch information
saulpw committed Oct 12, 2023
1 parent cdee695 commit c979567
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
4 changes: 4 additions & 0 deletions tests/golden/issue1377.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
A customer name sales rep sku category quantity unit price ext price date
680916 Mueller and Sons Loring Predovic GP-14407 Belt 19 88.49 1681.31 2015-11-17 05:58:34
680916 Mueller and Sons Loring Predovic FI-01804 Shirt 3 78.07 234.21 2016-02-13 04:04:11
530925 Purdy and Sons Teagan O'Keefe EO-54210 Shirt 19 30.21 573.99 2015-08-11 12:44:38
7 changes: 7 additions & 0 deletions tests/golden/issue1377b.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
A customer name sales rep sku category quantity unit price ext price date
680916 Mueller and Sons Loring Predovic GP-14407 Belt 19 88.49 1681.31 2015-11-17 05:58:34
680916 Mueller and Sons Loring Predovic GP-14407 Belt 19 88.49 1681.31 2015-11-17 05:58:34
680916 Mueller and Sons Loring Predovic FI-01804 Shirt 3 78.07 234.21 2016-02-13 04:04:11
530925 Purdy and Sons Teagan O'Keefe EO-54210 Shirt 19 30.21 573.99 2015-08-11 12:44:38
680916 Mueller and Sons Loring Predovic FI-01804 Shirt 3 78.07 234.21 2016-02-13 04:04:11
530925 Purdy and Sons Teagan O'Keefe EO-54210 Shirt 19 30.21 573.99 2015-08-11 12:44:38
9 changes: 9 additions & 0 deletions tests/issue1377.vdj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!vd -p
{"sheet": null, "col": null, "row": null, "longname": "open-file", "input": "sample_data/sample-sales-reps.xlsx", "keystrokes": "o", "comment": null}
{"sheet": "sample-sales-reps", "col": "", "row": "\u30adsample-salesv4", "longname": "open-row", "input": "", "keystrokes": "Enter", "comment": "open current row with sheet-specific dive"}
{"sheet": "sample-sales-reps_sample-salesv4", "col": "", "row": "0", "longname": "select-row", "input": "", "keystrokes": "s", "comment": "select current row"}
{"sheet": "sample-sales-reps_sample-salesv4", "col": "", "row": "1", "longname": "select-row", "input": "", "keystrokes": "s", "comment": "select current row"}
{"sheet": "sample-sales-reps_sample-salesv4", "col": "", "row": "2", "longname": "select-row", "input": "", "keystrokes": "s", "comment": "select current row"}
{"sheet": "sample-sales-reps_sample-salesv4", "col": "", "row": "", "longname": "copy-selected", "input": "", "keystrokes": "gy", "comment": "yank (copy) selected rows to clipboard"}
{"col": "", "row": "", "longname": "open-new", "input": "", "keystrokes": "Shift+A", "comment": "Open new empty sheet"}
{"col": "", "row": "", "longname": "paste-after", "input": "", "keystrokes": "p", "comment": "paste clipboard rows after current row"}
10 changes: 10 additions & 0 deletions tests/issue1377b.vdj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!vd -p
{"sheet": null, "col": null, "row": null, "longname": "open-file", "input": "sample_data/sample-sales-reps.xlsx", "keystrokes": "o", "comment": null}
{"sheet": "sample-sales-reps", "col": "", "row": "\u30adsample-salesv4", "longname": "open-row", "input": "", "keystrokes": "Enter", "comment": "open current row with sheet-specific dive"}
{"sheet": "sample-sales-reps_sample-salesv4", "col": "", "row": "0", "longname": "select-row", "input": "", "keystrokes": "s", "comment": "select current row"}
{"sheet": "sample-sales-reps_sample-salesv4", "col": "", "row": "1", "longname": "select-row", "input": "", "keystrokes": "s", "comment": "select current row"}
{"sheet": "sample-sales-reps_sample-salesv4", "col": "", "row": "2", "longname": "select-row", "input": "", "keystrokes": "s", "comment": "select current row"}
{"sheet": "sample-sales-reps_sample-salesv4", "col": "", "row": "", "longname": "copy-selected", "input": "", "keystrokes": "gy", "comment": "yank (copy) selected rows to clipboard"}
{"col": "", "row": "", "longname": "open-new", "input": "", "keystrokes": "Shift+A", "comment": "Open new empty sheet"}
{"col": "", "row": "", "longname": "paste-after", "input": "", "keystrokes": "p", "comment": "paste clipboard rows after current row"}
{"col": "", "row": "1", "longname": "paste-before", "input": "", "keystrokes": "Shift+P", "comment": "paste clipboard rows before current row"}
27 changes: 23 additions & 4 deletions visidata/clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import functools
import os

from visidata import VisiData, vd, asyncthread
from visidata import Sheet, Path
from visidata import VisiData, vd, asyncthread, SettableColumn
from visidata import Sheet, Path, Column

if sys.platform == 'win32':
syscopy_cmd_default = 'clip.exe'
Expand Down Expand Up @@ -129,14 +129,33 @@ def delete_row(sheet, rowidx):
sheet.setModified()
return oldrow


@Sheet.api
@asyncthread
def paste_after(sheet, rowidx):
'Paste rows from *vd.cliprows* at *rowidx*.'
if not vd.memory.cliprows: #1793
vd.warning('nothing to paste')
return
to_paste = list(deepcopy(r) for r in reversed(vd.memory.cliprows))
sheet.addRows(to_paste, index=rowidx)

for col in vd.memory.clipcols[sheet.nVisibleCols:]:
newcol = SettableColumn()
newcol.__setstate__(col.__getstate__())
sheet.addColumn(newcol)

addedRows = []

for extrow in vd.memory.cliprows:
if isinstance(extrow, Column):
newrow = copy(extrow)
else:
newrow = sheet.newRow()
for col, extcol in zip(sheet.visibleCols, vd.memory.clipcols):
col.setValue(newrow, extcol.getTypedValue(extrow))

addedRows.append(newrow)

sheet.addRows(addedRows, index=rowidx)


Sheet.addCommand('y', 'copy-row', 'copyRows([cursorRow])', 'yank (copy) current row to clipboard')
Expand Down
4 changes: 2 additions & 2 deletions visidata/modify.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ def addRows(sheet, rows, index=None, undo=True):
'Add *rows* after row at *index*.'
addedRows = {}
if index is None: index=len(sheet.rows)
for row in Progress(rows, gerund='adding'):
for i, row in enumerate(Progress(rows, gerund='adding')):
addedRows[sheet.rowid(row)] = row
sheet.addRow(row, index=index+1)
sheet.addRow(row, index=index+i+1)

if sheet.defer:
sheet.rowAdded(row)
Expand Down

0 comments on commit c979567

Please sign in to comment.