4
4
# clicompanion.py - commandline tool.
6
# Copyright 2010 Duane Hinnen
6
# Copyright 2010 Duane Hinnen, Kenny Meyer
8
8
# This program is free software: you can redistribute it and/or modify it
9
9
# under the terms of the GNU General Public License version 3, as published
20
20
# IMPORTANT: you need to move the .cheatsheet file to your $HOME
29
27
pygtk.require('2.0')
34
cheatsheet = os.path.expanduser("~/.clicompanion")
32
CHEATSHEET = os.path.expanduser("~/.clicompanion")
33
CONFIG_ORIG = "/etc/clicompanion.d/clicompanion.config"
38
# create the terminal and set its size
40
39
vte = vte.Terminal()
41
40
vte.set_size_request(700, 350)
42
41
# fork_command() will run a command, in this case it shows a prompt
43
42
vte.fork_command('bash')
44
#copy config file to user $HOME if does not exist
46
if not os.path.isfile (CHEATSHEET):
47
os.system ("cp %s %s" % (CONFIG_ORIG, CHEATSHEET))
45
49
# close the window and quit
46
50
def delete_event(self, widget, data=None):
92
96
def responseToDialog(self, text, dialog, response):
93
97
dialog.response(response)
95
# Add command dialog box
99
# Add command dialog box
96
100
def add_command(self, widget, data=None):
97
101
# Create Dialog object
98
102
dialog = gtk.MessageDialog(
130
134
#add it and show it
131
135
dialog.vbox.pack_end(hbox2, True, True, 0)
132
136
dialog.vbox.pack_end(hbox1, True, True, 0)
134
137
dialog.show_all()
136
138
# Show the dialog
140
142
text1 = entry1.get_text()
141
143
text2 = entry2.get_text()
142
144
text3 = entry3.get_text()
143
# open flat file that contains the command dictionary
144
with open(cheatsheet, "a") as cheatfile:
145
cheatfile.write(text1+" :"+text2+" : "+text3+'\n')
146
# open flat file that contains the commands and add the new command
147
with open(CHEATSHEET, "a") as cheatfile:
149
cheatfile.write(text1+" :"+text2+" : "+text3+'\n')
151
l = str(text1+" :"+text2+" : "+text3)
154
self.liststore.append([ls[0],ls[1],ls[2]])
149
157
# The destroy method must be called otherwise the 'Close' button will
154
# Remove command method
162
# Remove command from command file and GUI
155
163
def remove_command(self, widget, data=None):
156
row_int = int(row[0][0]) #convert pathlist into something usable
164
row_int = int(ROW[0][0]) #convert pathlist into something usable
157
165
del self.liststore[row_int] #remove line from list
159
167
# open command file and delete line so the change is persistent
160
with open(cheatsheet, "r") as cheatfile:
168
with open(CHEATSHEET, "r") as cheatfile:
161
169
cheatlines = cheatfile.readlines()
162
170
del cheatlines[row_int]
163
171
cheatfile.close()
164
with open(cheatsheet, "w") as cheatfile2:
172
with open(CHEATSHEET, "w") as cheatfile2:
165
173
cheatfile2.writelines(cheatlines)
166
174
cheatfile2.close()
168
176
def _filter_commands(self, widget, data=None):
169
177
"""Show commands matching a given search term.
171
179
The user should enter a term in the search box and the treeview should
172
180
only display the rows which contain the search term. Pretty
181
straight-forward. DH: Maybe for a smart guy like you ;)
176
184
# Get the text from the search box
192
202
#send the command to the terminal
193
203
def run_command(self, widget, data=None):
196
row_int = int(row[0][0]) # removes everything but number. Before: [5,]
206
row_int = int(ROW[0][0]) # removes everything but number from EX: [5,]
198
cmnd = states[row_int] #states is where commands are stored
208
cmnd = STATES[row_int] #STATES is where commands are stored
199
209
if not self.liststore[row_int][1] == " ": # command with user input
200
210
text = Companion.get_info(self, text)
201
211
#print text #debug
206
216
Companion.vte.show()
207
217
Companion.vte.grab_focus()
209
#open the website that is the help file
210
def open_site(self, widget, data=None):
211
siteOpen = webbrowser.open("http://okiebuntu.homelinux.com")
219
#open the man page for selected command
220
def man_page(self, widget, data=None):
221
row_int = int(ROW[0][0]) # removes everything but number from EX: [5,]
222
cmnd = STATES[row_int] #STATES is where commands are store
223
splitcommand=cmnd.split(" ")
224
Companion.vte.feed_child("man "+splitcommand[0]+"\n") #send command
214
228
# open file containing command dictionary and put it in a variable
215
229
def update(self):
216
with open(cheatsheet, "r") as cheatfile:
230
with open(CHEATSHEET, "r") as cheatfile:
217
231
bugdata=cheatfile.read()
218
232
cheatfile.close()
221
235
# add bug data from .clicompanion to the liststore
223
for line in bugdata.splitlines():
237
for line in sorted(bugdata.splitlines()):
224
238
l = line.split(':',2)
225
self.states.append(l[0])
226
240
self.liststore.append([l[0],l[1],l[2]])
228
243
def __init__(self):
229
246
# Create a new window
230
247
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
231
self.window.set_title("CLI Companion")
248
self.window.set_title("CLI Companion")
232
249
# Sets the border width of the window.
233
250
self.window.set_border_width(10)
234
251
#set the size of the window
279
296
self.treeview.get_selection().set_mode(gtk.SELECTION_SINGLE)
280
297
self.treeview.get_selection().connect('changed',lambda s: mark_selected(s))
284
300
def mark_selected(treeselection):
285
301
(model,pathlist)=treeselection.get_selected_rows()
288
304
#print pathlist #debug
293
309
self.scrolledwindow = gtk.ScrolledWindow()
294
310
self.scrolledwindow.set_size_request(700, 220)
296
def buttonBox(self, spacing, layout):
312
def button_box(self, spacing, layout):
297
313
#button box at bottom of main window
298
314
frame = gtk.Frame()
299
315
bbox = gtk.HButtonBox()
330
346
self.vbox.pack_start(self.scrolledwindow)
331
347
self.vbox.pack_start(self.search_box, True, True, 10)
332
348
self.vbox.pack_start(self.vte, True, True, 0)
333
self.vbox.pack_start(buttonBox( self, 10, gtk.BUTTONBOX_END), True, True, 5)
349
self.vbox.pack_start(button_box( self, 10, gtk.BUTTONBOX_END), True, True, 5)
335
351
self.scrolledwindow.add(self.treeview)
336
352
self.window.add(self.vbox)