Skip to content

Instantly share code, notes, and snippets.

@tararoys
Last active September 1, 2023 05:14
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tararoys/c538b7ae8e1f21db9a794c2c0f5becf4 to your computer and use it in GitHub Desktop.
Save tararoys/c538b7ae8e1f21db9a794c2c0f5becf4 to your computer and use it in GitHub Desktop.
Code to print out all voice commands in the knausj talon repository,

#Talon Voice Commands Cheatsheet

This is a demo for how to get a cheatsheet of all Talon voice commands.

To recreate what I did:

  1. Have talon installed on your computer following the instructions at talonvoice.com
  2. Paste both the cheatsheet.py and cheatsheet.talon into the user directory of ~/talon. This makes these scripts available to talon.
  3. Open the talon repl and type
actions.user.cheatsheet()

This will generate a markdown file in the same directory you put cheatsheet.py. Currently, the markdown file doesn't look very nice because I am misusing markdown so that when I do the next step, I get containers around the html I generate that are easy to select with css selectors. It woud probably be better to just have the python script create html in the first place, or even 4. In a shell, I open pandoc and run the command

> pandoc -s cheatsheet.md -c cheatsheet.css -f markdown-raw_html -t html -o cheatsheet.html

This command says "create a standalone document (-s) from the markdown document cheatsheet.md markdown (cheatsheet.md -f markdown) to html (-t html) with the css styleshees cheatsheet.css (-c cheatsheet.css)"

And that gets me the output, cheatsheet.html

This creates a wepbate that when you print it, has make a twenty-four page document with every talon command on it, formatted in a way where it is easy to find things, very information-dense, and organized.

body {
column-count: 3
}
ul {
margin: 0 0 0 0;
padding: 0 0 0 0
}
pre{
overflow: hidden
}
p {
display: flex;
border: solid black;
margin: 0 0 0 0
}
blockquote{
padding: 0 0 0 0;
margin: unset
}
p > * {
flex: 1;
/*! margin: 0 0 0 0; */
align-content: ;
}
li {
list-style:none
}
li > p {
border: none;
overflow: hidden;
display: block
}
p:nth-child(2n){
background-color:lightgrey
}
from talon import Module, actions, registry
import sys, os
def list_to_markdown_table(file, list_name):
file.write(f"# {list_name} \n\n")
command_list = registry.lists[list_name][0].items()
file.write(f">\n")
file.write(f"> command word {list_name} \n\n")
for key, value in command_list:
file.write( "> **" + key + "** *" + value + "*\n>\n")
file.write("\n\n")
def write_alphabet(file):
list_to_markdown_table(file, 'user.letter')
def write_numbers(file):
list_to_markdown_table(file, 'user.number_key')
def write_modifiers(file):
list_to_markdown_table(file, 'user.modifier_key')
def write_special(file):
list_to_markdown_table(file, 'user.special_key')
def write_symbol(file):
list_to_markdown_table(file, 'user.symbol_key')
def write_arrow(file):
list_to_markdown_table(file, 'user.arrow_key')
def write_punctuation(file):
list_to_markdown_table(file, 'user.punctuation')
def write_function(file):
list_to_markdown_table(file, 'user.function_key')
def write_formatters(file):
file.write(f"# formatters \n\n")
command_list = registry.lists['user.formatters'][0].items()
file.write("> command word user.formatters \n")
# file.write("|------|-----|\n")
for key, value in command_list:
file.write( "> **"+ key + "** `" + actions.user.formatted_text(f"example of formatting with {key}", key) + "` \n>\n")
def write_context_commands(file, commands):
# write out each command and it's implementation
for key in commands:
try:
rule = commands[key].rule.rule
implementation = commands[key].target.code.replace("\n","\n\t\t")
except Exception:
continue
file.write("\n - **" + rule + "** `" + implementation + "`\n")
def pretty_print_context_name(file, name):
## The logic here is intended to only print from talon files that have actual voice commands.
splits = name.split(".")
index = -1
os = ""
if "mac" in name:
os = "mac"
if "win" in name:
os = "win"
if "linux" in name:
os = "linux"
if "talon" in splits[index]:
index = -2
short_name = splits[index].replace("_", " ")
else:
short_name = splits[index].replace("_", " ")
if "mac" == short_name or "win" == short_name or "linux" == short_name:
index = index - 1
short_name = splits[index].replace("_", " ")
file.write("\n\n\n" + "# " + os + " " + short_name + "\n\n")
mod = Module()
@mod.action_class
class user_actions:
def cheatsheet():
"""Print out a sheet of talon commands"""
#open file
this_dir = os.path.dirname(os.path.realpath(__file__))
file_path = os.path.join(this_dir, 'cheatsheet.md')
file = open(file_path,"w")
write_alphabet(file)
write_numbers(file)
write_modifiers(file)
write_special(file)
write_symbol(file)
write_arrow(file)
write_punctuation(file)
write_function(file)
write_formatters(file)
#print out all the commands in all of the contexts
list_of_contexts = registry.contexts.items()
for key, value in list_of_contexts:
commands= value.commands #Get all the commands from a context
if len(commands) > 0:
pretty_print_context_name(file, key)
write_context_commands(file,commands)
file.close()
print cheatsheet: user.cheatsheet()
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment