howl.ui.CommandPanel

Overview

A CommandPanel object is attached to every Window and manages the command line that appears at the bottom of the window to handle user input. The CommandPanel for the primary window is available at howl.app.window.command_panel.

The recommended way to invoke and control the command panel is by using one of the built-in interactions. The command panel API can also be used directly for finer control of the command line. This API is described in this page.

Example

Here is an example showing use of the command panel to display a prompt and read user entered text:

TextReader = {}
function TextReader:init(command_line)
  self.command_line = command_line
  command_line.prompt = 'Name:'
  command_line.title = 'Please enter your name'
end

TextReader.keymap = {}
function TextReader.keymap:enter()
  self.command_line:finish(self.command_line.text)
end

function TextReader.keymap:escape()
  self.command_line:finish()
end

howl.app.window.command_panel:run(TextReader)

The same example rewritten in Moonscript is below:

class TextReader
  new: (opts={}) =>
    @opts = moon.copy opts

  init: (@command_line) =>
    @command_line.prompt = @opts.prompt
    @command_line.title = @opts.title

  keymap:
    enter: =>
      @command_line\finish @command_line.text

    escape: => @command_line\finish!

howl.app.window.command_panel\run TextReader(prompt: 'Name:', title: 'Please enter your name')

The command_panel.run method is passed a table containing a command line definition. The definition is executed by the command panel. In this example, the following steps occur after run is called:

  1. The command panel creates a CommandLine object.

  2. A command line box is displayed on the bottom of the window.

  3. The init function of the definition is invoked and passed the CommandLine object. Note that above the init function sets the prompt and title fields on the CommandLine object. These are part of the command line API and setting these fields immediately updates the displayed prompt and title. The text field is used to read or set the editable text in the command line.

  4. After init returns, the command line enters an active state and handles events based on user input. For instance, when the user presses enter, the keymap.enter function on the definition is invoked. The command line stays active until the command_line.finish function is called. Note that above this is called in both enter and escape keypress handlers.

  5. When the user presses enter or escape, the command_line.finish method is called from the keymap handler. Then the command line is closed and the argument passes to the command_line is returned as the result of command_panel.run.

The above example shows a simple end-to-end execution of the command line. Note that the API consists of two parts - the CommandPanel object and the CommandLine object.


See also:

CommandPanel

The CommandPanel only has one method called run.

run(def, opts)

Creates a CommandLine, displayes the command line, and invokes the def command line definition. def is table that provides the following fields:

The opts table may provide the following fields:

CommandLine

A command line widget contains many parts:

This composite widget is controlled using the following properties and methods:

Properties (CommandLine)

text

Gets or sets the editable text of the command line widget.

prompt

Gets or sets the prompt shown in the command line. The prompt is some text displayed to the left of the editable part of the widget.

title

Gets of sets the title shown just at the top of the widget.

notification

A NotificationWidget attached to the command line. To display a message call:

command_line.notification\info 'hello'

Methods (CommandLine)

clear()

Clears the command line editable text.

write(text)

Writes text into the editable text.

finish(result)

Closes the command line and returns the original CommandPanel.run function call. The result passed into finish is returned as the result of run.

add_widget (name, widget)

Adds a custom widget widget in the command line view. name is the name used to identify the widget. Currently two types of widgets are available - widgets must be an instance of either ListWidget or NotificationWidget.

remove_widget (name)

Removes the widget identified by name from the command line view.