promptbuilder.py 8.3 KB
Newer Older
1
#!/usr/bin/env bashstyle --python
2 3 4 5 6 7 8
#coding=utf-8
#########################################################
# 							#
# This is BashStyle-NG  				#
#							#
# Licensed under GNU GENERAL PUBLIC LICENSE v3		#
#							#
9
# Copyright 2007 - 2016 Christopher Bratusek		#
10 11
#							#
#########################################################
12

Christopher Roy Bratusek's avatar
Christopher Roy Bratusek committed
13
MODULES = [ 'sys', 'undobuffer', 'widgethandler', 'dicts', 'prompts' ]
14 15 16 17 18 19 20 21 22 23 24 25

FAILED = []

for module in MODULES:
	try:
		globals()[module] = __import__(module)
	except ImportError:
		FAILED.append(module)

try:
	from gi.repository import Gtk
except ImportError:
26
	FAILED.append(_("Gtk (from gi.repository)"))
27 28

if FAILED:
29
    print(_("The following modules failed to import: %s") % (" ".join(FAILED)))
30 31 32 33
    sys.exit(1)

class PromptBuilder(object):

34
	def __init__(self, config, userconfig, factoryconfig):
35 36
		######################## load translations & widgethandler #########################
		gtkbuilder = widgethandler.gtkbuilder
37

38 39
		######################## GtkTextView ###############################################

40 41 42 43 44 45 46 47 48 49 50 51
		self.prompt_command = gtkbuilder.get_object("prompt_command")

		self.prompt_command_buffer = undobuffer.UndoableBuffer()
		self.prompt_command.set_buffer(self.prompt_command_buffer)
		self.prompt_command_buffer.set_text("%s" % config["Custom"]["command"])

		self.custom_prompt = gtkbuilder.get_object("custom_prompt")

		self.custom_prompt_buffer = undobuffer.UndoableBuffer()
		self.custom_prompt.set_buffer(self.custom_prompt_buffer)
		self.custom_prompt_buffer.set_text("%s" % config["Custom"]["prompt"])

52
		def set_custom_prompt(widget, setting):
53 54
			start = widget.get_start_iter()
			end = widget.get_end_iter()
55
			config["Custom"]["{}".format(setting)] = widget.get_text(start, end, False)
56

57 58
		self.prompt_command_buffer.connect("changed", set_custom_prompt, "command")
		self.custom_prompt_buffer.connect("changed", set_custom_prompt, "prompt")
59

60
		self.active_buffer = "P_C"
61

62 63
		def set_active_buffer(widget, data, buffer):
			self.active_buffer = buffer
64

65 66
		self.prompt_command.connect("focus-in-event", set_active_buffer, "P_C")
		self.custom_prompt.connect("focus-in-event", set_active_buffer, "PS1")
67

68
		######################## Helper Functions ##########################################
69

70
		def prompt_add(widget, text_p_c, text_ps1):
71
			if self.active_buffer == "P_C":
72
				self.prompt_command_buffer.insert_at_cursor(text_p_c)
73
			elif self.active_buffer == "PS1":
74
				self.custom_prompt_buffer.insert_at_cursor(text_ps1)
75

76
		def prompt_add_combo(widget, dict_p_c, dict_ps1):
77
			if widget.get_active() != 0:
78
				prompt_add(widget, dict_p_c[widget.get_active()], dict_ps1[widget.get_active()])
79
				widget.set_active(0)
80

81
		######################## GtkButtons ################################################
82

83 84 85
		self.empty = gtkbuilder.get_object("cpb_empty")
		self.undo = gtkbuilder.get_object("cpb_undo")
		self.redo = gtkbuilder.get_object("cpb_redo")
86 87
		self.reset = gtkbuilder.get_object("cpb_reset")
		self.factory = gtkbuilder.get_object("cpb_factory")
88

89
		def do_empty(widget):
90 91 92 93
			if self.active_buffer == "P_C":
				self.prompt_command_buffer.set_text("")
			elif self.active_buffer == "PS1":
				self.custom_prompt_buffer.set_text("")
94

95
		def do_undo(widget):
96 97 98 99
			if self.active_buffer == "P_C":
				self.prompt_command_buffer.undo()
			elif self.active_buffer == "PS1":
				self.custom_prompt_buffer.undo()
100

101
		def do_redo(widget):
102 103 104 105
			if self.active_buffer == "P_C":
				self.prompt_command_buffer.redo()
			elif self.active_buffer == "PS1":
				self.custom_prompt_buffer.redo()
106

107
		def do_reset(widget):
108 109
			self.prompt_command_buffer.set_text("%s" % userconfig["Custom"]["command"])
			self.custom_prompt_buffer.set_text("%s" % userconfig["Custom"]["prompt"])
110 111

		def do_revert(widget):
112 113
			self.prompt_command_buffer.set_text("%s" % factoryconfig["Custom"]["command"])
			self.custom_prompt_buffer.set_text("%s" % factoryconfig["Custom"]["prompt"])
114

115 116 117
		self.empty.connect("clicked", do_empty)
		self.undo.connect("clicked", do_undo)
		self.redo.connect("clicked", do_redo)
118 119
		self.reset.connect("clicked", do_reset)
		self.factory.connect("clicked", do_revert)
120

121
		######################## Toolbox ###################################################
122

123 124 125
		self.show_toolbox = gtkbuilder.get_object("show_toolbox")

		def do_show_toolbox(widget, data=None):
