简体   繁体   中英

Python module for editing text in CLI

Is there some python module or commands that would allow me to make my python program enter a CLI text editor, populate the editor with some text, and when it exits, get out the text into some variable?

At the moment I have users enter stuff in using raw_input(), but I would like something a bit more powerful than that, and have it displayed on the CLI.

Well, you can launch the user's $EDITOR with subprocess, editing a temporary file:

import tempfile
import subprocess
import os

t = tempfile.NamedTemporaryFile(delete=False)
try:
    editor = os.environ['EDITOR']
except KeyError:
    editor = 'nano'
subprocess.call([editor, t.name])

You could have a look at urwid , a curses-based, full-fledged UI toolkit for python. It allows you to define very sophisticated interfaces and it includes different edit box types for different types of text.

Here's the function I wrote for another project. It allow user to edit a line of text complete with support for line wraps and a cursor | you can move back and forth with the arrow keys. It relies on the readchar module: pip3 install readchar to read keycodes so it should work in windows, but I have only tested it on linux terminal and as part of the initramfs.

GitHub: https://github.com/SurpriseDog/KeyLocker/blob/main/text_editor.py

(More likely to be up to date)

#!/usr/bin/python3

import sys
import shutil
from readchar import readkey


def text_editor(init='', prompt=''):
    '''
    Allow user to edit a line of text complete with support for line wraps
    and a cursor | you can move back and forth with the arrow keys.
    init    = initial text supplied to edit
    prompt  = Decoration presented before the text (not editable and not returned)
    '''

    term_width = shutil.get_terminal_size()[0]
    ptr = len(init)
    text = list(init)
    prompt = list(prompt)

    c = 0
    while True:
        if ptr and ptr > len(text):
            ptr = len(text)

        copy = prompt + text.copy()
        if ptr < len(text):
            copy.insert(ptr + len(prompt), '|')

        # Line wraps support:
        if len(copy) > term_width:
            cut = len(copy) + 3 - term_width
            if ptr > len(copy) / 2:
                copy = ['<'] * 3 + copy[cut:]
            else:
                copy = copy[:-cut] + ['>'] * 3


        # Display current line
        print('\r' * term_width + ''.join(copy), end=' ' * (term_width - len(copy)))


        # Read new character into c
        if c in (53, 54):
            # Page up/down bug
            c = readkey()
            if c == '~':
                continue
        else:
            c = readkey()

        if len(c) > 1:
            # Control Character
            c = ord(c[-1])
            if c == 68:     # Left
                ptr -= 1
            elif c == 67:   # Right
                ptr += 1
            elif c == 53:   # PgDn
                ptr -= term_width // 2
            elif c == 54:   # PgUp
                ptr += term_width // 2
            elif c == 70:   # End
                ptr = len(text)
            elif c == 72:   # Home
                ptr = 0
            else:
                print("\nUnknown control character:", c)
                print("Press ctrl-c to quit.")
                continue
            if ptr < 0:
                ptr = 0
            if ptr > len(text):
                ptr = len(text)

        else:
            num = ord(c)
            if num in (13, 10):  # Enter
                print()
                return ''.join(text)
            elif num == 127:     # Backspace
                if text:
                    text.pop(ptr - 1)
                    ptr -= 1
            elif num == 3:       # Ctrl-C
                sys.exit(1)
            else:
                # Insert normal character into text.
                text.insert(ptr, c)
                ptr += 1

if __name__ == "__main__":
    print("Result =", text_editor('Edit this text', prompt="Prompt: "))

如果您不需要 Windows 支持,您可以使用readline 模块在 shell 提示符下进行基本的命令行编辑。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM