File: mode.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (47 lines) | stat: -rw-r--r-- 1,363 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Package mode implements modes, which are widgets tailored for a specific
// task.
package modes

import (
	"errors"

	"src.elv.sh/pkg/cli"
	"src.elv.sh/pkg/cli/tk"
	"src.elv.sh/pkg/ui"
)

// ErrFocusedWidgetNotCodeArea is returned when an operation requires the
// focused widget to be a code area but it is not.
var ErrFocusedWidgetNotCodeArea = errors.New("focused widget is not a code area")

// FocusedCodeArea returns a CodeArea widget if the currently focused widget is
// a CodeArea. Otherwise it returns the error ErrFocusedWidgetNotCodeArea.
func FocusedCodeArea(a cli.App) (tk.CodeArea, error) {
	if w, ok := a.FocusedWidget().(tk.CodeArea); ok {
		return w, nil
	}
	return nil, ErrFocusedWidgetNotCodeArea
}

// Returns text styled as a modeline.
func modeLine(content string, space bool) ui.Text {
	t := ui.T(content, ui.Bold, ui.FgWhite, ui.BgMagenta)
	if space {
		t = ui.Concat(t, ui.T(" "))
	}
	return t
}

func modePrompt(content string, space bool) func() ui.Text {
	p := modeLine(content, space)
	return func() ui.Text { return p }
}

// Prompt returns a callback suitable as the prompt in the codearea of a
// mode widget.
var Prompt = modePrompt

// ErrorText returns a red "error:" followed by unstyled space and err.Error().
func ErrorText(err error) ui.Text {
	return ui.Concat(ui.T("error:", ui.FgRed), ui.T(" "), ui.T(err.Error()))
}