• Beta
Golang UX
  • 18 Nov 2020
  • 2 Minutes To Read
  • Share
  • Dark
    Light

Golang UX

  • Share
  • Dark
    Light

Supported methods

The following UX elements are supported by the Golang SDK:

  • Print
  • ProgressBars
  • Spinners

To create a UX element in your Op, you need to create a new UX object.

func NewUx() *Ux

Alternatively, the Ux object is included in the Client object:

client := NewClient()
client.Ux.Print(...)

func (*Ux) Bold

func (*Ux) Bold(text string) string

Formats text as boldface for printing with Print or as a message in any of the other Ux or Prompt methods.

Formats correctly in the terminal and in Slack.

Example:

client := ctoai.NewClient()
permission, err := client.Prompt.Input(
    "permission",
    fmt.Sprintf(
        "If you are certain you want to %s this repository, enter its name", 
        client.Ux.Bold("permanently delete"),
   ),
)

func (*Ux) Italic

func (*Ux) Italic(text string) string

Formats text as italic for printing with Print or as a message in any of the other Ux or Prompt methods.

Formats correctly in the terminal and in Slack, but terminal support for italic text is limited.

Example:

ux := ctoai.NewUx()
err := ux.Print(
    fmt.Sprintf("We hope you have a %s good experience with CTO.ai", ux.Italic("very")),
)

func (*Ux) Print

func (*Ux) Print(text string) error

Prints text to the output interface (e.g. terminal or Slack).

Example:

u := ctoai.NewUx()
err := u.Print("testing")
if err != nil {
    panic(err)
}

Output:

testing

func (*Ux) ProgressBarAdvance

func (*Ux) ProgressBarAdvance(increment int) error

ProgressBarAdvance adds onto a progressbar that is already present on the interface (e.g. terminal or Slack).

The increment indicates the additional length (out of total length) that will be filled.

Example:

...
[progressbar animation with 1/5 of the bar filled here] Downloading...
err := u.ProgressBarAdvance(1)
if err != nil {
    panic(err)
}

Output:

[progressbar animation with 2/5 of the bar filled here] Downloading...

func (*Ux) ProgressBarStart

func (*Ux) ProgressBarStart(length, initial int, message string) error

ProgressBarStart presents a progressbar on the output interface (i.e. terminal or slack) that will stay present until the progressbar stop method is called.

The input length is the total length of the progress bar, e.g. if you have 5 steps in your logic, then a unit length of 5 might be an appropriate length.

The initial length indicates the unit length (out of total length) that is initially filled at the start.

Example:

u := ctoai.NewUx()
err := u.ProgressBarStart(5, 1, "Downloading...")
if err != nil {
    panic(err)
}

Output:

[progressbar animation with 1/5 of the bar filled here] Downloading...

func (*Ux) ProgressBarStop

func (*Ux) ProgressBarStop(message string) error

ProgressBarStop completes a progressbar that is already present on the interface (i.e. terminal or Slack).

The message will change the initial text (set from the ux.ProgressBarStart method).

Example:

...
[progressbar animation with 2/5 of the bar filled here] Downloading...
err := u.ProgressBarStop("Done!")
if err != nil {
    panic(err)
}

Output:

[progressbar animation with 5/5 of the bar filled here] Done!

func (*Ux) SpinnerStart

func (*Ux) SpinnerStart(text string) error

SpinnerStart presents a spinner on the output interface (e.g. terminal or Slack) that will spin until the SpinnerStop method is called.

Example:

u := ctoai.NewUx()
err := u.SpinnerStart("Starting process...")
if err != nil {
    panic(err)
}

Output:

[spinner emoji w/ spinner animation here] Starting process...

func (*Ux) SpinnerStop

func (*Ux) SpinnerStop(text string) error

SpinnerStop stops a spinner that has been previously started on the interface (e.g. terminal or Slack).

Example:

... //previous spinner started here
err := u.SpinnerStop("Done!")
if err != nil {
    panic(err)
}

Output:

[spinner completed completed here] Done!

🚀 What's next?

  • If you want to explore the options offered by our SDKs, go to the Golang SDK page.
  • To see which prompts we offer for this SDK, you can go to Golang Prompts page.
Was This Article Helpful?