2 # -*- coding: utf-8 -*-
6 from PyQt4
.QtCore
import QTimer
7 from PyQt4
.QtGui
import QApplication
, QTabWidget
, QPushButton
9 from pyqterm
import TerminalWidget
10 from pyqterm
.procinfo
import ProcessInfo
13 class TabbedTerminal(QTabWidget
):
15 def __init__(self
, parent
=None):
16 super(TabbedTerminal
, self
).__init
__(parent
)
17 self
.proc_info
= ProcessInfo()
18 self
.setTabPosition(QTabWidget
.South
)
19 self
._new
_button
= QPushButton(self
)
20 self
._new
_button
.setText("New")
21 self
._new
_button
.clicked
.connect(self
.new_terminal
)
22 self
.setCornerWidget(self
._new
_button
)
23 self
.setTabsClosable(True)
25 self
.setWindowTitle("Terminal")
28 self
.tabCloseRequested
[int].connect(self
._on
_close
_request
)
29 self
.currentChanged
[int].connect(self
._on
_current
_changed
)
30 QTimer
.singleShot(0, self
.new_terminal
) # create lazy on idle
33 def _on_close_request(self
, idx
):
34 term
= self
.widget(idx
)
37 def _on_current_changed(self
, idx
):
38 term
= self
.widget(idx
)
39 self
._update
_title
(term
)
41 def new_terminal(self
):
42 term
= TerminalWidget(self
)
43 term
.session_closed
.connect(self
._on
_session
_closed
)
44 self
.addTab(term
, "Terminal")
45 self
._terms
.append(term
)
46 self
.setCurrentWidget(term
)
49 def timerEvent(self
, event
):
50 self
._update
_title
(self
.currentWidget())
52 def _update_title(self
, term
):
54 self
.setWindowTitle("Terminal")
56 idx
= self
.indexOf(term
)
58 self
.proc_info
.update()
59 child_pids
= [pid
] + self
.proc_info
.all_children(pid
)
60 for pid
in reversed(child_pids
):
61 cwd
= self
.proc_info
.cwd(pid
)
65 cmd
= self
.proc_info
.commands
[pid
]
66 title
= "%s: %s" % (os
.path
.basename(cwd
), cmd
)
69 self
.setTabText(idx
, title
)
70 self
.setWindowTitle(title
)
72 def _on_session_closed(self
):
75 self
._terms
.remove(term
)
78 self
.removeTab(self
.indexOf(term
))
79 widget
= self
.currentWidget()
86 if __name__
== "__main__":
87 app
= QApplication(sys
.argv
)
88 win
= TabbedTerminal()