Make Highly Customized Boxes for your CLI

Box CLI Maker πŸ“¦

Box CLI Maker is a Highly Customized Terminal Box Creator.

go.dev reference godocs.io CI Go Report Card GolangCI GitHub release

Features

  • Make Terminal Box in 8️⃣ inbuilt different styles
  • 16 Inbuilt Colors and True Color Support 🎨
  • Custom Title Positions
  • Make your own Terminal Box style πŸ“¦
  • Align the text according to the need
  • Unicode, Emoji and Windows Console Support πŸ˜‹
  • Written in πŸ‡¬ πŸ‡΄

Installation

 go get github.com/Delta456/box-cli-maker

Usage

In main.go

package main

import "github.com/Delta456/box-cli-maker/v2"

func main() {
 Box := box.New(box.Config{Px: 2, Py: 5, Type: "Single", Color: "Cyan"})
 Box.Print("Box CLI Maker", "Highly Customized Terminal Box Maker")
}

box.New(config Config) accepts a Config struct with following parameters and returns a Box struct.

  • Parameters
    • Px : Horizontal Padding
    • Py : Vertical Padding
    • ContentAlign : Align the content inside the Box i.e. Center, Left and Right
    • Type: Type of Box
    • TitlePos : Position of the Title i.e. Inside, Top and Bottom
    • Color : Color of the Box

Box struct Methods

Box.Print(title, lines string) prints Box from the specified arguments.

  • Parameters
    • title : Title of the Box
    • lines : Content to be written inside the Box

Box.Println(title, lines string) prints Box in a newline from the specified arguments.

  • Parameters
    • title : Title of the Box
    • lines : Content to be written inside the Box

Box.String(title, lines string) string return string representation of Box.

  • Parameters
    • title : Title of the Box
    • lines : Content to be written inside the Box

Box Types

  • Single

single

  • Single Double

single_double

  • Double

double

  • Double Single

double_single

  • Bold

bold

  • Round

round

  • Hidden

hidden

  • Classic

classic

Title Positions

  • Inside

single

  • Top

top

  • Bottom

bottom

Making custom Box

You can make your custom Box by using the inbuilt Box struct provided by the module.

type Box struct {
 TopRight    string // Symbols used for TopRight Corner
 TopLeft     string // Symbols used for TopLeft Corner
 Vertical    string // Symbols used for Vertical Bars
 BottomRight string // Symbols used for BottomRight Corner
 BottomLeft  string // Symbols used for BottomRight Corner
 Horizontal  string // Symbols used for Horizontal Bars
 Config // Config for the Box struct
}

Using it:

package main

import "github.com/Delta456/box-cli-maker/v2"

func main() {
    config := box.Config{Px: 2, Py: 3, Type: "", TitlePos: "Inside"}
    boxNew := box.Box{TopRight: "*", TopLeft: "*", BottomRight: "*", BottomLeft: "*", Horizontal: "-", Vertical: "|", Config: config}
    boxNew.Println("Box CLI Maker", "Make Highly Customized Terminal Boxes")
}

Output:

custom

Color Types

It has color support from gookit/color module from which this module uses FgColor and FgHiColor. Color is a key for the following maps:

 fgColors map[string]color.Color = {
  "Black":   color.FgBlack,
  "Blue":    color.FgBlue,
  "Red":     color.FgRed,
  "Green":   color.FgGreen,
  "Yellow":  color.FgYellow,
  "Cyan":    color.FgCyan,
  "Magenta": color.FgMagenta,
  "White":   color.FgWhite,
}

 fgHiColors map[string]color.Color = {
  "HiBlack":   color.FgDarkGray,
  "HiBlue":    color.FgLightBlue,
  "HiRed":     color.FgLightRed,
  "HiGreen":   color.FgLightGreen,
  "HiYellow":  color.FgLightYellow,
  "HiCyan":    color.FgLightCyan,
  "HiMagenta": color.FgLightMagenta,
  "HiWhite":   color.FgLightWhite,
}

If you want High Intensity Colors then the Color name must start with Hi. If Color option is empty or invalid then Box with default Color is formed.

  1. True Color is also possible though you need to provide it as uint or [3]uint and make sure that the terminals which will be targetted must have it supported.

  2. [3]uint's element all must be in a range of [0, 0xFF] and uint in range of [0x000000, 0xFFFFFF].

As convenience, if the terminal's doesn't support True Color then it will round off according to the terminal's max supported colors which makes it easier for the users not to worry about other terminal for most of the cases.

