spreadsheet

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

commit
61f967c8272d9e27bcf1b37e2713f7800d2baa27
parent
a976538ce74991e6307e195acb44cde280e8feb8
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-07-06 17:08
wip: drap

Diffstat

M sheet/__main__.py 28 ++++++++++++++++++++++++++++
M sheet/term.py 4 ++++

2 files changed, 32 insertions, 0 deletions


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

@@ -10,6 +10,8 @@ from .term import align_left
   10    10 from .term import align_right
   11    11 from .term import invert
   12    12 from .term import red
   -1    13 from .term import blue
   -1    14 from .sheet import iter_range
   13    15 
   14    16 
   15    17 def to_cell(value: float|int|str|None|Exception, width: int) -> str:
@@ -35,6 +37,7 @@ class App(boon.App):
   35    37         self.cursor_y = 0
   36    38         self.widths = {}
   37    39         self.input = None
   -1    40         self.drag = None
   38    41 
   39    42     @property
   40    43     def cursor(self):
@@ -88,6 +91,14 @@ class App(boon.App):
   88    91                     break
   89    92                 value = self.sheet.get_value((x, y))
   90    93                 cell = to_cell(value, self.get_width(x))
   -1    94                 if (
   -1    95                     self.drag
   -1    96                     and min(self.cursor_x, self.drag[0]) <= x
   -1    97                     and x <= max(self.cursor_x, self.drag[0])
   -1    98                     and min(self.cursor_y, self.drag[1]) <= y
   -1    99                     and y <= max(self.cursor_y, self.drag[1])
   -1   100                 ):
   -1   101                     cell = blue(cell)
   91   102                 if x == self.cursor_x and y == self.cursor_y:
   92   103                     cell = invert(cell)
   93   104                 lines[-1] += cell
@@ -117,6 +128,16 @@ class App(boon.App):
  117   128     def cancel_input(self):
  118   129         self.input = None
  119   130 
   -1   131     def submit_drag(self):
   -1   132         value = self.sheet.get_raw(self.drag)
   -1   133         for x, y in iter_range(self.cursor, self.drag):
   -1   134             # TODO: modify references in expression
   -1   135             self.sheet.set((x, y), value)
   -1   136         self.drag = None
   -1   137 
   -1   138     def cancel_drag(self):
   -1   139         self.drag = None
   -1   140 
  120   141     def on_key(self, key):
  121   142         if self.input:
  122   143             if not self.input.full and key in [
@@ -145,6 +166,11 @@ class App(boon.App):
  145   166             self.cursor_x += 1
  146   167         elif key == boon.KEY_LEFT:
  147   168             self.cursor_x = max(self.cursor_x - 1, 0)
   -1   169         elif self.drag is not None:
   -1   170             if key == '\n':
   -1   171                 self.submit_drag()
   -1   172             elif key == boon.KEY_ESC:
   -1   173                 self.cancel_drag()
  148   174         elif key == '>':
  149   175             self.change_width(self.cursor_x, 1)
  150   176         elif key == '<':
@@ -156,6 +182,8 @@ class App(boon.App):
  156   182             self.input = Input(key, self.submit_input, self.cancel_input, full=False)
  157   183         elif key == boon.KEY_DEL:
  158   184             self.sheet.set(self.cursor, '')
   -1   185         elif key == '#':
   -1   186             self.drag = self.cursor
  159   187 
  160   188 
  161   189 app = App()

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

@@ -21,5 +21,9 @@ def red(s):
   21    21     return f'\033[31m{s}\033[0m'
   22    22 
   23    23 
   -1    24 def blue(s):
   -1    25     return f'\033[34m{s}\033[0m'
   -1    26 
   -1    27 
   24    28 def invert(s):
   25    29     return f'\033[7m{s}\033[0m'