implemented edurl and viewurl helpers

This commit is contained in:
Luxferre
2025-12-26 09:59:30 +02:00
parent a1ebcfcb4c
commit ab5c859cee
3 changed files with 30 additions and 10 deletions
+7 -2
View File
@@ -217,9 +217,14 @@ 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 on a file (see below)
- `edbuf()`: create an empty buffer, run the editor interface on it and return the result as a string (without creating any files) - `edbuf(bufstr='')`: create an empty buffer, fill it with the input string (if any), run the editor interface on it and return the result as a string (without creating any files)
- `edurl(url)`: download the URL contents via HTTP, run the editor interface on it and return the result as a string (without creating any files)
- `view(filename, lno=False)`: start a `more`-like pager for viewing a text file (pass an additional parameter to view it with line numbers) - `view(filename, lno=False)`: start a `more`-like pager for viewing a text file (pass an additional parameter to view it with line numbers)
- `viewbuf(text, lno=False)`: same as `view` but for string variables instead of files
- `viewurl(url, lno=False)`: download the URL contents via HTTP and run `viewbuf` method on the result
Note: with `edbuf` and `edurl` methods, you can always save the file locally while inside the editor interface. Use the `f` command to specify the name (see below).
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:
+22 -7
View File
@@ -201,20 +201,35 @@ def edit(fname):
editor = Ed(fname) editor = Ed(fname)
editor.run() editor.run()
def edbuf(): def edbuf(bufstr=''):
editor = Ed() editor = Ed()
editor.buffer = bufstr.split('\n')
editor.current_line = len(editor.buffer)
editor.run() editor.run()
return '\n'.join(editor.buffer) return '\n'.join(editor.buffer)
def edurl(url):
from deck.http import http_fetch
status, text = http_fetch(url)
return edbuf(text)
def viewbuf(text, lno=False):
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)
def view(fname, lno=False): 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: viewbuf(text, 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: except:
print('Error opening the input file!') print('Error opening the input file!')
def viewurl(url, lno=False):
from deck.http import http_fetch
status, text = http_fetch(url)
viewbuf(text, lno)
+1 -1
View File
@@ -12,7 +12,7 @@ from deck.hwinfo import board_id, battery_v, cpu_f, REAL_HW
from deck.http import wget, wput, http_fetch, http_set_ua from deck.http import wget, wput, http_fetch, http_set_ua
from deck.http import auth_header, url_escape, form_encode from deck.http import auth_header, url_escape, form_encode
from deck import xlat from deck import xlat
from app.ed import edit, view, edbuf from app.ed import edit, view, edbuf, viewbuf, edurl, viewurl
from app.blog import blogpost, blogpost_str, bloged from app.blog import blogpost, blogpost_str, bloged
try: try:
from deck import net from deck import net