src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/cli/modes/mode.go (about)

     1  // Package mode implements modes, which are widgets tailored for a specific
     2  // task.
     3  package modes
     4  
     5  import (
     6  	"errors"
     7  
     8  	"src.elv.sh/pkg/cli"
     9  	"src.elv.sh/pkg/cli/tk"
    10  	"src.elv.sh/pkg/ui"
    11  )
    12  
    13  // ErrFocusedWidgetNotCodeArea is returned when an operation requires the
    14  // focused widget to be a code area but it is not.
    15  var ErrFocusedWidgetNotCodeArea = errors.New("focused widget is not a code area")
    16  
    17  // FocusedCodeArea returns a CodeArea widget if the currently focused widget is
    18  // a CodeArea. Otherwise it returns the error ErrFocusedWidgetNotCodeArea.
    19  func FocusedCodeArea(a cli.App) (tk.CodeArea, error) {
    20  	if w, ok := a.FocusedWidget().(tk.CodeArea); ok {
    21  		return w, nil
    22  	}
    23  	return nil, ErrFocusedWidgetNotCodeArea
    24  }
    25  
    26  // Returns text styled as a modeline.
    27  func modeLine(content string, space bool) ui.Text {
    28  	t := ui.T(content, ui.Bold, ui.FgWhite, ui.BgMagenta)
    29  	if space {
    30  		t = ui.Concat(t, ui.T(" "))
    31  	}
    32  	return t
    33  }
    34  
    35  func modePrompt(content string, space bool) func() ui.Text {
    36  	p := modeLine(content, space)
    37  	return func() ui.Text { return p }
    38  }
    39  
    40  // Prompt returns a callback suitable as the prompt in the codearea of a
    41  // mode widget.
    42  var Prompt = modePrompt
    43  
    44  // ErrorText returns a red "error:" followed by unstyled space and err.Error().
    45  func ErrorText(err error) ui.Text {
    46  	return ui.Concat(ui.T("error:", ui.FgRed), ui.T(" "), ui.T(err.Error()))
    47  }