126 127 128 129 130
                        toolbox = gtkbuilder.get_object("Toolbox")
                        toolbox_close = gtkbuilder.get_object("toolbox.close")
                        toolbox.show_all()
                        toolbox_close.connect("clicked", lambda w: toolbox.hide() or True)
                        toolbox.connect("delete-event", lambda w, e: w.hide() or True)
131

132
		self.show_toolbox.connect("clicked", do_show_toolbox)
133

134 135
		######################## Toolbox Buttons ###########################################

136
		def load_toolbutton(object, text_p_c, text_ps1):
137
			widget = gtkbuilder.get_object("%s" % object)
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
			widget.connect("clicked", prompt_add, text_p_c, text_ps1)

		load_toolbutton("username", "${USER}", "\\u")
		load_toolbutton("hostname", "${HOSTNAME/.*}", "\\h")
		load_toolbutton("fhostname", "${HOSTNAME}", "\\H")
		load_toolbutton("time", "$(date +%H:%M:%S)", "\\t")
		load_toolbutton("date", "$(date date +%d.%m.%Y)", "\\d")
		load_toolbutton("sign", "", "\\$")
		load_toolbutton("fworkdir", "${PWD}", "\\w")
		load_toolbutton("workdir", "${PWD/*\/}", "\\W")
		load_toolbutton("euid", "${EUID}", "${EUID}")
		load_toolbutton("jobs", "$(jobs -pr)", "\\j")
		load_toolbutton("bang", "", "\\!")
		load_toolbutton("number", "", "\\#")
		load_toolbutton("pid", "${BAHSPID}", "${BASHPID}")
		load_toolbutton("shlvl", "${SHLVL}", "${SHLVL}")
		load_toolbutton("truncpwd", "$(truncpwd)", "\\$(truncpwd)")
		load_toolbutton("showsize", "$(systemkit dirsize)", "\\$(systemkit dirsize)")
		load_toolbutton("countprocesses", "$(systemkit processes)", "\\$(systemkit processes)")
		load_toolbutton("showuptime", "$(systemkit uptime)", "\\$(systemkit uptime)")
		load_toolbutton("showtty", "$(systemkit tty)", "\\$(systemkit tty)")
		load_toolbutton("showcpuload", "$(systemkit cpuload)", "\\$(systemkit cpuload)")
		load_toolbutton("showseconds", "${SECONDS}", "${SECONDS}")
		load_toolbutton("showbatteryload", "$(systemkit battery)", "\\$(systemkit battery)")
162 163 164
		load_toolbutton("showexit", "${lastexit}", "${lastexit}")
		load_toolbutton("showlastcmd", "${lastcommand}", "${lastcommand}")
		load_toolbutton("showlastcmd_cut", "${lastcommandprintable}", "${lastcommandprintable}")
165

166
		######################## Toolbox Comboboxes ########################################
167

168
		def load_toolcombo(object, dict_p_c, dict_ps1):
169 170
			widget = gtkbuilder.get_object("%s" % object)
			widget.set_active(0)
171
			widget.connect("changed", prompt_add_combo, dict_p_c, dict_ps1)
172

173 174 175 176 177
		load_toolcombo("showmem", dicts.memory_getters_p_c, dicts.memory_getters_ps1)
		load_toolcombo("showspace", dicts.space_getters_p_c, dicts.space_getters_ps1)
		load_toolcombo("countfiles", dicts.counters_p_c, dicts.counters_ps1)
		load_toolcombo("showload", dicts.load_getters_p_c, dicts.load_getters_ps1)
		load_toolcombo("insert_color", dicts.symbolic_colors_p_c, dicts.symbolic_colors_ps1)
178

179 180
		######################## Default Styles ############################################

181 182 183 184 185 186 187
		self.insert_prompt = gtkbuilder.get_object("insert_prompt")
		self.insert_prompt.set_active(0)

		styles_pc = {
			      1 : prompts.empty_pc,
			      2 : prompts.empty_pc,
			      3 : prompts.floating_clock_pc,
188
			      4 : prompts.equinox_pc,
189 190 191 192 193 194 195 196 197 198 199 200 201 202
			      5 : prompts.empty_pc,
			      6 : prompts.poweruser_pc,
			      7 : prompts.empty_pc,
			      8 : prompts.empty_pc,
			      9 : prompts.empty_pc,
			     10 : prompts.empty_pc,
			     11 : prompts.empty_pc,
			     12 : prompts.ayoli_pc,
			    }

		styles_ps1 = {
			       1 : prompts.separator_ps,
			       2 : prompts.vector_ps,
			       3 : prompts.floating_clock_ps,
203
			       4 : prompts.equinox_ps,
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
			       5 : prompts.elite_ps,
			       6 : prompts.poweruser_ps,
			       7 : prompts.dirks_ps,
			       8 : prompts.dotprompt_ps,
			       9 : prompts.sepang_ps,
			      10 : prompts.quirk_ps,
			      11 : prompts.sputnik_ps,
			      12 : prompts.ayoli_ps,
			     }

		def do_insert_prompt(widget, data=None):
			selection = widget.get_active()
			if selection != 0:
				self.prompt_command_buffer.set_text(styles_pc[selection])
				self.custom_prompt_buffer.set_text(styles_ps1[selection])

		self.insert_prompt.connect("changed", do_insert_prompt)