spreadsheet

terminal spreadsheet application
git clone https://git.ce9e.org/spreadsheet.git

commit
8cd2d6d7aaf3a681207f79808d8770f9b10f4edf
parent
48e565a88880ee3019d500dce147547ca21eb8ca
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-08-18 12:07
add tsv support

Diffstat

M sheet/__main__.py 17 ++++++-----------
M sheet/csv.py 20 ++++++++++++--------

2 files changed, 18 insertions, 19 deletions


diff --git a/sheet/__main__.py b/sheet/__main__.py

@@ -56,8 +56,7 @@ class App(boon.App):
   56    56         super().__init__()
   57    57         self.path = path or ''
   58    58         if path:
   59    -1             with open(self.path) as fh:
   60    -1                 self.sheet = load_csv(fh)
   -1    59             self.sheet = load_csv(self.path)
   61    60         else:
   62    61             self.sheet = Sheet()
   63    62         self.x0 = 0
@@ -181,14 +180,12 @@ class App(boon.App):
  181   180         self.input = None
  182   181 
  183   182     def submit_write(self):
  184    -1         with open(self.input.value, 'w') as fh:
  185    -1             dump_csv(self.sheet, fh)
   -1   183         self.path = self.input.value
   -1   184         dump_csv(self.sheet, self.input.value)
  186   185         self.input = None
  187   186 
  188   187     def submit_write_eval(self):
  189    -1         self.path = self.input.value
  190    -1         with open(self.path, 'w') as fh:
  191    -1             dump_csv(self.sheet, fh, display=True)
   -1   188         dump_csv(self.sheet, self.input.value, display=True)
  192   189         self.input = None
  193   190 
  194   191     def submit_drag(self):
@@ -314,10 +311,8 @@ def main():
  314   311     if args.eval:
  315   312         if not args.path:
  316   313             raise ValueError('path missing')
  317    -1         with open(args.path) as fh:
  318    -1             sheet = load_csv(fh)
  319    -1         with open(args.eval, 'w') as fh:
  320    -1             dump_csv(sheet, fh, display=True)
   -1   314         sheet = load_csv(args.path)
   -1   315         dump_csv(sheet, args.eval, display=True)
  321   316     else:
  322   317         app = App(args.path)
  323   318         app.run()

diff --git a/sheet/csv.py b/sheet/csv.py

@@ -19,15 +19,17 @@ def to_display(value: float|int|str|Bar|None|Exception) -> str:
   19    19         return repr(value)
   20    20 
   21    21 
   22    -1 def load_csv(fh, **kwargs):
   -1    22 def load_csv(path, **kwargs):
   23    23     sheet = Sheet()
   24    -1     for y, row in enumerate(csv.reader(fh, **kwargs)):
   25    -1         for x, raw in enumerate(row):
   26    -1             sheet.set((x, y), raw)
   -1    24     dialect = 'excel-tab' if path.endswith('.tsv') else 'excel'
   -1    25     with open(path) as fh:
   -1    26         for y, row in enumerate(csv.reader(fh, dialect=dialect, **kwargs)):
   -1    27             for x, raw in enumerate(row):
   -1    28                 sheet.set((x, y), raw)
   27    29     return sheet
   28    30 
   29    31 
   30    -1 def dump_csv(sheet, fh, *, display=False, **kwargs):
   -1    32 def dump_csv(sheet, path, *, display=False, **kwargs):
   31    33     if display:
   32    34         def get(cell):
   33    35             return to_display(sheet.get_value(cell))
@@ -37,6 +39,8 @@ def dump_csv(sheet, fh, *, display=False, **kwargs):
   37    39     width = max((cell[0] for cell in sheet.raw), default=0) + 1
   38    40     height = max((cell[1] for cell in sheet.raw), default=0) + 1
   39    41 
   40    -1     w = csv.writer(fh, **kwargs)
   41    -1     for y in range(height):
   42    -1         w.writerow([get((x, y)) for x in range(width)])
   -1    42     dialect = 'excel-tab' if path.endswith('.tsv') else 'excel'
   -1    43     with open(path, 'w') as fh:
   -1    44         w = csv.writer(fh, dialect=dialect, **kwargs)
   -1    45         for y in range(height):
   -1    46             w.writerow([get((x, y)) for x in range(width)])