From 1692715085d64f2f18509d33c63b41e9acb667f6 Mon Sep 17 00:00:00 2001 From: Luxferre Date: Mon, 22 Dec 2025 11:08:13 +0200 Subject: [PATCH] added line numbering to view --- README.md | 2 +- app/ed.py | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 76f6d2b..8041327 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ A simple line-oriented text editor and page-oriented text viewer for MicroPython Exports the following functions: - `edit(filename)`: start an `ed`-like text editor (see below) -- `view(filename)`: start a `more`-like pager for viewing a text file +- `view(filename, lno=False)`: start a `more`-like pager for viewing a text file (pass an additional parameter to view it with line numbers) The editor mode supports the following subset of POSIX ed commands in the standard `[range][command][param]` syntax: diff --git a/app/ed.py b/app/ed.py index e8adba9..c01dfac 100644 --- a/app/ed.py +++ b/app/ed.py @@ -105,7 +105,7 @@ class Ed: end = min(len(self.buffer), end) for i in range(start - 1, end): if cmd_char == 'n': - print(f"{i + 1}\t{self.buffer[i]}") + print(f"{(i+1):<5} {self.buffer[i]}") else: print(self.buffer[i]) self.current_line = end @@ -201,10 +201,15 @@ def edit(fname): editor = Ed(fname) editor.run() -def view(fname): +def view(fname, lno=False): try: with open(fname, 'r') as f: text = f.read() + if lno: + buf = text.split('\n') + for i in range(0, len(buf) - 1): + buf[i] = f'{(i+1):<5} {buf[i]}' + text = '\n'.join(buf) print_paged(text) except: print('Error opening the input file!')