Skip to content

Commit e593aa8

Browse files
authored
Merge pull request #922 from rolandwalker/sql-syntax-highlighting
Allow fine-grained customization of Pygments SQL highlighting styles
2 parents f9a4d94 + 46faaee commit e593aa8

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Features:
66

77
* Add an option `--init-command` to execute SQL after connecting (Thanks: [KITAGAWA Yasutaka]).
88
* Use InputMode.REPLACE_SINGLE
9+
* Allow customization of Pygments SQL syntax-highlighting styles.
910
* Add a `\clip` special command to copy queries to the system clipboard.
1011
* Add a special command `\pipe_once` to pipe output to a subprocess.
1112

mycli/clistyle.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,36 @@
4444
v: k for k, v in TOKEN_TO_PROMPT_STYLE.items()
4545
}
4646

47+
# all tokens that the Pygments MySQL lexer can produce
48+
OVERRIDE_STYLE_TO_TOKEN = {
49+
'sql.comment': Token.Comment,
50+
'sql.comment.multi-line': Token.Comment.Multiline,
51+
'sql.comment.single-line': Token.Comment.Single,
52+
'sql.comment.optimizer-hint': Token.Comment.Special,
53+
'sql.escape': Token.Error,
54+
'sql.keyword': Token.Keyword,
55+
'sql.datatype': Token.Keyword.Type,
56+
'sql.literal': Token.Literal,
57+
'sql.literal.date': Token.Literal.Date,
58+
'sql.symbol': Token.Name,
59+
'sql.quoted-schema-object': Token.Name.Quoted,
60+
'sql.quoted-schema-object.escape': Token.Name.Quoted.Escape,
61+
'sql.constant': Token.Name.Constant,
62+
'sql.function': Token.Name.Function,
63+
'sql.variable': Token.Name.Variable,
64+
'sql.number': Token.Number,
65+
'sql.number.binary': Token.Number.Bin,
66+
'sql.number.float': Token.Number.Float,
67+
'sql.number.hex': Token.Number.Hex,
68+
'sql.number.integer': Token.Number.Integer,
69+
'sql.operator': Token.Operator,
70+
'sql.punctuation': Token.Punctuation,
71+
'sql.string': Token.String,
72+
'sql.string.double-quouted': Token.String.Double,
73+
'sql.string.escape': Token.String.Escape,
74+
'sql.string.single-quoted': Token.String.Single,
75+
'sql.whitespace': Token.Text,
76+
}
4777

4878
def parse_pygments_style(token_name, style_object, style_dict):
4979
"""Parse token type and style string.
@@ -108,6 +138,9 @@ def style_factory_output(name, cli_style):
108138
elif token in PROMPT_STYLE_TO_TOKEN:
109139
token_type = PROMPT_STYLE_TO_TOKEN[token]
110140
style.update({token_type: cli_style[token]})
141+
elif token in OVERRIDE_STYLE_TO_TOKEN:
142+
token_type = OVERRIDE_STYLE_TO_TOKEN[token]
143+
style.update({token_type: cli_style[token]})
111144
else:
112145
# TODO: cli helpers will have to switch to ptk.Style
113146
logger.error('Unhandled style / class name: %s', token)

mycli/myclirc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ table_format = ascii
4141
# friendly, monokai, paraiso, colorful, murphy, bw, pastie, paraiso, trac, default,
4242
# fruity.
4343
# Screenshots at http://mycli.net/syntax
44+
# Can be further modified in [colors]
4445
syntax_style = default
4546

4647
# Keybindings: Possible values: emacs, vi.
@@ -113,6 +114,35 @@ output.odd-row = ""
113114
output.even-row = ""
114115
output.null = "#808080"
115116

117+
# SQL syntax highlighting overrides
118+
# sql.comment = 'italic #408080'
119+
# sql.comment.multi-line = ''
120+
# sql.comment.single-line = ''
121+
# sql.comment.optimizer-hint = ''
122+
# sql.escape = 'border:#FF0000'
123+
# sql.keyword = 'bold #008000'
124+
# sql.datatype = 'nobold #B00040'
125+
# sql.literal = ''
126+
# sql.literal.date = ''
127+
# sql.symbol = ''
128+
# sql.quoted-schema-object = ''
129+
# sql.quoted-schema-object.escape = ''
130+
# sql.constant = '#880000'
131+
# sql.function = '#0000FF'
132+
# sql.variable = '#19177C'
133+
# sql.number = '#666666'
134+
# sql.number.binary = ''
135+
# sql.number.float = ''
136+
# sql.number.hex = ''
137+
# sql.number.integer = ''
138+
# sql.operator = '#666666'
139+
# sql.punctuation = ''
140+
# sql.string = '#BA2121'
141+
# sql.string.double-quouted = ''
142+
# sql.string.escape = 'bold #BB6622'
143+
# sql.string.single-quoted = ''
144+
# sql.whitespace = ''
145+
116146
# Favorite queries.
117147
[favorite_queries]
118148

0 commit comments

Comments
 (0)