8.3 KiB
Task
VNote supports a simple task system like VSCode Tasks, which enables executing third-party programs easily.
Loading Tasks
VNote will try to load tasks from three locations:
default_config_folder/tasks
for built-in tasksuser_config_folder/tasks
for user-defined tasksnotebook_config_folder/tasks
for tasks defined by this notebook
A task is defined by a *.json
entry file.
A Simple Task
Click the Add Task
item on the task menu, which will open the user-defined tasks folder.
New a folder named hello
and under it create a file named hello.json
. Edit the JSON file as:
{
"command": "echo 'Hello Tasks'"
}
Reload tasks in the menu and we could see that a new task named hello
is listed on the menu. Click it to run the task.
Customize Menu Item
{
"label": "Hello",
"icon": "tasks-solid.svg",
"shortcut": "Alt+H, T",
"command": "echo",
"args": [
"Hello tasks!"
]
}
The icon file tasks-solid.svg
should be saved alongside the JSON entry file.
Sub-Tasks
Tasks could be embedded infinitely. Sub-tasks will inherit most properties from parent.
{
"label": "Hello Tasks",
"icon": "tasks-solid.svg",
"shortcut": "Alt+H, T",
"command": "echo",
"args": ["Hello tasks!"],
"tasks": [
{
"label": "Hello Cat",
"icon": "cat-solid.svg",
"shortcut": "Alt+H, C",
"args": ["Hello cat!"]
},
{
"label": "Hello Dove",
"icon": "dove-solid.svg",
"shortcut": "Alt+H, D",
"args": ["Hello dove!"]
},
{
"label": "Hello Fish",
"icon": "fish-solid.svg",
"shortcut": "Alt+H, F",
"args": ["Hello fish!"]
}
]
}
Command Types
The type
property of one task defines how the command will be executed.
shell
: Default, will run the command as a shell commandprocess
: Will run the command as a standalone program
{
"type": "process",
"label": "Open File with",
"args": ["${buffer}"],
"tasks": [
{
"label": "Typora",
"icon": "Typora.svg",
"command": "C:\\Programs\\Typora0.9.98\\x64\\Typora.exe"
},
{
"label": "VS Code",
"icon": "vscode.svg",
"command": "C:\\Users\\tootal\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
}
]
}
::: alert-info
Yep, tootal
is the contributor who initiated the Task system of VNote!
:::
VNote does not provide a terminal. We may need to use start
or gnome-terminal
or konsole
to run some programs in terminal.
{
"label": "Vim",
"icon": "vim.svg",
"type": "process",
"command": "gnome-terminal",
"args": [
"--execute",
"vim",
"${buffer}"
]
}
Localization and Platform-Specific Options
Provide a locale string JSON object to provide localization.
{
"label": {
"en_US": "Hello",
"zh_CN": "你好"
}
}
We could use windows
/linux
/osx
keyword to specify options for different platforms.
{
"type": "process",
"label": "Open File with",
"args": ["${buffer}"],
"tasks": [
{
"label": "Typora",
"icon": "Typora.svg",
"windows": {
"command": "C:\\Programs\\Typora0.9.98\\x64\\Typora.exe"
},
"linux": {
"command": "/usr/bin/typora"
}
},
{
"label": "VS Code",
"icon": "vscode.svg",
"windows": {
"command": "C:\\Users\\tootal\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
},
"linux": {
"command": "/usr/bin/code"
}
}
]
}
Task Options
A task could have several options, some of them are mandatory.
We will use [m]
to mark the mandatory options, [l]
to mark the options supporting localization.
version
: the version of the task filelabel[l]
: the name of the tasktype
: the type of task;shell
(default)/process
command[l]
: the command to executeargs[l]
: the arguments passed to the commandoptions
: options for runnig taskcwd
: current working directory to run task; will try current notebook root folder, then current buffer folder, and then current task file folder in order if missingenv
: environment variables for running taskshell
: options for tasks ofshell
typeexecutable
: the shell executable file;Powershell.exe
by default on Windows and/bin/bash
by default on Linux/macOSargs
: the arguments to start shell
tasks
: define sub-tasksinputs
: define input variablesid[m]
: ID of the input variabletype
:promptString
(default, prompt for user input) orpickString
(prompt for user selection)description[l]
: description of the input variabledefault[l]
: default valuepassword
: whether use password mode forpromptString
typeoptions[l]
: options defined forpickString
type
windows
: options for Windows systemlinux
: options for Linux systemosx
: options for macOS system
Variables
A task could use variables provided by VNote in the form ${variableName}
. Variables could provide useful information to the task when running.
Variables could be used in options command
/args
/options.cwd
/options.env
.
Built-In Variables
Notebook-related variables:
notebookFolder
: path of the notebook root foldernotebookFolderName
notebookName
notebookDescription
Buffer-related variables:
buffer
: path of current bufferbufferNotebookFolder
: path of the notebook root folder of the notebook of current bufferbufferRelativePath
bufferName
bufferBaseName
bufferDir
: folder of current bufferbufferExt
: extension suffix of current bufferselectedText
: selected text of current buffer view window
Task-related variables:
cwd
: current working directorytaskFile
: the path of the task entry filetaskDir
: the path of the directory containing the task entry fileexeFile
: the path of VNote executable filepathSeparator
: the platform-dependent path separatornotebookTaskFolder
: the path of current notebook task folderuserTaskFolder
: the path of user task folderappTaskFolder
: the path of default task folderuserThemeFolder
: the path of user theme folderappThemeFolder
: the path of default theme folderuserDocsFolder
: the path of user docs folderappDocsFolder
: the path of default docs folder
Other specifal variables:
- Call snippets of VNote via
${magic:snippet_name}
- Access environment variable via
${env:env_name}
- Access VNote's configurations via
${config:[main|session].json_object_path}
main
for the main configurations fromvnotex.json
andsession
for the session configurations fromsession.json
- Use
arr[index]
to access a JSON array ${config:main.core.shortcuts.FullScreen}
will get the shortcut ofFullScreen
Input Variables
A task could use input variables to prompt for user inputs via ${input:input_id}
.
There are now two types of input variables:
promptString
pickString
{
"command": "echo",
"args": ["${input:what}"],
"inputs": [
{
"id": "what",
"type": "promptString",
"description": "Type something, it will show in output panel."
}
]
}
Shell Variables
A task could execute a shell command and get its output via shell variables like ${shell:shell_command}
.
${shell:git rev-parse --abbrev-ref HEAD}
→master
${shell:whoami}
→tootal
${shell:dig github.com -4 +short}
→52.69.186.44
Examples
There is a built-in task named Git
which locates in the default configuration task folder.
Compile and run:
{
"command": "g++ \"${buffer}\" -o \"${bufferBaseName}\"; if ($?) { start cmd \"/c `\"${bufferBaseName}`\" & pause\" }"
}
Run a HTTP server:
{
"command": "start cmd.exe \"/c python -m http.server\" ; start http://localhost:8000"
}