2title = "Command line interface apps"
7# Command line interface apps
11In this walkthrough we will go in-depth on how to build command line interface (CLI)
12applications in Urbit using the `shoe` library.
14There are three CLI apps that currently ship with urbit - `%dojo`, `%chat-cli`,
15and `%shoe`. You should be familiar with the former two, the latter is an example
16app that shows off how the `shoe` library works that we will be looking at
17closely. These are all Gall apps, and as such their source can be found in the `app/` folder of your `%home` desk.
19In [the `shoe` library](#the-shoe-library) we take a closer look at the `shoe` library and its
20cores and how they are utilized in CLI apps. Then in [the `sole`
21library](#the-sole-library) we look at what `shoe` effects ultimately break down
22into. Finally in [`%shoe` app walkthrough](#shoe-app-walkthrough) we explore
23the functionality of the `%shoe` app and then go through the code line-by-line.
26considered to be an application equivalent of the [Hoon school
27lesson](@/docs/tutorials/hoon/generators.md#ask) on `sole` and `%ask`
28generators, which only covers the bare minimum necessary to write generators
32## The `shoe` library {#the-shoe-library}
34Here we describe how sessions are identified, the specialized `card`s that Gall agents
35with the `shoe` library are able to utilize, and the different cores of `/lib/shoe.hoon` and their purpose.
37### Session identifiers
39An app using the `shoe` library will automatically track sessions by their `sole-id=@ta`.
40These are opaque identifiers generated by the connecting client. An app using the `shoe`
41library may be connected to by a local or remote ship in order to send commands,
42and each of these connections is assigned a unique `@ta` that identifies the
43ship and which session on that ship if there are multiple.
47Gall agents with the `shoe` library are able to utilize `%shoe` `card`s. These
48additions to the standard set of `cards` have the following shape:
50[%shoe sole-ids=(list @ta) effect=shoe-effect]`
52`sole-ids` is the `list` of session ids that the following `effect` is
53emitted to. An empty `sole-ids` sends the effect to all connected sessions.
54`shoe-effect`s, for now, are always of the shape `[%sole effect=sole-effect]`, where
55`sole-effect`s are basic console events such as displaying text, changing the
56prompt, beeping, etc. These are described in the section on the [`sole` library](#the-sole-library).
58For example, a `%shoe` `card` that causes all connected sessions to beep would
59be `[%shoe ~ %sole %bel ~]`.
63An iron (contravariant) door that defines an interface for Gall agents utilizing
64the `shoe` library. Use this core whenever you want to receive input from the user and run a command. The input will get
65put through the parser (`+command-parser`) and results in a noun of
66`command-type` that the underlying application specifies, which shoe then feeds back into the underlying app as an `+on-command` callback.
68In addition to the ten arms that all Gall core apps possess, `+shoe` defines and expects a few
69more, tailored to common CLI logic. Thus you will need to wrap the `shoe:shoe` core using the `agent:shoe` function to obtain a standard
7010-arm Gall agent core. See the [shoe example app
71walkthrough](#shoe-example-app-walkthrough) for how to do this.
73The additional arms are described below. The Hoon code shows their expected type signature. As we'll see [later](#shoe-app-walkthrough), the `command-type` can differ per application. Note also that most of these take a session identifier as an argument. This lets applications provide different users (at potentially different "places" within the application) with different affordances.
80 |~(nail *(like [? command-type]))
83Input parser for a specific command-line session. Will be run on whatever the user tries to input into the command prompt, and won't let them type anything that doesn't parse. If the head of the result is true,
84instantly run the command. If it's false, require the user to press return.
91 *(list (option:auto tank))
94Autocomplete options for the command-line session (to match `+command-parser`).
100 |~ [sole-id=@ta command=command-type]
104Called when a valid command is run.
114Called to determine whether a session may be opened or connected to. For example, you
115may only want the local ship to be able to connect.
122 *(quip card _^|(..on-init))
125Called when a session is opened or connected to.
132 *(quip card _^|(..on-init))
135Called when a previously made session gets disconnected from.
139This core contains the bare minimum implementation of the additional `shoe` arms
140beyond the 10 standard Gall app ams. It is used
141analogously to how the `default-agent` core is used for regular Gall apps.
145This is a function for wrapping a `shoe` core, which has too many
146arms to be a valid Gall agent core. This turns it into a standard Gall agent core by
147integrating the additional arms into the standard ones.
150## The `sole` library {#the-sole-library}
152`shoe` apps may create specialized `card`s of the `[%shoe
153(list @ta) shoe-effect]` shape, where `shoe-effect` currently just wrap `sole-effect`s, i.e. instructions for displaying text and producing other effects in the console.
155The list of possible `sole-effects` can be found in `/sur/sole.hoon`. A few
156commonly used ones are as follows.
157- `[%txt tape]` is used to display a line of text.
158- `[%bel ~]` is used to emit a beep.
159- `[%pro sole-prompt]` is used to set the prompt.
160- `[%mor (list sole-effect)]` is used to emit multiple effects.
162For example, a `sole-effect` that beeps and displays `This is some text.` would
165[%mor [%txt "This is some text."] [%bel ~] ~]
168## `%shoe` app walkthrough {#shoe-app-walkthrough}
170Here we explore the capabilities of the `%shoe` example app and then go through
171the code, explaining what each line does.
173### Playing with `%shoe`
175First let's test the functionality of `%shoe` so we know what we're getting
178Start two fake ships, one named `~zod` and the other can have any name - we will
179go with `~nus`. Fake ships run locally are able to see each other, and our
180intention is to connect their `%shoe` apps.
182On each fake ship start `%shoe` by entering `|start %shoe` into dojo. This will
184change the prompt to `~zod:shoe>` and `~nus:shoe>`. Type `demo` and watch the following appear:
189`~zod ran the command` should be displayed in bold green text, signifying that
190the command originated locally.
192Now we will connect the sessions. Switch `~zod` back to dojo with `Ctrl-X` and enter `|link
193~nus %shoe`. If this succeeds you will see the following.
196; ~nus is your neighbor
197[linked to [p=~nus q=%shoe]]
199Now `~zod` will have two `%shoe` sessions running - one local one on `~zod` and
200one remote one on `~nus`, which you can access by pressing `Ctrl-X` until you see
201`~nus:shoe>` from `~zod`'s console. On the other hand, you should not see
202`~zod:shoe>` on `~nus`'s side, since you have not connected `~nus` to `~zod`'s
203`%shoe` app. When you enter `demo` from `~nus:shoe>` on
204`~zod`'s console you will again see `~zod ran the command`, but this time it
205should be in the ordinary font used by the console, signifying that the command
206is originating from a remote session. Contrast this with entering `demo` from
207`~nus:shoe>` in `~nus`'s console, which will display `~nus ran the command` in
210Now try to link to `~zod`'s `%shoe` session from `~nus` by switching to the dojo
211on `~nus` and entering `|link ~zod %shoe`. You should see
214[unlinked from [p=~zod q=%shoe]]
216and if you press `Ctrl-X` you will not get a `~zod:shoe>` prompt. This is
217because the example app is set up to always allow `~zod` to connect (as well as
218subject moons if the ship happens to be a planet) but not `~nus`, so this
219message means that `~nus` failed to connect to `~zod`'s `%shoe` session.
224:: shoe: example usage of /lib/shoe
226:: the app supports one command: "demo".
227:: running this command renders some text on all sole clients.
229/+ shoe, verb, dbug, default-agent
232`/+` is the Ford rune which imports libraries from the `/lib` directory into
234 * `shoe` is the `shoe` library.
235 * `verb` is a library used to print what a Gall agent is doing.
236 * `dbug` is a library of debugging tools.
237 * `default-agent` contains a Gall agent core with minimal implementations of
248The types used by the app.
250`state-0` stores the state of the app, which is null as there is no state to
251keep track of. It is good practice to include a version number anyways,
252in case the app is made stateful at a later time.
254`command` is typically a set of tagged union types that represent the possible
255commands that can be entered by the user. Since this app only supports one
256command, it is unnecessary for it to have any associated data, thus the command
257is represented by `~`.
259In a non-trivial context, a `command` is commonly given by `[%name
260data]`, where `%name` is the identifier for the type of command and `data` is
261a type or list of types that contain data needed to execute the command. See
262`app/chat-cli.hoon` for examples of commands, such as `[%say letter:store]` and
263`[%delete path]`. This is not required though, and you could use something like
264`[chat-room=@t =action]`.
266`card` is either an ordinary Gall agent `card` or a `%shoe` `card`, which takes
267the shape `[%shoe sole-ids=(list @ta) effect=shoe-effect]`. A `%shoe` `card` is
268sent to all `sole`s listed in `sole-ids`, imaking them run the `sole-effect`
269specified by `effect` (i.e. printing some text). Here we can
270reference `card:shoe` because of `/+ shoe` at the beginning of the app.
277Add the bunt value of `state-0` to the head of the subject, then give it the
278macro `state`. The `-` here is a lark expression referring to the head of the
279subject. This allows us to use `state` to refer to the state elsewhere in the
280code no matter what version we're using, while also getting direct access to the
281contents of `state` (if it had any).
287%- (agent:shoe command)
288^- (shoe:shoe command)
291The casts here are just reminders of what is being produced. So let's focus on
292what the `%` runes are doing, from bottom to top. We call `(agent:shoe command)`
293on what follows (i.e. the rest of the app),
294producing a standard Gall agent core. Then we call wrap the Gall agent core with
295`agent:dbug`, endowing it with additional arms useful for debugging, and then
296wrap again with `verb`.
302 def ~(. (default-agent this %|) bowl)
303 des ~(. (default:shoe this command) bowl)
305This is boilerplate Gall agent core code. We set `this` to be a macro for the
306subject, which is the Gall agent core itself. We set `def` and `des` to be
307macros for initialized `default-agent` and `default:shoe` doors respectively.
309Next we implement all of the arms required for a `shoe` agent. Starting with
310the standard Gall arms:
313++ on-init on-init:def
320++ on-poke on-poke:def
321++ on-watch on-watch:def
322++ on-leave on-leave:def
323++ on-peek on-peek:def
324++ on-agent on-agent:def
325++ on-arvo on-arvo:def
326++ on-fail on-fail:def
328These are minimalist Gall app arm implementations using the default behavior found in `def`.
330Here begins the implementation of the additional arms required by the
331`(shoe:shoe command)` interface.
335 ^+ |~(nail *(like [? command]))
336 (cold [& ~] (jest 'demo'))
338`+command-parser` is of central importance - it is what is used to parse user
339input and transform it into `command`s for the app to execute. Writing a proper
340command parser requires understanding of the Hoon parsing functions found in the
341standard library. How to do so may be found in the parsing documentation (coming
342soon). For now, it is sufficient to know that this arm matches the text "demo" and
343produces a `[? command]`-shaped noun in response. Note how the `&` signifies that the command will be run as soon as it has been entered, without waiting for the user to press return.
350 :~ ['demo' leaf+"run example command"]
353`+tab-list` is pretty much plug-n-play. For each command you want to be tab
354completed, add an entry to the `list` begun by `:~` of the form `[%command
355leaf+"description"]`. Now whenever the user types a partial command and presses
356tab, the console will display the list of commmands that match the partial
357command as well as the descriptions given here.
359Thus here we have that starting to type `demo` and pressing tab will result in
360the following output in the console:
362demo run example command
365with the remainder of `demo` now added to the input line.
367Next we have `+on-command`, which is called whenever `+command-parser`
368recognizes that `demo` has been entered by a user.
371 |= [sole-id=@ta =command]
374This is a gate that takes in the `sole-id` corresponding to the session and the
375`command` noun parsed by `+command-parser` and returns a `list` of `card`s and
376`_this`, which is our shoe agent core including its state.
378 =- [[%shoe ~ %sole -]~ this]
380This creates a cell of a `%shoe` card that triggers a `sole-effect` given by the head of
381the subject `-`, then the Gall agent core `this` - i.e. the return result of
382this gate. The use of the `=-` rune means that what follows this
383expression is actually run first, which puts the desired `sole-effect` into the
386 =/ =tape "{(scow %p src.bowl)} ran the command"
388We define the `tape` that we want to be printed.
392 [%klr [[`%br ~ `%g] [(crip tape)]~]~]
394We cannot just produce the `tape` we want printed, - it needs to fit the
395`sole-effect` type. This tells us that if the
396origin of the command is not our ship to just print it normally with the `%txt`
397`sole-effect`. Otherwise we use `%klr`, which prints it stylistically (here it
398makes the text green and bold).
400The following allows either `~zod`, or the host ship and its moons, to connect to
401this app's command line interface using `|link`.
407 (team:title [our src]:bowl)
411We use the minimal implementations for the final two `shoe` arms, since we don't
412want to do anything special when users connect or disconnect.
414++ on-connect on-connect:des
415++ on-disconnect on-disconnect:des
419This concludes our review of the code of the `%shoe` app. To continue learning
420how to build your own CLI app, we recommend checking out `/app/chat-cli.hoon`.
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list