diff --git a/README.md b/README.md index 2544b62..8ad764c 100644 --- a/README.md +++ b/README.md @@ -217,9 +217,14 @@ 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) -- `edbuf()`: create an empty buffer, run the editor interface on it and return the result as a string (without creating any files) +- `edit(filename)`: start an `ed`-like text editor on a file (see below) +- `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) +- `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: diff --git a/app/ed.py b/app/ed.py index b389991..797c1d8 100644 --- a/app/ed.py +++ b/app/ed.py @@ -201,20 +201,35 @@ def edit(fname): editor = Ed(fname) editor.run() -def edbuf(): +def edbuf(bufstr=''): editor = Ed() + editor.buffer = bufstr.split('\n') + editor.current_line = len(editor.buffer) editor.run() 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): 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) + viewbuf(text, lno) except: 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) diff --git a/tdeckboot.py b/tdeckboot.py index 6628a95..e7a29ac 100644 --- a/tdeckboot.py +++ b/tdeckboot.py @@ -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 auth_header, url_escape, form_encode 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 try: from deck import net