Here's a list of 24 bit supported terminals and 8 bit supported terminals.

This module also enables True Color and 256 Colors support on Windows Console through Virtual Terminal Processing but you need have at least Windows 10 Version 1511 for 256 colors or Windows 10 Version 1607 for True Color Support.

4-bit Colors are now standardized so it is supported by all terminals now.

If ConEmu or ANSICON is installed for Windows systems then it will be also detected. It is highly recommended to use the latest versions of both of them to have the best experience!

Note

1. Vertical Alignment

As different terminals have different font by default so the right vertical alignment may not be aligned well. You will have to change your font accordingly to make it work.

2. Limitations of Unicode and Emoji

It uses mattn/go-runewidth for Unicode and Emoji support though there are some limitations:

  • Windows Terminal, ConEmu and Mintty are the only know terminal emulators which can render Unicode and Emojis properly on Windows.
  • Indic Text only works on very few Terminals as less support it.
  • It is recommended not to use this for Online Playgrounds like Go Playground and Repl.it, CI/CDs etc. because they use a font that only has ASCII support and other Character Set is used which becomes problematic for finding the length as the font changes during runtime.
  • Change your font which supports Unicode and Emojis else the right vertical alignment will break.

3. Terminal Color Detection

It is possible to round off true color provided to 8 bit or 16 bit according to your terminal's maximum capacity.

There is no standardized way of detecting the terminal's maximum color capacity so the way of detecting your terminal might not work for you. If this can be fixed for your terminal then you can always make a PR.

The following two points are just applicable for Unix systems:

  • If you think that the module can't detect True Color of the terminal then you must set your environment variable COLORTERM to truecolor or 24bit for True Color support.

  • If you are targetting 8 bit color based terminals and the module couln't detect it then set your environment variable TERM to name of the terminal emulator with 256color as suffix like xterm-256color.

There might be no color effect for very old terminals like Windows Console (Legacy Mode) or environment variable TERM give DUMB so it will output some garbage value or a warning if used.

In Online Playgrounds, CI/CDs, Browsers etc, it is recommended not to use this module with color effect as few may have it but this is hard to detect in general. If you think that it's possible then open an issue and address the solution!

Acknowledgements

I thank the following people and their packages whom I have studied and was able to port to Go accordingly.

Special thanks to @elimsteve who helped me to optimize the code and told me the best possible ways to fix my problems and @JalonSolov for tab lines support.

Kudos to moul/golang-repo-template for their Go template.

Related

License

Licensed under MIT

