The most common prompt for accepting user input.
- The first argument is a string or a prompt configuration object.
- The second argument is a list of choices, a string to render, or a function that returns choices or a string to render.
let value = await arg()
let name = await arg("Enter your name")
let name = await arg("Select a name", [
"John",
"Mindy",
"Joy",
])
let name = await arg("Select a name", async () => {
let response = await get("https://swapi.dev/api/people/");
return response?.data?.results.map((p) => p.name);
})
let person = await arg("Select a person", async () => {
let response = await get("https://swapi.dev/api/people/");
// return an array of objects with "name", "value", and "description" properties
return response?.data?.results.map((person) => {
return {
name: person.name,
description: person.url,
value: person
}
});
})
let char = await arg("Type then pick a char", (input) => {
// return an array of strings
return input.split("")
})
let url = "https://swapi.dev/api/people"
let name = await arg({
placeholder: "Select a name",
shortcuts: [
{
name: "Explore API",
key: "cmd+e",
onPress: async () => {
open(url)
},
bar: "right"
}
]
}, async () => {
let response = await get(url);
return response?.data?.results.map((p) => p.name);
})
div
displays HTML. Pass a string of HTML to div
to render it. div
is commonly used in conjunction with md
to render markdown.
- Just like arg, the first argument is a string or a prompt configuration object.
- Optional:The second argument is a string of tailwind class to apply to the container, e.g.,
bg-white p-4
.
await div(`Hello world!`)
await div(md(`
# Hello world!
## Thanks for coming to my demo
* This is a list
* This is another item
* This is the last item
`))
await div(`Hello world!`, `bg-white text-black text-4xl p-4`)
let name = await div(md(`# Pick a Name
* [John](submit:John)
* [Mindy](submit:Mindy)
* [Joy](submit:Joy)
`))
await div(md(`# You selected ${name}`))
dev
Opens a standalone instance of Chrome Dev Tools so you can play with JavaScript in the console. Passing in an object will set the variable x
to your object in the console making it easy to inspect.
- Optional: the first argument is an object to set to the variable
x
to in the console.
dev()
dev({
name: "John",
age: 40
})
The editor
function opens a text editor with the given text. The editor is a full-featured "Monaco" editor with syntax highlighting, find/replace, and more. The editor is a great way to edit or update text to write a file. The default language is markdown.
let content = await editor()
let content = await editor("Hello world!")
let response = await get(`https://raw.githubusercontent.com/johnlindquist/kit/main/API.md`)
let content = await editor(response.data)
The term
function opens a terminal window. The terminal is a full-featured terminal, but only intended for running commands and CLI tools that require user input. term
is not suitable for long-running processes (try exec
instead).
- Optional: the first argument is a command to run with the terminal
await term()
await term(`cd ~/.kenv/scripts && ls`)
The template
prompt will present the editor populated by your template. You can then tab through each variable in your template and edit it.
- The first argument is a string template. Add variables using $1,
$2, etc. You can also use $ {1:default value} to set a default value.
let text = await template(`Hello $1!`)
let text = await template(`
Dear \${1:name},
Please meet me at \${2:address}
Sincerely, John`)
The hotkey
prompt allows you to press modifier keys, then submits once you've pressed a non-monodifier key. For example, press command
then e
to submit key info about the command
and e
keys:
{
"key": "e",
"command": true,
"shift": false,
"option": false,
"control": false,
"fn": false,
"hyper": false,
"os": false,
"super": false,
"win": false,
"shortcut": "command e",
"keyCode": "KeyE"
}
This can be useful when you want to use a palette of commands and trigger each of them by switching on a hotkey.
- Optional: The first argument is a string to display in the prompt.
let keyInfo = await hotkey()
await editor(JSON.stringify(keyInfo, null, 2))
Use await drop()
to prompt the user to drop a file or folder.
// Note: Dropping one or more files returns an array of file information
// Dropping text or an image from the browser returns a string
let fileInfos = await drop()
let filePaths = fileInfos.map(f => f.path).join(",")
await div(md(filePaths))
The fields
prompt allows you to rapidly create a form with fields.
- An array of labels or objects with label and field properties.
let [first, last] = await fields(["First name", "Last name"])
let [name, age] = await fields([
{
name: "name",
label: "Name",
type: "text",
placeholder: "John"
},
{
name: "age",
label: "Age",
type: "number",
placeholder: "40"
}
])
Prompt the user to select a file using the Finder dialog:
let filePath = await selectFile()
Prompt the user to select a folder using the Finder dialog:
let folderPath = await selectFolder()
A widget
creates a new window using HTML. The HTML can be styled via Tailwind CSS class names.
Templating and interactivity can be added via petite-vue.
- The first argument is a string of HTML to render in the window.
- Optional: the second argument is "Browser Window Options"
await widget(`<h1 class="p-4 text-4xl">Hello World!</h1>`)
let clock = await widget(`<h1 class="text-7xl p-5 whitespace-nowrap">{{date}}</h1>`, {
transparent: true,
draggable: true,
hasShadow: false,
alwaysOnTop: true,
})
setInterval(()=> {
clock.setState({
date: new Date().toLocaleTimeString()
})
}, 1000)
let text = ""
let count = 0
let w = await widget(`
<div class="p-5">
<h1>Widget Events</h1>
<input autofocus type="text" class="border dark:bg-black"/>
<button id="myButton" class="border px-2 py-1">+</button>
<span>{{count}}</span>
</div>
`)
w.onClick((event) => {
if (event.targetId === "myButton") {
w.setState({count: count++})
}
})
w.onClose(async () => {
await widget(`
<div class="p-5">
<h1>You closed the other widget</h1>
<p>${text}</p>
</div>
`)
})
w.onInput((event) => {
text = event.value
})
w.onMoved(({ x, y}) => {
// e.g., save position
})
w.onResized(({ width, height }) => {
// e.g., save size
})
The path
prompt allows you to select a file or folder from the file system. You navigate with tab/shift+tab (or right/left arrows) and enter to select.
- Optional: The first argument is the initial directory to open with. Defaults to the home directory.
let selectedFile = await path()
These API docs are definitely incomplete and constantly evolving. If you're missing something, suggest an edit to the docs or open an issue on GitHub.
Press Enter to download the latest docs.