首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将输出重定向到VTE终端的最佳方法

将输出重定向到VTE终端的最佳方法
EN

Stack Overflow用户
提问于 2014-08-11 02:23:38
回答 2查看 1.4K关注 0票数 0

将命令的输出重定向到VTE终端的最佳方法是哪一种?

我有这样的想法:

在职业教育执行中:

代码语言:javascript
运行
复制
tty > /usr/tmp/terminal_number    

然后从python程序读取文件:

代码语言:javascript
运行
复制
with open('/usr/tmp/terminal_number', 'r') as f:
    self.VTE_redirect_path=f.readline()

然后执行bash命令,例如:

代码语言:javascript
运行
复制
os.system('''echo "foo" >  {0}'''.format(self.VTE_redirect_path))

此方法的问题是需要刷新包含terminal_number的文件/dev/pts/#。而且,我也不喜欢必须创建文件来进行通信的想法。有直接的解决办法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-11 02:43:14

在VTE中使用terminal.feed("string")

馈送

使用python是建议的执行命令的方法。如果您想要使用命令,那么您应该这样做。

代码语言:javascript
运行
复制
#Uncomment the next line to get the print() function of python 3
#from __future__ import print_function
import os
import subprocess
from subprocess import Popen
command = "echo \"something\""
env = os.environ.copy()
try:
    po = Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,
        universal_newlines=True, env=env)
    po.wait()
    output, error_output = po.communicate()

    if po.returncode:
        print(error_output)
    else:
        print(output)

except OSError as e:
    print('Execution failed:', e, file=sys.stderr)

如果您想在gtk中使用gtk,那么可以这样做。

代码语言:javascript
运行
复制
#place the following in a method of a vte instance
env = os.environ.copy()
try:
    po = Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,
        universal_newlines=True, env=env)
    po.wait()
    output, error_output = po.communicate()

    if po.returncode:
        print(error_output)
    else:
        self.feed(output) #here you're printing to your terminal.

except OSError as e:
    print('Execution failed:', e, file=sys.stderr)

对于常规终端中最好的控件,您可以尝试cmd模块。这将要求您生成您自己的提示符,所以这是一个更具体的选项,以获得您想要的。

票数 0
EN

Stack Overflow用户

发布于 2014-08-12 14:46:34

@Quentin --您给我的解决方案--打印控制台输出非常糟糕(它没有缩进),所以我不得不使用我的解决方案。以下是一个明确的例子:

代码语言:javascript
运行
复制
from gi.repository import Gtk, GObject, Vte, GLib
import os, time, threading

def command_on_VTE(self,command):
    length=len(command)
    self.terminal.feed_child(command, length)


class TheWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="inherited cell renderer")
        self.set_default_size(600, 300)
        self.terminal=Vte.Terminal()
        self.terminal.fork_command_full(
                Vte.PtyFlags.DEFAULT, #default is fine
                os.environ['HOME'], #where to start the command?
                ["/bin/bash"], #where is the emulator?
                [], #it's ok to leave this list empty
                GLib.SpawnFlags.DO_NOT_REAP_CHILD,
                None, #at least None is required
                None,
                )

        #set up the interface
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        #a scroll window is required for the terminal
        scroller = Gtk.ScrolledWindow()
        scroller.set_hexpand(True)
        scroller.set_vexpand(True)
        scroller.add(self.terminal)
        box.pack_start(scroller, False, True, 2)
        self.add(box)

        #To get the command to automatically run
        #a newline(\n) character is used at the end of the
        #command string.
        command_on_VTE(self,'''tty > /tmp/terminal_number\n''') # Get the terminal ID

        # read the terminal ID
        while not os.path.exists("/tmp/terminal_number"):
            time.sleep(0.1) 
        with open('/tmp/terminal_number', 'r') as f:
            self.VTE_redirect_path=f.readline()
            os.remove('/tmp/terminal_number')

        # this cleans the vte
        os.system('''printf "\\033c" > {0}'''.format(self.VTE_redirect_path)) 


        # this calls the exemple
        threading.Thread(target=self.make_a_test).start()   

    def make_a_test(self):
        os.system('''ls -laR /home/ > {rdc}
echo "-------- The listing ended -------

Note that the input of the commands are not printed
" > {rdc}'''.format(rdc=self.VTE_redirect_path))

win = TheWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

我还没有找到一种获得终端ID的方法,而不需要传递一个临时文件。如果有某种方式将变量从VTE传递到python脚本,则可以跳过此操作。在这方面任何帮助都是很棒的!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25235092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档