Package prompt

import "github.com/BurntSushi/wingo/prompt"
Overview
Index
Subdirectories

Overview ▾

package prompt implements several prompt windows that can be used in any X window manager. They are rather crude as they are built using X without any GUI toolkits.

See the examples directory for how to use each of the prompts.

Index ▾

Constants
Variables
type Cycle
    func NewCycle(X *xgbutil.XUtil, theme *CycleTheme, config CycleConfig) *Cycle
    func (cycle *Cycle) AddChoice(choice CycleChoice) *CycleItem
    func (cycle *Cycle) Choose()
    func (cycle *Cycle) Destroy()
    func (cycle *Cycle) GrabId() xproto.Window
    func (cycle *Cycle) Hide()
    func (cycle *Cycle) Id() xproto.Window
    func (cycle *Cycle) Next()
    func (cycle *Cycle) Prev()
    func (cycle *Cycle) Show(workarea xrect.Rect, keyStr string, items []*CycleItem) bool
type CycleChoice
type CycleConfig
type CycleItem
    func (ci *CycleItem) Destroy()
    func (ci *CycleItem) UpdateImage()
    func (ci *CycleItem) UpdateText()
type CycleTheme
type Input
    func NewInput(X *xgbutil.XUtil, theme *InputTheme, config InputConfig) *Input
    func (inp *Input) Destroy()
    func (inp *Input) Hide()
    func (inp *Input) Id() xproto.Window
    func (inp *Input) Show(workarea xrect.Rect, label string, do func(inp *Input, text string), canceled func(inp *Input)) bool
    func (inp *Input) Showing() bool
type InputConfig
type InputTheme
type Message
    func NewMessage(X *xgbutil.XUtil, theme *MessageTheme, config MessageConfig) *Message
    func (msg *Message) Destroy()
    func (msg *Message) Hide()
    func (msg *Message) Id() xproto.Window
    func (msg *Message) Show(workarea xrect.Rect, message string, duration time.Duration, hidden func(msg *Message)) bool
    func (msg *Message) Showing() bool
type MessageConfig
type MessageTheme
type Select
    func NewSelect(X *xgbutil.XUtil, theme *SelectTheme, config SelectConfig) *Select
    func (slct *Select) AddChoice(choice SelectChoice) *SelectItem
    func (slct *Select) AddGroup(group SelectGroup) *SelectGroupItem
    func (slct *Select) Destroy()
    func (slct *Select) FilterItems(search string)
    func (slct *Select) Hide()
    func (slct *Select) Id() xproto.Window
    func (slct *Select) NewStaticGroup(label string) SelectGroup
    func (slct *Select) Show(workarea xrect.Rect, tabCompleteType int, groups []*SelectShowGroup, data interface{}) bool
    func (slct *Select) Showing() bool
type SelectChoice
type SelectConfig
type SelectGroup
type SelectGroupItem
    func (si *SelectGroupItem) Destroy()
    func (si *SelectGroupItem) ShowGroup(items []*SelectItem) *SelectShowGroup
    func (si *SelectGroupItem) UpdateText()
type SelectItem
    func (si *SelectItem) Destroy()
    func (si *SelectItem) UpdateText()
type SelectShowGroup
type SelectTheme

Package files

cycle.go cycle_item.go doc.go ignore_focus.go input.go message.go select.go select_group.go select_item.go

Constants

const (
    TabCompletePrefix = iota
    TabCompleteAny
    TabCompleteMultiple
)

Variables

