-
Notifications
You must be signed in to change notification settings - Fork 7
/
glance
executable file
·131 lines (107 loc) · 4.09 KB
/
glance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
# Glance v0.1
# Released under BSD license. (c) [email protected]
# http://www.gulecha.org
import pygtk
import sys
import gtk
import os
def run_program(cmd):
#returns (output, exit value)
fd=os.popen(cmd,"r")
output=fd.read()
exitvalue=fd.close()
return (output,exitvalue)
def sanitize_string(clipstr):
b=""
for a in clipstr:
if a.isalnum() or a in "'-.,* ":
b = b + a
#trim whitespaces
b = b.strip()
if len(b) > 50:
return ""
else:
return b
helpmsg="Enter a word to lookup definition.\n\nTry an exact word, ex: 'Monarch', or suffix/prefix, ex: '*arch' or 'mon*'\n\nIf you find this simple program useful, and have suggestions to make it better, let Anil know."
class LookupWindow:
def destroy(self, widget):
gtk.main_quit()
def filldef(self, widget, dbuffer):
word=widget.get_text()
word=sanitize_string(word)
if word=="":
return
option=""
if word[-1]=="*":
option = " -s prefix "
if word[:1]=="*":
option = " -s suffix "
if len(word)>2 and word[-1]=="*" and word[:1]=="*" :
option = " -s substring "
content,exitval=run_program("dict " + option + '"' + word + '" 2>&1')
if exitval != None:
content="Nothing found!\n--\n" + content + "\n--\n" + helpmsg
dbuffer.set_text(content)
def __init__(self):
# Create a new dialog window for the scrolled window to be
# packed into.
window = gtk.Dialog()
window.connect("destroy", self.destroy)
window.set_title("Glance: Quick Word-lookuper")
window.set_border_width(5)
window.set_size_request(600, 500)
window.set_resizable(False)
window.set_gravity(gtk.gdk.GRAVITY_SOUTH_WEST)
width, height = window.get_size()
window.move(gtk.gdk.screen_width() - width, gtk.gdk.screen_height() - height)
# create a new scrolled window.
scrolled_window = gtk.ScrolledWindow()
scrolled_window.set_border_width(5)
# the policy is one of POLICY AUTOMATIC, or POLICY_ALWAYS.
# POLICY_AUTOMATIC will automatically decide whether you need
# scrollbars, whereas POLICY_ALWAYS will always leave the scrollbars
# there. The first one is the horizontal scrollbar, the second, the
# vertical.
scrolled_window.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
# The dialog window is created with a vbox packed into it.
window.vbox.pack_start(scrolled_window, True, True, 0)
scrolled_window.show()
# create a table of 10 by 10 squares.
table = gtk.Table(1, 2, False)
# pack the table into the scrolled window
scrolled_window.add_with_viewport(table)
table.show()
selected_word=gtk.Clipboard(selection="PRIMARY").wait_for_text()
defview = gtk.TextView(buffer=None)
dbuffer = defview.get_buffer()
entrybox = gtk.Entry()
entrybox.set_text(selected_word)
entrybox.connect_object("activate", self.filldef, entrybox, dbuffer)
table.attach(entrybox, 0, 1, 0, 1, yoptions=gtk.SHRINK, xpadding=10, ypadding=5)
entrybox.show()
defview.set_justification(gtk.JUSTIFY_LEFT)
defview.set_editable(False)
defview.set_left_margin(10)
defview.set_right_margin(10)
dbuffer.set_text(helpmsg)
table.attach(defview, 0, 1, 1, 2)
defview.show()
self.filldef(entrybox,dbuffer)
# Add a "close" button to the bottom of the dialog
button = gtk.Button("close")
button.connect_object("clicked", self.destroy, window)
# this makes it so the button is the default.
button.set_flags(gtk.CAN_DEFAULT)
window.action_area.pack_start( button, True, True, 0)
# This grabs this button to be the default button. Simply hitting
# the "Enter" key will cause this button to activate.
button.grab_default()
button.show()
window.show()
def main():
gtk.main()
return 0
if __name__ == "__main__":
LookupWindow()
main()