wsl
The community/apps/wsl
directory provides support for using Talon with the Windows Subsystem for Linux (WSL). It allows you to navigate the file system and run commands within WSL using voice commands.
The wsl.talon
file at https://github.com/talonhub/community/blob/main/apps/wsl/wsl.talon contains the Talon script that defines the voice commands and their associated actions. It begins with a crucial note about activating the user.wsl
tag in the appropriate contexts. This tag is how these voice commands are enabled when you're using WSL. It is recommended to activate the user.wsl
tag by matching on the app: windows_terminal
and the title matching ^WSL:
. An example of how to configure your .bashrc
file to achieve this is also provided.
The file also defines the following voice commands:
* go <user.letter>
: Opens a directory within the /mnt
directory using the given letter (e.g., "go c" opens /mnt/c
).
* wsl reset path detection
: Executes the user.wsl_reset_path_detection()
action, which re-enables WSL path detection.
* wsl speak
: Executes the user.wsl_speak()
action, which sends a message to each of the configured WSL distros that is printed to the log.
This script also applies the terminal
, user.file_manager
, user.generic_unix_shell
, user.git
, and user.kubectl
tags.
The wsl.py
file at https://github.com/talonhub/community/blob/main/apps/wsl/wsl.py contains the Python code that implements the actions defined in wsl.talon
. It does the following:
* Sets up a Talon context that activates when the windows_terminal
app is in focus and the user.wsl
tag is active.
* It defines a regular expression wsl_title_regex
used to parse the window title to extract the WSL distro and current path.
* Uses the Windows registry to determine which WSL distros are installed and can be interacted with.
* Provides a run_wslpath
function that translates paths between Windows and WSL, ensuring the correct path is used in file manager operations.
* Defines run_wsl
to run wsl commands, and to provide proper error handling for The Windows Subsystem for Linux instance has terminated.
* Implements a set of actions for user.file_manager
, and user.terminal
.
The most important part of this code is how it gets and translates paths. The _parse_win_title
function extracts the distro name and current path from the active window title. The run_wslpath
function then uses the wslpath
command to convert paths between Windows and WSL formats, and handles errors. The user.file_manager_current_path
action uses these functions to return the current path in a format that the user.file_manager
actions can understand.
The wsl.py
file also defines a wsl_reset_path_detection
action that can be called from the wsl.talon
file. This is a failsafe for when the WSL subsystem has failed. This reset is used in the wsl.talon
file by the wsl reset path detection
voice command.
In summary, these two files work together to enable voice control of WSL within the Windows Terminal. The wsl.talon
script provides the user-facing voice commands and tag activation context, while wsl.py
implements the underlying logic for interacting with WSL and translating paths.