var DefaultCycleConfig = CycleConfig{
    Grab:       true,
    CancelKey:  "Escape",
    ConfirmKey: "Return",
}
var DefaultCycleTheme = &CycleTheme{
    BorderSize:  10,
    BgColor:     render.NewImageColor(color.RGBA{0xff, 0xff, 0xff, 0xff}),
    BorderColor: render.NewImageColor(color.RGBA{0x0, 0x0, 0x0, 0xff}),
    Padding:     10,
    Font: xgraphics.MustFont(xgraphics.ParseFont(
        bytes.NewBuffer(misc.DataFile("DejaVuSans.ttf")))),
    FontSize:         20.0,
    FontColor:        render.NewImageColor(color.RGBA{0x0, 0x0, 0x0, 0xff}),
    IconSize:         100,
    IconBorderSize:   5,
    IconTransparency: 50,
}
var DefaultInputConfig = InputConfig{
    CancelKey:    "Escape",
    BackspaceKey: "BackSpace",
    ConfirmKey:   "Return",
}
var DefaultInputTheme = &InputTheme{
    BorderSize:  5,
    BgColor:     render.NewImageColor(color.RGBA{0xff, 0xff, 0xff, 0xff}),
    BorderColor: render.NewImageColor(color.RGBA{0x0, 0x0, 0x0, 0xff}),
    Padding:     10,

    Font: xgraphics.MustFont(xgraphics.ParseFont(
        bytes.NewBuffer(misc.DataFile("DejaVuSans.ttf")))),
    FontSize:   20.0,
    FontColor:  render.NewImageColor(color.RGBA{0x0, 0x0, 0x0, 0xff}),
    InputWidth: 500,
}
var DefaultMessageConfig = MessageConfig{
    CancelKey:  "Escape",
    ConfirmKey: "Return",
}
var DefaultMessageTheme = &MessageTheme{
    BorderSize:  5,
    BgColor:     render.NewImageColor(color.RGBA{0xff, 0xff, 0xff, 0xff}),
    BorderColor: render.NewImageColor(color.RGBA{0x0, 0x0, 0x0, 0xff}),
    Padding:     10,

    Font: xgraphics.MustFont(xgraphics.ParseFont(
        bytes.NewBuffer(misc.DataFile("DejaVuSans.ttf")))),
    FontSize:  20.0,
    FontColor: render.NewImageColor(color.RGBA{0x0, 0x0, 0x0, 0xff}),
}
var DefaultSelectConfig = SelectConfig{
    CancelKey:    "Escape",
    BackspaceKey: "BackSpace",
    ConfirmKey:   "Return",
}
var DefaultSelectTheme = &SelectTheme{
    BorderSize:  10,
    BgColor:     render.NewImageColor(color.RGBA{0xff, 0xff, 0xff, 0xff}),
    BorderColor: render.NewImageColor(color.RGBA{0x0, 0x0, 0x0, 0xff}),
    Padding:     20,

    Font: xgraphics.MustFont(xgraphics.ParseFont(
        bytes.NewBuffer(misc.DataFile("DejaVuSans.ttf")))),
    FontSize:  20.0,
    FontColor: render.NewImageColor(color.RGBA{0x0, 0x0, 0x0, 0xff}),

    ActiveBgColor:   render.NewImageColor(color.RGBA{0x0, 0x0, 0x0, 0xff}),
    ActiveFontColor: render.NewImageColor(color.RGBA{0xff, 0xff, 0xff, 0xff}),

    GroupBgColor: render.NewImageColor(color.RGBA{0xff, 0xff, 0xff, 0xff}),
    GroupFont: xgraphics.MustFont(xgraphics.ParseFont(
        bytes.NewBuffer(misc.DataFile("DejaVuSans.ttf")))),
    GroupFontSize:  25.0,
    GroupFontColor: render.NewImageColor(color.RGBA{0x33, 0x66, 0xff, 0xff}),
    GroupSpacing:   15,
}

type Cycle

type Cycle struct {
    X *xgbutil.XUtil // exported for no reason
    // contains filtered or unexported fields
}

Cycle represents a single cycle prompt. A new cycle prompt can be created by:

cycle := prompt.NewCycle(XUtilValue, cycleThemeValue, cycleConfigValue)

And it can be displayed using:

shown := cycle.Show(geometry, "", cycleItemsSlice)

Where the cycle item slice is constructed from *CycleItem values that are created using the (*Cycle).AddItem method.

func NewCycle

func NewCycle(X *xgbutil.XUtil, theme *CycleTheme, config CycleConfig) *Cycle

NewCycle creates a new prompt. As many prompts as you want can be created, and they could even technically be shown simultaneously so long as at most one of them is using a grab. (The grab will fail for the others and they will not be shown.)

CycleTheme and CycleConfig values can either use DefaultCycle{Theme,Config} values found in this package, or custom ones can be created using composite literals.

func (*Cycle) AddChoice

func (cycle *Cycle) AddChoice(choice CycleChoice) *CycleItem

AddItem should be thought of as a *CycleItem constructor. Its main role is to adapt a CycleChoice value to a value that is suitable for the cycle prompt to paint onto its window. The resulting CycleItem value can be used to update the image/text.

The CycleItem value must be destroyed by calling (*CycleItem).Destroy when it is no longer used. (This frees the X window resources associated with the *CycleItem.)

func (*Cycle) Choose

func (cycle *Cycle) Choose()

Choose "selects" the currently highlighted choice.

func (*Cycle) Destroy

func (cycle *Cycle) Destroy()

func (*Cycle) GrabId

func (cycle *Cycle) GrabId() xproto.Window

GrabId returns the window id that the grab is set on. This is useful if you need to attach any Key{Press,Release} handlers.

func (*Cycle) Hide

func (cycle *Cycle) Hide()

Hide will hide the cycle prompt and reset any relevant state information. The keyboard grab will also be released if one was made.

func (*Cycle) Id

func (cycle *Cycle) Id() xproto.Window

Id returns the window id of the top-level window of the cycle prompt. I'm not sure why you might need it.

func (*Cycle) Next

func (cycle *Cycle) Next()

