Skip to content

Commit 7b50f4d

Browse files
committed
add ability to select markdown theme
1 parent 1061785 commit 7b50f4d

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ chatblade can be used with an Azure OpenAI endpoint, in which case in addition t
227227
### Help
228228

229229
```
230-
usage: Chatblade [-h] [--openai-api-key key] [--temperature t] [-c {3.5,4}] [-i] [-s] [-t] [-p name] [-e] [-r] [-n] [-o] [-l] [-S sess] [--session-list] [--session-path]
231-
[--session-dump] [--session-delete] [--session-rename newsess]
230+
usage: Chatblade [-h] [--openai-api-key key] [--temperature t] [-c {3.5,4}] [-i] [-s] [-t] [-p name] [-e] [-r] [-n] [-o] [--theme theme] [-l] [-S sess] [--session-list]
231+
[--session-path] [--session-dump] [--session-delete] [--session-rename newsess]
232232
[query ...]
233233
234234
a CLI Swiss Army Knife for ChatGPT
@@ -244,13 +244,14 @@ options:
244244
-i, --interactive start an interactive chat session. This will implicitly continue the conversation
245245
-s, --stream Stream the incoming text to the terminal
246246
-t, --tokens display what *would* be sent, how many tokens, and estimated costs
247-
-p name, --prompt-file name prompt name - will load the prompt at ~/.config/chatblade/name as system msg
247+
-p name, --prompt-file name prompt name - will load the prompt with that name at ~/.config/chatblade/name or a path to a file
248248
249249
result formatting options:
250250
-e, --extract extract content from response if possible (either json or code block)
251251
-r, --raw print session as pure text, don't pretty print or format
252252
-n, --no-format do not add pretty print formatting to output
253253
-o, --only Only display the response, omit query
254+
--theme theme Set the theme for syntax highlighting see https://pygments.org/styles/, can also be set with CHATBLADE_THEME
254255
255256
session options:
256257
-l, --last alias for '-S last', the default session if none is specified

chatblade/parser.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ def get_openai_key(options):
1919
else:
2020
return None
2121

22+
def get_theme(options):
23+
if options["theme"]:
24+
return options["theme"]
25+
elif "CHATBLADE_THEME" in os.environ:
26+
return os.environ["CHATBLADE_THEME"]
27+
else:
28+
return None
2229

2330
def extract_query(query):
2431
"""The query comes from both the query and any piped input
@@ -40,6 +47,7 @@ def extract_query(query):
4047
def extract_options(options):
4148
options = vars(options) # to map
4249
options["openai_api_key"] = get_openai_key(options)
50+
options["theme"] = get_theme(options)
4351
options["model"] = {"3.5": "gpt-3.5-turbo", "4": "gpt-4"}[options["chat_gpt"]]
4452
del options["query"]
4553
del options["chat_gpt"]
@@ -137,6 +145,12 @@ def parse(args):
137145
help="Only display the response, omit query",
138146
action="store_true",
139147
)
148+
display_opts.add_argument(
149+
"--theme",
150+
metavar="theme",
151+
type=str,
152+
help="Set the theme for syntax highlighting see https://pygments.org/styles/, can also be set with CHATBLADE_THEME",
153+
)
140154

141155
session_opts = parser.add_argument_group("session options")
142156
session_opts.add_argument(

chatblade/printer.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def print_message(message, args):
6262
printable = message.content
6363
if not args.raw:
6464
printable = detect_and_format_message(
65-
message.content, cutoff=1000 if message.role == "user" else None
65+
message.content, cutoff=1000 if message.role == "user" else None, theme=args.theme
6666
)
6767
if not args.no_format:
6868
console.print(Rule(message.role, style=COLORS[message.role]))
@@ -86,7 +86,7 @@ def extract_messages(messages, args):
8686
print(message.content.strip())
8787

8888

89-
def detect_and_format_message(msg, cutoff=None):
89+
def detect_and_format_message(msg, cutoff=None, theme=None):
9090
if cutoff and len(msg) > cutoff:
9191
msg = "... **text shortened** ... " + msg[-cutoff:]
9292
return msg
@@ -95,7 +95,8 @@ def detect_and_format_message(msg, cutoff=None):
9595
return JSON(extract_json(msg))
9696
elif looks_like_markdown(msg):
9797
utils.debug(detected="markdown")
98-
return Markdown(msg)
98+
theme = "monokai" if theme is None else theme
99+
return Markdown(msg,code_theme=theme)
99100
else:
100101
utils.debug(detected="regular")
101102
return msg

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = chatblade
3-
version = 0.2.3
3+
version = 0.3.0
44
description = CLI Swiss Army Knife for ChatGPT
55
long_description = file: README.md
66
long_description_content_type=text/markdown

0 commit comments

Comments
 (0)