Owner
Comments
  • [BUG] long text fails to wrap

    [BUG] long text fails to wrap

    Describe the bug Text lines that are longer than terminal width fail to wrap inside the box

    To Reproduce You can reproduce the behaviour by simply creating a string that is longer than terminal width: notice that you can retrieve the current terminal length with

    import (
        "golang.org/x/term"
    )
    
    width, _, err := term.GetSize(0)
    if err != nil {
        return
    }
    

    and replicate a string of given width.

    Then simply

    Box.Print("title", long_string)
    

    produces effects like this.

    Expected behavior The box wraps text automatically. Ideally the user can specify wrap after a certain length.

    Workaround At the moment I am making use of go-wordwrap to wrap the text myself before feeding it into the box.

    import (
    	"github.com/mitchellh/go-wordwrap"
    )
    Box.Print("title", wordwrap.WrapString(sb.String(), uint((2*width)/3)))
    

    Versions (please complete the following information, if relevant):

    Machine:   MacBookPro15,2
    Kernel:     Darwin 21.2.0
    OS         macOS 12.1.0 Monterey
    Terminal   iTerm2 (Version 3.4.14)
    Shell      /bin/zsh
    
  • [BUG] When your text have no-displayable characters, the box alignment won't be perfect.

    [BUG] When your text have no-displayable characters, the box alignment won't be perfect.

    Describe the bug When your text have no-displayable characters, the box alignment won't be perfect.

    To Reproduce

    package main
    
    import "github.com/Delta456/box-cli-maker/v2"
    
    func main() {
    	Box := box.New(box.Config{Px: 2, Py: 1, Type: "Single", Color: "Cyan", TitlePos: "Top"})
    	Box.Print("\033[1mFirst Break\033[0m", "Btw \033[1mhere is broken as well\033[0m, sorry")
    }
    
    
  • [BUG] Formatting of the Top Title is lost

    [BUG] Formatting of the Top Title is lost

    Describe the bug On the dev branch, If I write a Title and a text with formatting like 033[1m something \033[0m The text formatting stay as it should be. But the Title one loose its formatting.

    To Reproduce

    func main() {
            Box := box.New(box.Config{Px: 2, Py: 1, Type: "Single", Color: "Cyan", TitlePos: "Top"})
    	Box.Print("\033[1mBroken\033[0m, sorry", "Btw \033[1mNot Broken\033[0m, very nice")
    }
    

    Screenshot from 2022-10-04 01-11-55

    As you see Broken is not anymore Bold as it should be.

    The output string is :

    "\x1b[36mβ”Œ Broken, sorry ──────────────┐\x1b[0m\n\x1b[36mβ”‚\x1b[0m"
    

    When it should be :

    "\x1b[36mβ”Œ \x1b[1mBroken\x1b[0m\x1b[36m, sorry ──────────────┐\x1b[0m"
    

    You have to somehow split by the reset tag \x1b[0m and add the color \x1b[36m after again.

  • Allowing Title Manipulation

    Allowing Title Manipulation

    In order to fix #31 :

    • I moved color.ClearCode to inside repeatWithString function (where I created a clean string for counting, and the no-altered string is used for print
    • After have done that I need now to add color before the command lines tag and after, so for that on checkColorType I split the string by the exit tag, and I add after it the style back.

    There is 2 questions now :

    • you know better your code, is it a good (non breaking) commit ?
    • That PR allows to change the color of the text by yourself (let's say I want to have a rainbow as title), Is it what you want ?
  • make sure detectTerminalColor() doesn't panic on macOS

    make sure detectTerminalColor() doesn't panic on macOS

    Hi @sharifelgamal! Thanks for forking my repo and fixing this issue. I didn't encounter this as I don't have a Mac OS πŸ˜…

    I hope I can PR these changes to my main repo.

  • [BUG] Multi line strings break vertical alignment

    [BUG] Multi line strings break vertical alignment

    Describe the bug The vertical alignment of the Box in the right will be broken if multi line strings are used.

    To Reproduce Steps to reproduce the behavior:

    Box := box.New(box.Config{Px: 2, Py: 5, Type: Single})
    Box.Println(`Box CLI Maker 
    		foo foobar`, `Highly Customized Terminal Box Maker  
    		bar`)
    

    Expected behavior The vertical alignment shouldn't break.

    Screenshots / Logs If applicable, add screenshots or logs to help explain your problem.

    image

    Versions (please complete the following information, if relevant):

    • Software version: v1.3.2
    • OS: Windows 10
    • Golang version: 1.15.3
  • release: v2.3.0

    release: v2.3.0

    Changelog

    • Addition of TitleColor and ContentColor
    • Enabled ANSI Codes for Title and Content
    • Tab lines finally work in the title
    • Add the ability to manually wrap Content via AllowWrapping and WrappingLimit
    • Several simplifications and fixes in README.md
    • Fix ANSICON detection check
    • Add examples in the examples/ folder
    • Add new screenshots of Boxes and badges in README.md
    • More documentation of code
    • Add logo (Made by @KunalRaghav)
  • dev: release v2.2.0

    dev: release v2.2.0

    TODO List:

    • [x] Add Terminal Detection.
      • [x] Unix Systems
      • [x] Windows 10 build >= build 10586 (8 bit) and >= 14931 (24 bit)
    • [x] Add 24 bit support for Windows Console
    • [x] Add rounding off of 24 bit color to the terminal's max capacity
      • [x] 8 bit (256 colors)
      • [x] 4 bit (16 colors)
    • [x] README.md enhancement
    • [x] go.doc changes
    • [x] Code Cleanup
    • [x] Print a warning if the terminal has no color support
    • [x] More testing for Unix Systems
    • [x] Use gookit/color instead of faith/color
    • [x] Use color.PrintXX instead of fmt.PrintXX??
    • [x] Add White and HiWhite as inbuilt colors
    • [x] More comments??
    • [x] Fix CI
    • [x] Move detection logic to detect_unix.go and detect_windows.go respectively.
  • [BUG] ^v2.0.0 not getting updated on pkg.go.dev

    [BUG] ^v2.0.0 not getting updated on pkg.go.dev

    What is the URL of the page with the issue?

    https://pkg.go.dev/github.com/Delta456/[email protected]

    What is your user agent?

    Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0

    Screenshot

    image

    What did you do?

    I tried sending a request to update my module to the latest

    Repo: https://github.com/Delta456/box-cli-maker

    What did you expect to see?

    Information got updated

    What did you see instead?

    404 Page Not Found

  • Setting ContentAlign to Left prints error.

    Setting ContentAlign to Left prints error.

    https://github.com/Delta456/box-cli-maker/blob/9758b768604d385557ae102160961fcb1dbfd8f4/util.go#L24

    Hello,

    Nice work on this project! I just wanted to point out that when you specify a ContentAlign of Left an error will be printed. It appears to be a missing else if check for Left. Ironically the else defaults to left align. :)

  • [feature request] IO-independent output

    [feature request] IO-independent output

    Is your feature request related to a problem? Please describe. Currently it only supports outputting to os.Stdout.

    Describe the solution you'd like Should take advantage of Go's io.Writer and have Stdout-specific configurations as a io.Writer-compliant type/struct. That way we can do stuff like simultaneous logging and displaying of output with io.MultiWriter

    Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

    Additional context See the io package.

  • [BUG] `TestTabWithColorBox` test fails on macos terminal

    [BUG] `TestTabWithColorBox` test fails on macos terminal

    Describe the bug When running the test especially on TestTabWithColorBox, it crashes (specifically an out-of-bounds crash)

    To Reproduce Steps to reproduce the behavior:

    1. Test go test -v .
    2. See error

    Expected behavior Successful test

    Screenshots / Logs image

    Versions (please complete the following information, if relevant):

    • Software version: dev branch
    • OS: Mac OS 11.6
    • Golang version: 1.19
    • Terminal using and Version: MacOS Terminal 2.11

    Additional context Debugging the text and the separator in runes shows the following results:

    color.ClearCode(topBar): [9484 32 66 111 120 32 32 32 32 32 67 76 73 32 32 32 32 32 77 97 107 101 114 32 32 32 128230 32 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9488]
    color.ClearCode(title): [66 111 120 32 32 32 67 76 73 32 32 32 32 32 77 97 107 101 114 32 32 32 128230]
    

    the title text in the topbar in []runes (the color.clearCode(topBar)) is 66 111 120 32 32 32 32 32 67 76 73 32 32 32 32 32 77 97 107 101 114 32 32 32 128230 while the separator (the color.clearCode(title)) is 66 111 120 32 32 32 67 76 73 32 32 32 32 32 77 97 107 101 114 32 32 32 128230

    if you look at it closely on the first set of 32s the text has 5 32s while the separator has only 3 32s

    probably an issue with the ClearCode method

  • [IMP] Breaking changes in v3 release

    [IMP] Breaking changes in v3 release

    As I was a newbie who started this project 2 years ago. I had made tons of mistakes by doing an initial release v1.0.0 instead of v0.0.1 as I thought it would be fine but now after gaining experience it seems like I will have to do some major changes.

    As a library, one must never print to os.Stderr, instead of doing that one must return an err and let the user handle it plus Color field would be renamed to BoxColor so that it will be consistent to TitleColor and ContentColor:

    package main
    
    import "github.com/Delta456/box-cli-maker/v3"
    import "log"
    
    func main() {
     Box := box.New(box.Config{Px: 2, Py: 5, Type: "Single", BoxColor: "Cyan"})
     err := Box.Print("Box CLI Maker", "Highly Customized Terminal Box Maker")
       if err != nil {
       log.Fatal(err)
       }
    }
    

    Most of the panics for unknown Color types will also be changed probably to err so that the user can handle those too.

    Warnings like Unknown Color Terminal Profile, Unknown Alignment provided etc will also be changed to err, though the Box will still be created successfully with default settings.

    If there are any more changes needed then I will also add those too.

Highly customizable and lightweight Go CLI app framework πŸ‘Œ
Highly customizable and lightweight Go CLI app framework πŸ‘Œ

Nice ?? Nice is a highly customizable and lightweight framework for crafting CLI apps. Nice respects idiomatic Go code and focuses to be clear, effici

Dec 30, 2022
An easy to use menu structure for cli applications that prompts users to make choices.
An easy to use menu structure for cli applications that prompts users to make choices.

WMenu Package wmenu creates menus for cli programs. It uses wlog for its interface with the command line. It uses os.Stdin, os.Stdout, and os.Stderr w

Dec 26, 2022
An attempt to make a cli for dev.to in Go
An attempt to make a cli for dev.to in Go

Devto a cli for dev.to This is a work in progress so don't a expect a full support for Dev API(beta). Table of contents Devto a cli for dev.to Table o

Mar 13, 2022
Minutes is a CLI tool for synchronizing work logs between multiple time trackers, invoicing, and bookkeeping software to make entrepreneurs' daily work easier.
Minutes is a CLI tool for synchronizing work logs between multiple time trackers, invoicing, and bookkeeping software to make entrepreneurs' daily work easier.

Minutes is a CLI tool for synchronizing work logs between multiple time trackers, invoicing, and bookkeeping software to make entrepreneurs' daily work easier.

Aug 8, 2022
JOB, make your short-term command as a long-term job. ε°†ε‘½δ»€θ‘Œθ§„εˆ’ζˆδ»»εŠ‘ηš„ε·₯ε…·

job make your short-term command as a long-term job Install Shell Install (Linux & MacOS) # binary will be $(go env GOPATH)/bin/job $: curl -sfL https

Nov 12, 2022
A daemon to make your keyboard backlight smart.

keyboard-backlight-daemon A daemon to make your keyboard backlight smart. Features Light up keyboard backlight based on user interaction (keyboard, mo

Jan 17, 2022
A highly-performant command runtime server written in Go for Flame πŸ”₯

Flame Command Runtime A highly-performant command runtime server written in Go for Flame ?? Flame Command Runtime(FCR) is powerful, highly customizabl

Aug 29, 2021
A cutting edge (haha), prototype, object-oriented and highly modular slash command handler for Discordgo.
A cutting edge (haha), prototype, object-oriented and highly modular slash command handler for Discordgo.

ken ⚠️ Disclaimer This package is still in a very early state of development and future updates might include breaking changes to the API until the fi

Dec 12, 2022
A highly-scalable, entity-resolution technology that was originally developed to connect internal data together

TiloRes CLI What is TiloRes? TiloRes is a highly-scalable, β€œentity-resolution” technology that was originally developed to connect internal data toget

Jun 17, 2022
Lightweight CLI tool to programmatically rescale your Hetzner virtual server daily to optimize your budget spending

Lightweight CLI tool to programmatically rescale your Hetzner virtual server daily to optimize your budget spending

Nov 28, 2022
A CLI to replace your git commit command, so your git message can partially follow the Conventional Changelog ecosystem
A CLI to replace your git commit command, so your git message can partially follow the Conventional Changelog ecosystem

COMMIT CLI A CLI to replace your git commit command, so your git message can partially follow the Conventional Changelog ecosystem. And yes, it is bui

Feb 9, 2022
Are you programming and suddenly your stomach is rumbling? No problem, order your Ifood without leaving your favorite text editor ❀️

vim-ifood VocΓͺ ta programando e de repente bateu aquela fome? Sem problemas, peΓ§a seu Ifood sem sair do seu editor de texto favorito ❀️ Are you progra

Jun 2, 2022
Go package to make lightweight ASCII line graph β•­β”ˆβ•― in command line apps with no other dependencies.
Go package to make lightweight ASCII line graph β•­β”ˆβ•― in command line apps with no other dependencies.

asciigraph Go package to make lightweight ASCII line graphs β•­β”ˆβ•―. Installation go get github.com/guptarohit/asciigraph Usage Basic graph package main

Jan 8, 2023
Make any Go function into a API (FaaS)

faas Make any (Go) function into an API with one HTTP request. This is a FaaS: functions as a service. But, in actuality, its more of a FaaSSS: functi

Dec 30, 2022
Make Link with Markdown Format
Make Link with Markdown Format

ml -- Make Link with Markdown Format This package is required Go 1.16 or later. Build and Install $ go install github.com/spiegel-im-spiegel/gpgpdump@

Mar 20, 2022
This repository contains utility functions that do not make sense in other packages.

Installation go get github.com/IQ-tech/go-utils Executing code before process exits AtInterruption receives a function that will be called once befor

Dec 9, 2021
This package to make it easy to work with env

Go Env This package to make it easy to work with env Example usage package main

Jan 30, 2022
Bofin - A command line tool that can be used by to make Weblink development more productive

Bofin A command line tool that can be used by to make Weblink development more p

Jan 13, 2022
Marshallable - Make generic data types marshallable

Marshallable Make generic data types marshallable! Features Implement methods: S

Feb 15, 2022