spreadsheet

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

commit
ba4cc0f02e65546ad7a1cdb7956a7c33f3f51ad6
parent
0ba74526ce86ad766008adb86ab7a575c9c04810
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-07-06 06:42
continue with input

Diffstat

M sheet/__main__.py 48 +++++++++++++++++++++++++++++++-----------------
M sheet/input.py 26 ++++++++++++++------------

2 files changed, 45 insertions, 29 deletions


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

@@ -72,8 +72,7 @@ class App(boon.App):
   72    72 
   73    73         for dy in range(rows - 2):
   74    74             y = self.y0 + dy
   75    -1             # FIXME: col_head vanishes
   76    -1             col_head = align_right(str(y + 1), 4)
   -1    75             col_head = align_center(str(y + 1), 4)
   77    76             if y == self.cursor_y:
   78    77                 col_head = invert(col_head)
   79    78             lines.append(col_head)
@@ -115,34 +114,49 @@ class App(boon.App):
  115   114     def cancel_input(self):
  116   115         self.input = None
  117   116 
  118    -1     def on_key(self, key):
   -1   117     def maybe_submit_input(self):
  119   118         if self.input:
  120    -1             return self.input.on_key(key)
   -1   119             if self.input.full:
   -1   120                 return False
   -1   121             else:
   -1   122                 self.submit_input()
   -1   123         return True
  121   124 
  122    -1         if key == 'q':
  123    -1             self.running = False
  124    -1         elif key == boon.KEY_DOWN:
  125    -1             self.cursor_y += 1
   -1   125     def on_key(self, key):
   -1   126         if key == boon.KEY_DOWN:
   -1   127             if self.maybe_submit_input():
   -1   128                 self.cursor_y += 1
  126   129         elif key == boon.KEY_UP:
  127    -1             self.cursor_y = max(self.cursor_y - 1, 0)
   -1   130             if self.maybe_submit_input():
   -1   131                 self.cursor_y = max(self.cursor_y - 1, 0)
  128   132         elif key == boon.KEY_NPAGE:
  129    -1             self.cursor_y += 20  # TODO: relativ to rows
   -1   133             if self.maybe_submit_input():
   -1   134                 self.cursor_y += 20  # TODO: relativ to rows
  130   135         elif key == boon.KEY_PPAGE:
  131    -1             self.cursor_y = max(self.cursor_y - 20, 0)
   -1   136             if self.maybe_submit_input():
   -1   137                 self.cursor_y = max(self.cursor_y - 20, 0)
  132   138         elif key == boon.KEY_RIGHT:
  133    -1             self.cursor_x += 1
   -1   139             if self.maybe_submit_input():
   -1   140                 self.cursor_x += 1
  134   141         elif key == boon.KEY_LEFT:
  135    -1             self.cursor_x = max(self.cursor_x - 1, 0)
   -1   142             if self.maybe_submit_input():
   -1   143                 self.cursor_x = max(self.cursor_x - 1, 0)
   -1   144 
   -1   145         if self.input:
   -1   146             self.input.on_key(key)
   -1   147         elif key == 'q':
   -1   148             self.running = False
  136   149         elif key == '>':
  137   150             self.change_width(self.cursor_x, 1)
  138   151         elif key == '<':
  139   152             self.change_width(self.cursor_x, -1)
  140    -1         elif key == '=':
  141    -1             pass
  142    -1             # self.set_width(self.cursor_x, max()  # TODO auto width
   -1   153         # elif key == '=':
   -1   154         #     self.set_width(self.cursor_x, max()  # TODO auto width
  143   155         elif key == '\n':
  144   156             raw = self.sheet.get_raw((self.cursor_x, self.cursor_y))
  145    -1             self.input = Input(raw, self.submit_input, self.cancel_input)
   -1   157             self.input = Input(raw, self.submit_input, self.cancel_input, full=True)
   -1   158         elif key in '=0123456789':
   -1   159             self.input = Input(key, self.submit_input, self.cancel_input, full=False)
  146   160         elif key == boon.KEY_DEL:
  147   161             self.sheet.set((self.cursor_x, self.cursor_y), '')
  148   162 

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

@@ -1,36 +1,38 @@
    1    -1 import string
    2    -1 
    3     1 import boon
    4     2 
    5     3 from .term import invert
    6     4 
    7     5 
    8     6 class Input:
    9    -1     def __init__(self, value, submit, cancel):
   -1     7     def __init__(self, value, submit, cancel, *, prompt='', full=False):
   10     8         self.value = value
   11     9         self.cursor = len(value)
   12    10         self.offset = 0
   13    11         self.submit = submit
   14    12         self.cancel = cancel
   -1    13         self.prompt = prompt
   -1    14         self.full = full
   15    15 
   16    16     def scroll_into_view(self, cols):
   17    17         if self.cursor < self.offset:
   18    18             self.offset = self.cursor
   19    -1         elif self.cursor > self.offset + cols - 1:
   20    -1             self.offset = self.cursor - cols + 1
   -1    19         else:
   -1    20             space = cols - len(self.prompt) - 1
   -1    21             if self.cursor > self.offset + space:
   -1    22                 self.offset = self.cursor - space
   21    23 
   22    24     def render(self, cols):
   23    25         self.scroll_into_view(cols)
   24    26         v = self.value + ' '
   25    27         return (
   26    -1             v[self.offset:self.cursor]
   -1    28             self.prompt
   -1    29             + v[self.offset:self.cursor]
   27    30             + invert(v[self.cursor])
   28    -1             + v[self.cursor + 1:self.offset + cols]
   -1    31             + v[self.cursor + 1:self.offset + cols - len(self.prompt)]
   29    32         )
   30    33 
   31    34     def on_key(self, key):
   32    35         # TODO ctrl-left/right to jump words
   33    -1         # TODO del
   34    36         if key == boon.KEY_LEFT:
   35    37             self.cursor = max(self.cursor - 1, 0)
   36    38         elif key == boon.KEY_RIGHT:
@@ -43,13 +45,13 @@ class Input:
   43    45             if self.cursor > 0:
   44    46                 self.value = self.value[:self.cursor - 1] + self.value[self.cursor:]
   45    47                 self.cursor -= 1
   -1    48         elif key == boon.KEY_DEL:
   -1    49             if self.cursor < len(self.value):
   -1    50                 self.value = self.value[:self.cursor] + self.value[self.cursor + 1:]
   46    51         elif key == chr(27):
   47    52             self.cancel()
   48    53         elif key == '\n':
   49    54             self.submit()
   50    55         elif key.isprintable():
   51    -1             self.value += key
   -1    56             self.value = self.value[:self.cursor] + key + self.value[self.cursor:]
   52    57             self.cursor += len(key)
   53    -1         else:
   54    -1             self.value += repr(key)
   55    -1             self.cursor += len(repr(key))