Next will highlight the next choice in the dialog.

func (*Cycle) Prev

func (cycle *Cycle) Prev()

Prev will highlight the previous choice in the dialog.

func (*Cycle) Show

func (cycle *Cycle) Show(workarea xrect.Rect,
    keyStr string, items []*CycleItem) bool

Show will map and show the slice of items provided.

'workarea' is the rectangle to position the prompt window in. (i.e., typically the rectangle of the monitor to place it on.)

'keyStr' is an optional parameter. If this prompt is shown in response to a keybinding, then keyStr should be the keybinding used. If there are modifiers used in the keyStr, the prompt will automatically close if all of the modifiers are released. (This is the "alt-tab" functionality.) Note that if you don't want this auto-closing feature, simply leave keyStr blank, even if the prompt is shown in response to a key binding.

Show returns false if the prompt cannot be shown for some reason.

type CycleChoice

type CycleChoice interface {
    // CycleIsActive should return whether the particular choice is "active" or
    // not. This is called every time the cycle prompt is displayed. In the
    // typical "alt-tab" example, this returns false when the window is
    // iconified (minimized). When this is false, the an "inactive" image is
    // used instead. (Which is a image with transparency equal to the
    // IconTransparency CycleTheme option.)
    CycleIsActive() bool

    // CycleImage returns the image used for the choice. (Both the active and
    // inactive images are built from this value.)
    // Note that it is okay for this method to be slow. It is only called
    // when a CycleChoice is added to the cycle prompt or when
    // (*CycleItem).UpdateImage is called. (So no image operations take place
    // when the cycle prompt is actually shown.)
    CycleImage() *xgraphics.Image

    // CycleText returns the text representing this choice. It can be empty.
    CycleText() string

    // CycleSelected is a hook that is called when this choice is chosen in the
    // cycle prompt.
    CycleSelected()

    // CycleHighlighted is a hook that is called when this choice is highlighted
    // in the cycle prompt.
    CycleHighlighted()
}

CycleChoice is any value capable of being shown in a prompt cycle.

type CycleConfig

type CycleConfig struct {
    Grab       bool
    CancelKey  string
    ConfirmKey string
}

CycleConfig values can be used to create prompts with different configurations. As of right now, the only configuration options supported is whether to issue a keyboard grab and the key to use to "cancel" the prompt. (If empty, no cancel key feature will be used automatically.) For a reasonable default configuration, use DefaultCycleConfig. It will set "Escape" as the cancel key and issue a grab.

type CycleItem

type CycleItem struct {
    // contains filtered or unexported fields
}

CycleItem is a representation of a CycleChoice that is amenable to being displayed in a cycle prompt. A CycleItem value is created and returned to the caller whenever (*Cycle).AddItem is called.

CycleItem values are used as a parameter to Show to dictate which choices are displayed in a viewing of a prompt.

Also, the Image and Text corresponding to this item can be updated using UpdateImage and UpdateText.

Finally, when a CycleChoice (and by extension, a CycleItem) is no longer in use, (*CycleItem).Destroy should be called. (This will destroy all X windows associated with the CycleItem.) Forgetting to call Destroy will result in X resources (window identifiers) not being freed until your connection is closed.

func (*CycleItem) Destroy

func (ci *CycleItem) Destroy()

Destroy destroys all windows associated with the CycleItem. This is necessary to free X resources and should be called whenever the CycleItem will no longer be used.

func (*CycleItem) UpdateImage

func (ci *CycleItem) UpdateImage()

UpdateImage will repaint the active and inactive images by calling CycleChoice.CycleImage. This is not called when the cycle prompt is shown; rather the burden is on the user to make sure the prompt has the most up to date image.

func (*CycleItem) UpdateText

func (ci *CycleItem) UpdateText()

UpdateText repaints the text to an image associated with a particular CycleChoice. The text is retrieved by calling CycleChoice.CycleText.

type CycleTheme

type CycleTheme struct {
    BorderSize  int
    BgColor     render.Color
    BorderColor render.Color
    Padding     int

    Font      *truetype.Font
    FontSize  float64
    FontColor render.Color

    IconSize         int
    IconBorderSize   int
    IconTransparency int
}

CycleTheme values can be used to create prompts with different colors, padding, border sizes, icon sizes, fonts, etc. You may use DefaultCycleTheme for a reasonable default theme if you don't care about the particulars.

type Input

type Input struct {
    X *xgbutil.XUtil
    // contains filtered or unexported fields
}

func NewInput

func NewInput(X *xgbutil.XUtil, theme *InputTheme, config InputConfig) *Input

func (*Input) Destroy

func (inp *Input) Destroy()

func (*Input) Hide

func (inp *Input) Hide()

func (*Input) Id

func (inp *Input) Id() xproto.Window

