Config dialog box. Still doesn't save / restore settings.
This commit is contained in:
parent
8db6995db9
commit
6cdd355350
137
crussh.py
137
crussh.py
@ -8,8 +8,8 @@
|
||||
|
||||
import sys
|
||||
import argparse
|
||||
import getpass
|
||||
import math
|
||||
import json
|
||||
try:
|
||||
import gtk
|
||||
except:
|
||||
@ -35,10 +35,85 @@ parser.add_argument('-s', '--fontsize', dest='fontsize', type=int, default=10,
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
### Config Dialog ###
|
||||
class CruSSHConf:
|
||||
### State Vars ###
|
||||
Config = {}
|
||||
MainWin = gtk.Window()
|
||||
|
||||
### Signal Hooks ###
|
||||
def destroy_hook(self, discard, save_func):
|
||||
self.MainWin.destroy()
|
||||
if save_func != None:
|
||||
save_func(self.Config)
|
||||
|
||||
def font_hook(self, fontbutton):
|
||||
self.Config["font"] = fontbutton.get_font_name()
|
||||
|
||||
def opacity_hook(self, range):
|
||||
self.Config["opacity"] = range.get_value()
|
||||
|
||||
### GUI Objects ###
|
||||
def initGUI(self, save_func = None):
|
||||
self.MainWin.set_modal(True)
|
||||
self.MainWin.props.allow_grow = False
|
||||
|
||||
MainBox = gtk.VBox(spacing=5)
|
||||
MainBox.props.border_width = 5
|
||||
self.MainWin.add(MainBox)
|
||||
|
||||
TermConfFrame = gtk.Frame(label="Terminal Options")
|
||||
TermConfTable = gtk.Table(2, 2)
|
||||
TermConfTable.props.border_width = 5
|
||||
TermConfTable.props.row_spacing = 5
|
||||
TermConfTable.props.column_spacing = 5
|
||||
TermConfFrame.add(TermConfTable)
|
||||
MainBox.pack_start(TermConfFrame)
|
||||
|
||||
FontLabel = gtk.Label("Font:")
|
||||
TermConfTable.attach(FontLabel, 1, 2, 1, 2, gtk.EXPAND)
|
||||
FontConf = gtk.FontButton(fontname=self.Config["font"])
|
||||
FontConf.connect("font-set", self.font_hook)
|
||||
TermConfTable.attach(FontConf, 2, 3, 1, 2, gtk.EXPAND)
|
||||
|
||||
OpacityLabel = gtk.Label("Opacity:")
|
||||
TermConfTable.attach(OpacityLabel, 1, 2, 2, 3, gtk.EXPAND)
|
||||
OpacityAdj = gtk.Adjustment(upper=65535, step_incr=1, value=self.Config["opacity"])
|
||||
OpacityScale = gtk.HScale(OpacityAdj)
|
||||
OpacityScale.set_draw_value(False)
|
||||
OpacityScale.connect("value-changed", self.opacity_hook)
|
||||
TermConfTable.attach(OpacityScale, 2, 3, 2, 3)
|
||||
|
||||
ConfirmBox = gtk.HBox(spacing=5)
|
||||
CancelButton = gtk.Button(stock=gtk.STOCK_CANCEL)
|
||||
ConfirmBox.pack_start(CancelButton, fill=False)
|
||||
SaveButton = gtk.Button(stock=gtk.STOCK_SAVE)
|
||||
ConfirmBox.pack_start(SaveButton, fill=False)
|
||||
MainBox.pack_start(ConfirmBox, fill=False, expand=False)
|
||||
|
||||
# wire up behaviour
|
||||
CancelButton.connect("clicked", lambda discard: self.MainWin.destroy())
|
||||
SaveButton.connect("clicked", self.destroy_hook, save_func)
|
||||
|
||||
self.MainWin.show_all()
|
||||
|
||||
# we'll wire up a supplied save_func that takes the Config dict as an argument.
|
||||
def __init__(self, config=None, save_func=None):
|
||||
if config != None:
|
||||
self.Config = config
|
||||
|
||||
self.initGUI(save_func)
|
||||
|
||||
### CruSSH! ###
|
||||
class CruSSH:
|
||||
### Config Vars ###
|
||||
Config = {}
|
||||
# config defaults
|
||||
Config = {
|
||||
"login": None,
|
||||
"port": None,
|
||||
"font": "Ubuntu Mono Bold 10",
|
||||
"opacity": 65535
|
||||
}
|
||||
|
||||
### State Vars ###
|
||||
Terminals = {}
|
||||
@ -46,7 +121,7 @@ class CruSSH:
|
||||
TermMinHeight = 1
|
||||
|
||||
### GUI Objects ###
|
||||
MainWin = gtk.Window(type=gtk.WINDOW_TOPLEVEL)
|
||||
MainWin = gtk.Window()
|
||||
ScrollWin = gtk.ScrolledWindow()
|
||||
LayoutTable = gtk.Table()
|
||||
EntryBox = gtk.Entry()
|
||||
@ -86,6 +161,15 @@ class CruSSH:
|
||||
self.reflowTable(cols, rows)
|
||||
self.MainWin.show_all()
|
||||
|
||||
def configTerminals(self):
|
||||
for host in self.Terminals:
|
||||
terminal = self.Terminals[host]
|
||||
terminal.set_size(80, 24)
|
||||
terminal.set_font_from_string(self.Config["font"])
|
||||
terminal.set_opacity(int(self.Config["opacity"]))
|
||||
self.TermMinWidth = (terminal.get_char_width() * 80) + terminal.get_padding()[0]
|
||||
self.TermMinHeight = (terminal.get_char_height() * 24) + terminal.get_padding()[1]
|
||||
|
||||
def removeTerminal(self, terminal):
|
||||
# brute force search since we don't actually know the hostname from the
|
||||
# terminal object. this is an infrequent operation, so it should be fine.
|
||||
@ -99,10 +183,36 @@ class CruSSH:
|
||||
self.MainWin.set_title("crussh: " + ' '.join(self.Terminals.keys()))
|
||||
self.MainWin.set_role(role="crussh_main_win")
|
||||
self.MainWin.connect("delete-event", lambda window, event: gtk.main_quit())
|
||||
MainVBox = gtk.VBox()
|
||||
self.MainWin.add(MainVBox)
|
||||
|
||||
MainMenuBar = gtk.MenuBar()
|
||||
MainVBox.pack_start(MainMenuBar, fill=True, expand=False)
|
||||
|
||||
FileItem = gtk.MenuItem(label="File")
|
||||
FileMenu = gtk.Menu()
|
||||
FileItem.set_submenu(FileMenu)
|
||||
QuitItem = gtk.MenuItem(label="Quit")
|
||||
QuitItem.connect("activate", lambda discard: gtk.main_quit())
|
||||
FileMenu.append(QuitItem)
|
||||
MainMenuBar.append(FileItem)
|
||||
|
||||
EditItem = gtk.MenuItem(label="Edit")
|
||||
EditMenu = gtk.Menu()
|
||||
EditItem.set_submenu(EditMenu)
|
||||
PrefsItem = gtk.MenuItem(label="Preferences")
|
||||
def save_func(new_config):
|
||||
self.Config = new_config
|
||||
self.configTerminals()
|
||||
self.reflow()
|
||||
PrefsItem.connect("activate", lambda discard: CruSSHConf(self.Config, save_func))
|
||||
EditMenu.append(PrefsItem)
|
||||
MainMenuBar.append(EditItem)
|
||||
|
||||
self.ScrollWin.props.hscrollbar_policy = gtk.POLICY_NEVER
|
||||
self.ScrollWin.props.vscrollbar_policy = gtk.POLICY_ALWAYS
|
||||
self.ScrollWin.props.shadow_type = gtk.SHADOW_ETCHED_IN
|
||||
MainVBox.pack_start(self.ScrollWin)
|
||||
|
||||
self.LayoutTable.set_homogeneous(True)
|
||||
self.LayoutTable.set_row_spacings(1)
|
||||
@ -126,11 +236,7 @@ class CruSSH:
|
||||
return True
|
||||
self.EntryBox.connect("key_press_event", feed_input)
|
||||
self.EntryBox.connect("key_release_event", feed_input)
|
||||
|
||||
VBox = gtk.VBox()
|
||||
VBox.pack_start(self.ScrollWin)
|
||||
VBox.pack_start(self.EntryBox, False, False)
|
||||
self.MainWin.add(VBox)
|
||||
MainVBox.pack_start(self.EntryBox, False, False)
|
||||
|
||||
# reflow layout on size change.
|
||||
self.MainWin.connect("size-allocate", lambda widget, allocation: self.reflow())
|
||||
@ -139,16 +245,15 @@ class CruSSH:
|
||||
self.EntryBox.props.has_focus = True
|
||||
|
||||
def __init__(self, hosts, login=None, port=None):
|
||||
self.Config["login"] = login
|
||||
self.Config["port"] = port
|
||||
# load existing config file, if present
|
||||
try:
|
||||
Config = json.load(open('~/.crusshrc'))
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
# init all terminals
|
||||
for host in hosts:
|
||||
terminal = vte.Terminal()
|
||||
terminal.set_size(80, 24)
|
||||
terminal.set_font_from_string("Ubuntu Mono Bold " + str(args.fontsize))
|
||||
self.TermMinWidth = (terminal.get_char_width() * 80) + terminal.get_padding()[0]
|
||||
self.TermMinHeight = (terminal.get_char_height() * 24) + terminal.get_padding()[1]
|
||||
# TODO: disable only this terminal widget on child exit
|
||||
# v.connect("child-exited", lambda term: gtk.main_quit())
|
||||
cmd_str = "/usr/bin/ssh"
|
||||
@ -163,7 +268,9 @@ class CruSSH:
|
||||
|
||||
# hook terminals so they reflow layout on exit
|
||||
self.Terminals[host].connect("child-exited", self.removeTerminal)
|
||||
|
||||
# configure all terminals
|
||||
self.configTerminals()
|
||||
# reflow after reconfig for font size changes
|
||||
self.initGUI()
|
||||
self.reflow(force=True)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user