ps1

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

go-ps1

Description

This package provides a Cobra command that helps your users add custom PS1 prompts for your CLI. Users can customize their own PS1 prompt using syntax from the Go template package and formatting tokens.

Example

  1. Initialize a PS1 object with a list of tokens that users can use to customize their prompt (for instance, the %C token below will display the user's login context) and add the prompt command to your Cobra CLI.
package main

import (
	"github.com/confluentinc/go-ps1"
	"github.com/spf13/cobra"
)

func main() {
	root := &cobra.Command{Use: "confluent"}
	
	ctx := context.Load()
	prompt := ps1.New(root.Name(), []ps1.Token{
		{
			Name: 'C',
			Desc: "Print the current login context.",
			Func: func() string { return ctx.Name() },
		},
	})

	root.AddCommand(prompt.NewCommand())
}
  1. Use this command to print a custom string consisting of the CLI name and the user's login context.
$ confluent prompt -f '(confluent|%C)'
(confluent|context-1)
  1. Then, append it to the $PS1 environment variable to continuously display it in the terminal prompt. (Note: ZSH users will have to run setopt prompt_subst prior to this step.)
$ export PS1='$(confluent prompt -f "(confluent|%C)") '$PS1
(confluent|context-1) $ confluent context use context-2
(confluent|context-2) $ ...

Additional Features

Colors and other styling attributes can be added to the format string. To avoid escaping special characters, consider defining this string in a separate variable.

$ format='({{fgcolor "blue" "confluent" | attr "bold"}}|{{fgcolor "red" %C}})'
$ export PS1='$(confluent prompt -f $format) '$PS1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(cliName string, tokens []Token) *ps1

New builds a PS1 object with a CLI name and custom formatting tokens.

Types

type Token

type Token struct {
	Name byte
	Desc string
	Func func() string
}

Token represents a formatting token: a single-character symbol that maps to a generic function that returns a string.

func (Token) String

func (t Token) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL