Skip to content

Instantly share code, notes, and snippets.

@webgtx
Created May 26, 2024 10:38
Show Gist options
  • Save webgtx/39bd5406982b334f628ce2a11a512e01 to your computer and use it in GitHub Desktop.
Save webgtx/39bd5406982b334f628ce2a11a512e01 to your computer and use it in GitHub Desktop.
from prompt_toolkit import prompt, HTML
from prompt_toolkit import print_formatted_text as print
from prompt_toolkit.application import run_in_terminal
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.lexers import PygmentsLexer
from prompt_toolkit.styles import Style
from prompt_toolkit.completion import NestedCompleter
from prompt_toolkit.validation import Validator, ValidationError
from os import getcwd
from time import sleep
from pygments.lexers.markup import MarkdownLexer
import csv
bindings = KeyBindings()
def draw_ladder():
for i in range(30):
print(HTML(f"<style bg='red'>{'0' * i}</style>"))
# sleep(.2)
def print_yo():
print("yo B)")
draw_ladder()
@bindings.add("c-t")
def _(e):
run_in_terminal(print_yo)
with open(f"{getcwd()}/curse_words.csv") as csvfile:
curse_words = [word for word in csv.reader(csvfile)][0]
curse_words.pop()
class CurseValidator(Validator):
def validate(self, document):
for bad_word in curse_words:
if document.text.find(bad_word) == 0:
raise ValidationError(message="Don't use bad words >:(")
style = Style.from_dict({
"": "#ff0066",
"request": "#00ffff",
"markdown": "underline ansired"
})
smart_completer = NestedCompleter.from_nested_dict({
"you": {
"are": {
"smart": None,
"cool": None,
"gorgeous": None,
}
},
"goodbye": None
})
plan = prompt([
("class:request", "Enter your text in "),
("class:markdown", "Markdown"),
("class:request", ": ")
], lexer=PygmentsLexer(MarkdownLexer),
style=style,
completer=smart_completer,
validator=CurseValidator(),
vi_mode=True,
bottom_toolbar=lambda: HTML("Welcome to <b>TDMD</b>"),
key_bindings=bindings,
multiline=True
)
print(plan)
confirmation = prompt(HTML("<b><request>Confirm?</request></b>: "), style=style)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment