home · contact · privacy
Reasonably auto-indent amount and currency. master
authorChristian Heller <c.heller@plomlompom.de>
Mon, 2 Feb 2026 05:08:14 +0000 (06:08 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 2 Feb 2026 05:08:14 +0000 (06:08 +0100)
src/ledgplom/http.py
src/ledgplom/ledger.py

index 480ff511b71f6713053b6f1c6dafb58664a848b3..2198a944a17bf2545b59dbef917f9daab5594e94 100644 (file)
@@ -68,18 +68,47 @@ class _Handler(PlomHttpHandler):
                 if lineno not in lineno_to_inputs:
                     lineno_to_inputs[lineno] = []
                 lineno_to_inputs[lineno] += [toks[2]]
                 if lineno not in lineno_to_inputs:
                     lineno_to_inputs[lineno] = []
                 lineno_to_inputs[lineno] += [toks[2]]
+            booking_comments: list[str] = []
+            booking_lines_items: list[tuple[int, int, int, str, str, str]] = []
+            idx_amt_dot_max = 0
+            len_from_dot_max = 0
             for lineno, input_names in lineno_to_inputs.items():
                 inputs = {key: self.postvars.first(f'line_{lineno}_{key}')
                           for key in input_names}
                 if 0 == lineno:
             for lineno, input_names in lineno_to_inputs.items():
                 inputs = {key: self.postvars.first(f'line_{lineno}_{key}')
                           for key in input_names}
                 if 0 == lineno:
-                    line_raw = inputs['date'] + SPACE + inputs['target']
+                    intro_line_items = inputs['date'], inputs['target']
                 else:
                 else:
-                    line_raw = (
-                            SPACE * int(inputs['len_indent'])
-                            + inputs['account'] + SPACE * 2
-                            + inputs['amount'] + SPACE + inputs['currency'])
-                if (comment := inputs['comment']):
-                    line_raw += f'{SPACE}{SEP_COMMENTS}{SPACE}{comment}'
+                    len_indent = int(inputs['len_indent'])
+                    idx_amt_dot = (len_indent
+                                   + len(inputs['account'])
+                                   + len(inputs['amount'].split('.')[0])
+                                   + 1)
+                    len_from_dot = ((len(inputs['amount'].split('.')[1]) + 1)
+                                    if '.' in inputs['amount'] else 0)
+                    idx_amt_dot_max = max(idx_amt_dot_max, idx_amt_dot)
+                    len_from_dot_max = max(len_from_dot_max, len_from_dot)
+                    booking_lines_items += [(
+                        len_indent,
+                        idx_amt_dot,
+                        len_from_dot,
+                        inputs['account'],
+                        inputs['amount'],
+                        inputs['currency'])]
+                booking_comments += [inputs['comment']]
+            for idx, comment in enumerate(booking_comments):
+                if 0 == idx:
+                    line_raw = SPACE.join(intro_line_items)
+                else:
+                    line_items = booking_lines_items[idx - 1]
+                    line_raw = SPACE * line_items[0]  # indent
+                    line_raw += line_items[3]  # account
+                    line_raw += SPACE * (idx_amt_dot_max - line_items[1])
+                    line_raw += 2*SPACE + line_items[4]  # amount
+                    line_raw += SPACE * (len_from_dot_max - line_items[2])
+                    line_raw += SPACE + line_items[5]  # currency
+                    line_raw = line_raw.rstrip()
+                if comment:
+                    line_raw = SPACE.join((line_raw, SEP_COMMENTS, comment))
                 new_lines += [line_raw]
         new_lines += self.postvars.first('raw_lines').splitlines()
         new_id = self.server.ledger.rewrite_block(old_id, new_lines)
                 new_lines += [line_raw]
         new_lines += self.postvars.first('raw_lines').splitlines()
         new_id = self.server.ledger.rewrite_block(old_id, new_lines)
index c9bc62edfca93c4be850a3ffef0a3712f14eb756..2a1ee5915e1efeac1ec23d3409e333c72b74750b 100644 (file)
@@ -531,9 +531,9 @@ class _DatBlock(_LinesBlock):
         copy = self.__class__()
         copy.add(tuple(_DatLine(line.raw) for line in self.gap_lines))
         if self.booking:
         copy = self.__class__()
         copy.add(tuple(_DatLine(line.raw) for line in self.gap_lines))
         if self.booking:
-            copy.add((_DatLine(' '.join((dt_date.today().isoformat(),
-                                         self.booking.intro_line.target,
-                                         self.booking.intro_line.comment))),
+            copy.add((_DatLine(SPACE.join((dt_date.today().isoformat(),
+                                          self.booking.intro_line.target,
+                                          self.booking.intro_line.comment))),
                       ),
                      at_end=False)
             copy.add(tuple(_DatLine(line.raw)
                       ),
                      at_end=False)
             copy.add(tuple(_DatLine(line.raw)