简体   繁体   中英

Using subprocess in a curses application

I'm creating an application that uses curses to build a simple user interface. It also uses the subprocess module to run external text editor so a user can type some text, then close the editor and go back to the main program.

The problem is that when the editor is console-based such as Vim or Nano, curses doesn't de-initialize properly. Which means if the color mode is used ( curses.start_color() ), terminal stays colored after the program is finished.

Here's a test script that has this issue (at least for me, I use Ubuntu and gnome-terminal):

import curses
import subprocess

screen = curses.initscr()
curses.noecho()
curses.cbreak()
screen.keypad(1)
try:
    curses.curs_set(0)
except curses.error:
    pass
curses.start_color()

screen.addstr(0, 0, 'hello, world!')
screen.refresh()

while 1:
    c = screen.getch()
    if c == ord('q'):
        break
    if c == ord('r'):
        subprocess.call('vim', shell=False)
        screen.clear()
        screen.addstr(0, 0, 'hello, world!')
        screen.refresh()

curses.nocbreak()
screen.keypad(0)
curses.echo()
curses.curs_set(1)
curses.endwin()

(Press r to enter Vim, then q to exit.)

Is there a way to fix this?

Would modifying your code to:

if c == ord('q'):
    subprocess.call('reset', shell=False)
    break

be enough for you? Or is there some additional behaviour in your real script that is not in the code you pasted here, that makes this workaround unsuitable to your goal?

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