added line numbering to view
This commit is contained in:
@@ -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:
|
||||
|
||||
|
||||
@@ -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!')
|
||||
|
||||
Reference in New Issue
Block a user