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:
|
Exports the following functions:
|
||||||
|
|
||||||
- `edit(filename)`: start an `ed`-like text editor (see below)
|
- `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:
|
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)
|
end = min(len(self.buffer), end)
|
||||||
for i in range(start - 1, end):
|
for i in range(start - 1, end):
|
||||||
if cmd_char == 'n':
|
if cmd_char == 'n':
|
||||||
print(f"{i + 1}\t{self.buffer[i]}")
|
print(f"{(i+1):<5} {self.buffer[i]}")
|
||||||
else:
|
else:
|
||||||
print(self.buffer[i])
|
print(self.buffer[i])
|
||||||
self.current_line = end
|
self.current_line = end
|
||||||
@@ -201,10 +201,15 @@ def edit(fname):
|
|||||||
editor = Ed(fname)
|
editor = Ed(fname)
|
||||||
editor.run()
|
editor.run()
|
||||||
|
|
||||||
def view(fname):
|
def view(fname, lno=False):
|
||||||
try:
|
try:
|
||||||
with open(fname, 'r') as f:
|
with open(fname, 'r') as f:
|
||||||
text = f.read()
|
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)
|
print_paged(text)
|
||||||
except:
|
except:
|
||||||
print('Error opening the input file!')
|
print('Error opening the input file!')
|
||||||
|
|||||||
Reference in New Issue
Block a user