-
Notifications
You must be signed in to change notification settings - Fork 0
/
text-gui
executable file
·102 lines (80 loc) · 3.61 KB
/
text-gui
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
#!/usr/bin/env python
# text-gui - Show arbitrary text in a simple GUI.
# May be useful in scripts bound to window manager shortcuts or the like.
# TODO:
# - ctrl+s to save to a file?
# settings
# dimensions (defaults; can be overridden by arguments)
default_width = 700
default_height = 800
default_max_width = 700
default_max_height = 800
# theme
text_fg = "#38f"
text_bg = "#320"
cursor_bg = "#eee"
title_fg = "#eee"
title_bg = "#111"
scroll_fg = text_fg
scroll_bg = text_bg
# actions
actions = []
# code
import os, argparse
from sys import stdin
from tkinter import *
from tkinter import scrolledtext
import tkinter.font as tkFont
def change_font_size(font, change):
"""Add CHANGE to the size of the font."""
font.configure(size=font["size"]+change)
def get_input_text(widget):
"""Get the string of text in the input field."""
return ''.join(map(lambda x: x[1], widget.dump('1.0', END, text=True)))[:-1]
def run_action(widget, command):
"""Run COMMAND, passing the text of WIDGET to its stdin."""
text = get_input_text(widget)
pipe = os.popen(command, 'w')
pipe.write(text)
pipe.close()
def make_window(text, title=None, width=default_width, height=default_height, max_width=default_max_width, max_height=default_max_height):
"""Create the text-gui window."""
root = Tk()
root.title(title or "text-gui")
root.minsize(width, height)
if max_width:
root.maxsize(max_width, max_height)
font = tkFont.Font(size=12)
if title:
label = Label(root, text=title, font=tkFont.Font(size=13), fg=title_fg, bg=title_bg)
label.pack(fill=X)
textbox = scrolledtext.ScrolledText(root, font=font, fg=text_fg, bg=text_bg, insertbackground=cursor_bg)
textbox.vbar.config(troughcolor=scroll_bg, bg=scroll_fg)
textbox.pack(fill=BOTH, expand=True)
textbox.insert(END, text)
root.bind("<Control-equal>", lambda e: change_font_size(font, 1))
root.bind("<Control-minus>", lambda e: change_font_size(font, -1))
root.bind("<Escape>", lambda e: quit())
root.bind("<Control-g>", lambda e: quit())
for action in actions:
binding, command = action.split('=', maxsplit=1)
root.bind("<" + binding + ">", lambda e: run_action(textbox, command))
root.mainloop()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Show arbitrary text in a simple GUI.")
parser.add_argument("-T", "--title", dest="title", help="The title of the window")
parser.add_argument("-t", "--text", dest="text", help="The text of the window; if not provided, will be read from stdin")
parser.add_argument("-W", "--width", dest="width", type=int, help="The window's initial width")
parser.add_argument("-H", "--height", dest="height", type=int, help="The window's initial height")
parser.add_argument("--max-width", dest="max_width", type=int, help="The window's max width")
parser.add_argument("--max-height", dest="max_height", type=int, help="The window's max height")
parser.add_argument("-a", "--action", dest="action", nargs='*', type=str, help="Set the command to run when a specified key is pressed. The command is passed the textual contents of the GUI via stdin. Example: --action Control-s=foo will send the text to the stdin of \"foo\" when ctrl+s is pressed. Note that this argument can be passed multiple times.")
args = parser.parse_args()
text = args.text or stdin.read()
title = args.title
width = args.width or default_width
height = args.height or default_height
max_width = args.max_width or default_max_width
max_height = args.max_height or default_max_height
actions = args.action or []
make_window(text, title=title, width=width, height=height, max_width=max_width, max_height=max_height)