stylishcobra

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 5 Imported by: 1

README

ColoredCobra just with lipgloss styles

This library is Ivan Pirog's ColoredCobra just modified to work with lipgloss. All the credit to him for this awesome tool

Go Reference Go Report Card

Cobra library for creating powerful modern CLI doesn't support color settings for console output. StylishCobra is a small library that allows you to colorize the text output of the Cobra library, making the console output look better.

StylishCobra allows to customize the cobra text output with lipgloss styles.

It's very easy to add StylishCobra to your project!


Installing

To add the library to your project run

go get -u github.com/auribuo/stylishcobra

Quick start

Open your cmd/root.go and insert this code:

import (
    "github.com/auribuo/stylishcobra"
    "github.com/charmbracelet/lipgloss"
)

Then, before the call to Execute() of your root command put the calls to customize cobra

stylishcobra.Setup(rootCmd). // This would be enough, it would do nothing
	StyleHeadings(lipgloss.NewStyle().Underline(true).Bold(true).Foreground(lipgloss.ANSIColor(termenv.ANSICyan))).
	StyleCommands(lipgloss.NewStyle().Foreground(lipgloss.ANSIColor(termenv.ANSIYellow)).Bold(true)).
	StyleAliases(lipgloss.NewStyle().Bold(true).Italic(true)).
	StyleCmdShortDescr(lipgloss.NewStyle().Foreground(lipgloss.ANSIColor(termenv.ANSIBrightRed))).
	StyleExample(lipgloss.NewStyle().Italic(true)).
	StyleExecName(lipgloss.NewStyle().Bold(true)).
	StyleFlags(lipgloss.NewStyle().Bold(true)).
	StyleFlagsDescr(lipgloss.NewStyle().Foreground(lipgloss.ANSIColor(termenv.ANSIRed))).
	StyleFlagsDataType(lipgloss.NewStyle().Foreground(lipgloss.Color("#444444")).Italic(true)).
	Init() // Call Init() to apply the styles

stylishcobra.Init(cfg) // Initialize with a *Config. Is a bit more tedious, bacuse lipgloss.NewStyle() returns no pointer but styles in the config are stored as pointers

You can also reuse styles etc. For more information refer to the lipgloss docs.

That's all. Now build your project and see the output of the help command.

Available config parameters:

Config Parameters

  • Headings: headers style.

  • Commands: commands style.

  • CmdShortDescr: short description of commands style.

  • ExecName: executable name style.

  • Flags: short and long flag names (-f, --flag) style.

  • FlagsDataType: style of flags data type.

  • FlagsDescr: flags description text style.

  • Aliases: list of command aliases style.

  • Example: example text style.

  • NoExtraNewlines: no line breaks before and after headings, if true. By default: false.

  • NoBottomNewline: no line break at the end of Cobra's output, if true. By default: false.

The functions in the builder pattern are named Style<Parameter> except for the NoExtraNewlines and NoBottomNewline, these can be enabled by using [En/Dis]ableExtraNewLines and [En/Dis]ableNoBottomNewline


NoExtraNewlines parameter results:

extranewlines

How it works

StylishCobra patches Cobra's usage template and extends it with functions for text styling. The styles are lipgloss styles allowing for a variety of customization.

License

StylishCobra is released under the MIT license. See LICENSE.

Documentation

Overview

StyledCobra allows you to colorize Cobra's text output using lipgloss, making it look better using simple settings to customize individual parts of console output.

Usage example:

1. Insert in cmd/root.go file of your project :

import (
     "github.com/auribuo/stylishcobra"
     "github.com/charmbracelet/lipgloss"
 )

2. Put the following code to the beginning of the Execute() function:

 stylishcobra.Setup(rootCmd).
	StyleHeadings(lipgloss.NewStyle().Underline(true).Bold(true).Foreground(lipgloss.ANSIColor(termenv.ANSICyan))).
	StyleCommands(lipgloss.NewStyle().Foreground(lipgloss.ANSIColor(termenv.ANSIYellow)).Bold(true)).
	StyleAliases(lipgloss.NewStyle().Bold(true).Italic(true)).
	StyleCmdShortDescr(lipgloss.NewStyle().Foreground(lipgloss.ANSIColor(termenv.ANSIGreen))).
	StyleExample(lipgloss.NewStyle().Italic(true)).
	StyleExecName(lipgloss.NewStyle().Bold(true)).
	StyleFlags(lipgloss.NewStyle().Bold(true)).
	StyleFlagsDescr(lipgloss.NewStyle().Foreground(lipgloss.ANSIColor(termenv.ANSIRed))).
	StyleFlagsDataType(lipgloss.NewStyle().Foreground(lipgloss.Color("#444444")).Italic(true)).
	Init()

3. Build & execute your code.

Copyright © 2024 Aurelio Buonomo Released under the MIT license.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(cfg *Config)

Types

type Config

type Config struct {
	RootCmd         *cobra.Command
	Headings        *lipgloss.Style
	Commands        *lipgloss.Style
	CmdShortDescr   *lipgloss.Style
	ExecName        *lipgloss.Style
	Flags           *lipgloss.Style
	FlagsDataType   *lipgloss.Style
	FlagsDescr      *lipgloss.Style
	Aliases         *lipgloss.Style
	Example         *lipgloss.Style
	NoExtraNewlines bool
	NoBottomNewline bool
}

Config contains all the styles that can be applied using stylishcobra. Use `Setup()` to start a builder to configure. Optionally you can also instanciate your own instance

func Setup

func Setup(rootCmd *cobra.Command) *Config

Setup starts the builing process and supplies the required root command

func (*Config) DisableBottomNewline

func (cfg *Config) DisableBottomNewline() *Config

func (*Config) DisableExtraNewlines

func (cfg *Config) DisableExtraNewlines() *Config

func (*Config) EnableBottomNewline

func (cfg *Config) EnableBottomNewline() *Config

func (*Config) EnableExtraNewlines

func (cfg *Config) EnableExtraNewlines() *Config

func (*Config) Init

func (cfg *Config) Init()

Init patches Cobra's usage template with configuration provided.

func (*Config) StyleAliases

func (cfg *Config) StyleAliases(style lipgloss.Style) *Config

func (*Config) StyleCmdShortDescr

func (cfg *Config) StyleCmdShortDescr(style lipgloss.Style) *Config

func (*Config) StyleCommands

func (cfg *Config) StyleCommands(style lipgloss.Style) *Config

func (*Config) StyleExample

func (cfg *Config) StyleExample(style lipgloss.Style) *Config

func (*Config) StyleExecName

func (cfg *Config) StyleExecName(style lipgloss.Style) *Config

func (*Config) StyleFlags

func (cfg *Config) StyleFlags(style lipgloss.Style) *Config

func (*Config) StyleFlagsDataType

func (cfg *Config) StyleFlagsDataType(style lipgloss.Style) *Config

func (*Config) StyleFlagsDescr

func (cfg *Config) StyleFlagsDescr(style lipgloss.Style) *Config

func (*Config) StyleHeadings

func (cfg *Config) StyleHeadings(style lipgloss.Style) *Config

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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