-
Notifications
You must be signed in to change notification settings - Fork 5
/
example-init.py
197 lines (174 loc) · 8.13 KB
/
example-init.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#
# Example init.py for documentation purposes
#
# Use this file as a template/example for creating an
# initialization/configuration script for PyCmd. Scripts are loaded and applied
# based on the following rules:
#
# * If present, an init.py script in PyCmd's installation directory is
# automatically executed and defines "global" (system-wide) settings
#
# * If present, an init.py script in %APPDATA%\PyCmd is automatically executed
# and defines "user" settings, possibly overriding the "global" ones
#
# * An additional .py script can be specified using the '-i' switch on the
# command line to define settings custom to a PyCmd session (possibly
# overriding the "global" and "user" ones)
#
# This file lists all the configuration options supported by PyCmd, together
# with default values, explanations and various advice.
#
# An important thing to have in mind: this is a regular Python script that gets
# executed in PyCmd's Python context; therefore, you can do virtually anything
# you want here, like play a song, format your hard-disk or show some custom
# greeting:
print '\n*** Hi, there! ***'
# pycmd_public is a collection of public functions, constants and objects that
# PyCmd "exports" for use within init.py files; you can safely rely on these
# being maintained throughout following versions. The documentation for this module
# can be found in pycmd_public.html.
#
# Note that importing symbols from pycmd_public is optional, as PyCmd automatically
# makes them available within the init.py files; still, having them explicitly
# imported might help you get coding assistance from your Python environment
from pycmd_public import appearance, behavior, abbrev_path # Redundant
# Color configuration is performed by including color specification sequences
# (defined by pycmd_public.color) in your strings, similarly to the ANSI escape
# sequences.
#
# "absolute" color specifications will result in the same color being used no
# matter the current color; some examples are color.Fore.YELLOW, color.Fore.SET_RED,
# color.Back.CLEAR_BRIGHT.
#
# "relative" color options define the color to use in terms of the current color;
# examples: color.Fore.TOGGLE_RED, color.Fore.TOGGLE_BRIGHT.
#
# You will notice that relative specifications are preferred in the default
# settings -- this is in order to make PyCmd work reasonably on any console color
# scheme. The absolute specs are clearer and easier to use, though, you can go
# probably go with them for your customizations.
#
# The console's default color attributes are available as color.Fore.DEFAULT and
# color.Back.DEFAULT.
from pycmd_public import color # Redundant
# The color of the regular user text (relative to the console's default)
#
# Note that this defines only the attributes of the user-typed text, *not* the
# default attributes of the console (i.e. the console's background or the output
# of the executed commands); use the console configuration dialog to change those.
#
# The default value is the console's default:
# appearance.colors.text = ''
appearance.colors.text = ''
# The color of the prompt (relative to the console's default); note that you can
# always override this if you define a custom prompt function -- see below
#
# The default value inverts the brightness (to make it stand out it from regular
# console text):
# appearance.colors.prompt = color.Fore.TOGGLE_BRIGHT
appearance.colors.prompt = color.Fore.TOGGLE_BRIGHT
# The color of text selected for copy/cut operations (relative to the regular
# user text as configured above)
#
# The default value inverts the background and the foreground
# appearance.colors.selection = (color.Fore.TOGGLE_RED +
# color.Fore.TOGGLE_GREEN +
# color.Fore.TOGGLE_BLUE +
# color.Back.TOGGLE_RED +
# color.Back.TOGGLE_GREEN +
# color.Back.TOGGLE_BLUE)
appearance.colors.selection = (color.Fore.TOGGLE_RED +
color.Fore.TOGGLE_GREEN +
color.Fore.TOGGLE_BLUE +
color.Back.TOGGLE_RED +
color.Back.TOGGLE_GREEN +
color.Back.TOGGLE_BLUE)
# The color of the current search filter during a history search (relative to the
# regular user text as configured above)
#
# The default is to highlight the filter by altering both background and the
# foreground:
# appearance.colors.search_filter = (color.Back.TOGGLE_RED +
# color.Back.TOGGLE_BLUE +
# color.Fore.TOGGLE_BRIGHT)
appearance.colors.search_filter = (color.Back.TOGGLE_RED +
color.Back.TOGGLE_BLUE +
color.Fore.TOGGLE_BRIGHT)
# The color of the matched substring(s) when displaying completion alternatives
# (relative to the console's default color)
#
# The default value highlights the matched substrings by toggling their RED bit:
# appearance.colors.completion_match = color.Fore.TOGGLE_RED
appearance.colors.completion_match = color.Fore.TOGGLE_RED
# The color of the current directory in the directory history listing (relative to
# the console's default color)
#
# The default is to obtain an "inverted" effect by toggling the brightness of the
# foreground and background:
# appearance.colors.dir_history_selection = (color.Fore.TOGGLE_BRIGHT +
# color.Back.TOGGLE_BRIGHT)
appearance.colors.dir_history_selection = (color.Fore.TOGGLE_BRIGHT +
color.Back.TOGGLE_BRIGHT)
# Custom prompt function, see below for comments on appearance.prompt
def git_prompt():
"""
Custom prompt that displays the name of the current git branch in addition
to the typical "abbreviated current path" PyCmd prompt.
Requires git & grep to be present in the PATH.
"""
# Many common modules (sys, os, subprocess, time, re, ...) are readily
# shipped with PyCmd, you can directly import them for use in your
# configuration script. If you need extra modules that are not bundled,
# manipulate the sys.path so that they can be found (just make sure that the
# version is compatible with the one used to build PyCmd -- check
# README.txt)
import subprocess
stdout = subprocess.Popen(
'git branch | grep "^*"',
shell=True,
stdout=subprocess.PIPE,
stderr=-1).communicate()[0]
branch_name = stdout.strip(' \n\r*')
path = abbrev_path()
# The current color setting is defined by appearance.colors.prompt
prompt = ''
if branch_name != '':
prompt += color.Fore.TOGGLE_BLUE + '[' + branch_name + ']' + color.Fore.TOGGLE_BLUE + ' '
prompt += path + '> '
return prompt
# Define a custom prompt function.
#
# This is called by PyCmd whenever a prompt is to be displayed. It should return
# a string to be shown as a prompt.
#
# Before the returned string is printed, the text color is set to
# appearance.colors.prompt; but you can always alter it or add more complex
# coloring by embedding color specifications in the returned string (like we do
# in our example git_prompt).
#
# The default is the typical "abbreviated path" prompt:
# appearance.prompt = pycmd_public.abbrev_path_prompt
appearance.prompt = git_prompt
# Make PyCmd be "quiet", i.e. skip its welcome and goodbye messages
#
# Note that even if this is set to True, you can still override it using the
# '-q' (quiet) flag on the command line.
#
# The default is False, i.e. the splash messages are shown:
# behavior.quiet_mode = False
behavior.quiet_mode = False
# Change the way PyCmd handles Tab-completion
#
# Currently, the only accepted (and, of course, default) value is 'bash', giving
# the typical bash-like completion.
#
behavior.completion_mode = 'bash'
# Remember, you can do whatever you want in this Python script!
#
# Also note that you can directly output colored text via the color
# specifications.
print ('*** Enjoy ' +
color.Fore.TOGGLE_RED + color.Fore.TOGGLE_BLUE +
'PyCmd' +
color.Fore.DEFAULT +
'! ***')