func (*Input) Show

func (inp *Input) Show(workarea xrect.Rect, label string,
    do func(inp *Input, text string), canceled func(inp *Input)) bool

func (*Input) Showing

func (inp *Input) Showing() bool

type InputConfig

type InputConfig struct {
    CancelKey    string
    BackspaceKey string
    ConfirmKey   string
}

type InputTheme

type InputTheme struct {
    BorderSize  int
    BgColor     render.Color
    BorderColor render.Color
    Padding     int

    Font      *truetype.Font
    FontSize  float64
    FontColor render.Color

    InputWidth int
}

type Message

type Message struct {
    X *xgbutil.XUtil
    // contains filtered or unexported fields
}

func NewMessage

func NewMessage(X *xgbutil.XUtil,
    theme *MessageTheme, config MessageConfig) *Message

func (*Message) Destroy

func (msg *Message) Destroy()

func (*Message) Hide

func (msg *Message) Hide()

func (*Message) Id

func (msg *Message) Id() xproto.Window

func (*Message) Show

func (msg *Message) Show(workarea xrect.Rect, message string,
    duration time.Duration, hidden func(msg *Message)) bool

func (*Message) Showing

func (msg *Message) Showing() bool

type MessageConfig

type MessageConfig struct {
    CancelKey  string
    ConfirmKey string
}

type MessageTheme

type MessageTheme struct {
    BorderSize  int
    BgColor     render.Color
    BorderColor render.Color
    Padding     int

    Font      *truetype.Font
    FontSize  float64
    FontColor render.Color
}

type Select

type Select struct {
    X *xgbutil.XUtil
    // contains filtered or unexported fields
}

func NewSelect

func NewSelect(X *xgbutil.XUtil,
    theme *SelectTheme, config SelectConfig) *Select

func (*Select) AddChoice

func (slct *Select) AddChoice(choice SelectChoice) *SelectItem

func (*Select) AddGroup

func (slct *Select) AddGroup(group SelectGroup) *SelectGroupItem

func (*Select) Destroy

func (slct *Select) Destroy()

func (*Select) FilterItems

func (slct *Select) FilterItems(search string)

func (*Select) Hide

func (slct *Select) Hide()

func (*Select) Id

func (slct *Select) Id() xproto.Window

func (*Select) NewStaticGroup

func (slct *Select) NewStaticGroup(label string) SelectGroup

NewStaticGroup returns a value implementing the SelectGroup interface with the label provided. This is useful for generating group labels that never change. (i.e., in Wingo, these would be the no-label, Visible and Hidden groups. While the groups defined by workspace have to implement the SelectGroup interface themselves.)

func (*Select) Show

func (slct *Select) Show(workarea xrect.Rect, tabCompleteType int,
    groups []*SelectShowGroup, data interface{}) bool

func (*Select) Showing

func (slct *Select) Showing() bool

type SelectChoice

type SelectChoice interface {
    SelectText() string
    SelectSelected(data interface{})
    SelectHighlighted(data interface{})
}

type SelectConfig

type SelectConfig struct {
    CancelKey    string
    BackspaceKey string
    ConfirmKey   string
    TabKey       string
}

type SelectGroup

type SelectGroup interface {
    SelectGroupText() string
}

type SelectGroupItem

type SelectGroupItem struct {
    // contains filtered or unexported fields
}

func (*SelectGroupItem) Destroy

func (si *SelectGroupItem) Destroy()

func (*SelectGroupItem) ShowGroup

func (si *SelectGroupItem) ShowGroup(items []*SelectItem) *SelectShowGroup

func (*SelectGroupItem) UpdateText

func (si *SelectGroupItem) UpdateText()

type SelectItem

type SelectItem struct {
    // contains filtered or unexported fields
}

func (*SelectItem) Destroy

func (si *SelectItem) Destroy()

func (*SelectItem) UpdateText

func (si *SelectItem) UpdateText()

type SelectShowGroup

type SelectShowGroup struct {
    *SelectGroupItem
    // contains filtered or unexported fields
}

type SelectTheme

type SelectTheme struct {
    BorderSize  int
    BgColor     render.Color
    BorderColor render.Color
    Padding     int

    Font      *truetype.Font
    FontSize  float64
    FontColor render.Color

    ActiveBgColor   render.Color
    ActiveFontColor render.Color

    GroupBgColor   render.Color
    GroupFont      *truetype.Font
    GroupFontSize  float64
    GroupFontColor render.Color
    GroupSpacing   int
}

Subdirectories

Name      Synopsis
..
examples     
     cycle      Example cycle shows how to use the cycle prompt.
     input      Example input shows how to use an Input prompt from the prompt pacakge.
     message      Example message shows how to use a Message prompt from the prompt pacakge.
     select      Example select shows how to use a Select prompt from the prompt pacakge.