helperFunctions

package module
v2.4.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2025 License: GPL-3.0 Imports: 22 Imported by: 0

README

helperFunctions

Miscellaneous helper functions


IMPORTANT NOTE This package will enter major refactoring soon; this current document is a bit outdated as it is.

This package provides functions that I have been using all over my various tools but the obvious struck: instead of duplicating code on my filesystem, I should have a single source of truth.

Overview

This package is currently divided in 4 files; if the scope of the project expands (and I expect it to expand, at some point), we might be talking about sub-packages here.

For now, we have 4 files, with a specific area of responsibility:

File Area / coverage
prompt4Types.go Returns a value of a given type (string, bool, int, etc), with a prompt
This might seem useless, but I've needed something like that many times
terminal.go Terminal-related functions, such as colouring output, getting terminal size (WxH), etc
encodeDecodePassword.go Functions related to encoding/decoding strings, prompting for (non-echoed) passwords, etc)
misc.go Various minor functions

This is basic, for now. I really intend on expanding on this package, and eventually have it documented on pkg.go.dev/

Installation

A simple go get github.com/jeanfrancoisgratton/functionHelpers and we're done.

Usage, per file/package

(it is "per file" for now, as I'm not dividing it in sub-packages)

FILE: misc.go

Two functions, SI(), and ReverseString().

SI()

SI came from a need to have comma-separated number out of a number that wasn't.

For instance, you have the number 123456789. In a comma-separated format (SI-notation, SI for "Système International"); it becomes, in a string: 123,456,789

You input a number, it will come back as an SI-formatted string.

ReverseString()

The name says it all:

You input a string ("abcdef"), it returns its reverse ("fedcba").

FILE: prompt4Types.go

Many functions there, all following the same pattern...

The functions:

GetStringValFromPrompt()
GetIntValFromPrompt()
GetBoolValFromPrompt()
GetStringSliceFromPrompt()

The structure is intuitive: Get<SCALAR/SLICE>FromPrompt()

DATATYPE is either String, Int, Bool... for now

SCALAR/SLICE is either Val (Scalar, single value) or Slice. This affects the return data type.

All take a single parameter, the "prompt", ie "the message/question you want to pass to the user"

FILE: terminal.go

This one is basically (for now) used to colour the output, or get the TTY size, or clear a TTY.

To get the current TTY size, you call GetTerminalSize(), it'll return the WIDTHxHEIGHT (2 int values)

To clear a terminal TTY is quite easy: just call ClearTTY(), no params.

The next functions are "colour functions:"
Red(sentence)
Green(sentence)
White(sentence)
Yellow(sentence)
Blue(sentence)

Where "sentence" is a variable of type string that you want to return in the appropriate colour.

FILE: encodeDecodePassword.go

3 functions in here :

GetPassword(sentence) : you will be prompted (the sentence string type in parameters) to enter a password. _The password is not echoed on the TTY; it will be returned in a non-encoded form as a string. If you wish to return it as an encoded form, you can use the next function...

EncodeString(string to be encoded, secret key) The string to be encoded will be returned. If secret key is empty, a default secret key is provided. Of course, this is not the suggested behaviour...

Important note: secret key has to be exactly 32 bytes long, otherwise the default key is used.

DecodeString(string to be decoded, secret key) The string to be decoded will be returned. If secret key is empty, a default secret key is provided. Of course, this is not the suggested behaviour...

Important note: secret key has to be exactly 32 bytes long, otherwise the default key is used.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AmericanStopPanelGlyph added in v2.4.2

func AmericanStopPanelGlyph() string

func AutoPager

func AutoPager(input interface{}, linesPerPage int) *cerr.CustomError

AutoPager dispatches to the appropriate PagerFrom* function based on input type.

func Blue

func Blue(sentence string) string

func BlueInfoGlyph added in v2.4.2

func BlueInfoGlyph() string

func Center

func Center(input string) string

Center string in the terminal

func ChangeLog

func ChangeLog(cl string, clear bool)

Prints the changelog of the calling tool

func ClearTTY

func ClearTTY()

Yeah... I know.. nobody should clear a TTY in-tool... :p

func DecodeString

func DecodeString(encodedstring string, privateKey string) string

func EnabledGlyph added in v2.4.1

func EnabledGlyph() string

Status / utility

func EncodeString

func EncodeString(string2encode string, privateKey string) string

Quick functions to encode and decode strings This is based on my encryption-decryption tool : https://github.com/jeanfrancoisgratton/encdec

func ErrorGlyph added in v2.4.2

func ErrorGlyph() string

func EuropeanStopPanelGlyph added in v2.4.2

func EuropeanStopPanelGlyph() string

Stop signs

func FatalCollisionGlyph added in v2.4.2

func FatalCollisionGlyph() string

Fatal symbols

func FatalSkullBonesGlyph added in v2.4.2

func FatalSkullBonesGlyph() string

func GetBoolValFromPrompt

func GetBoolValFromPrompt(prompt string) bool

func GetFStype

func GetFStype(mountpoint string) (string, *cerr.CustomError)

Check which type of filesystem the mountpoint is. Currently only supports MacOS and linux; I've broken my Windows test VM and have no time for it

func GetIntValFromPrompt

func GetIntValFromPrompt(prompt string) int

func GetPassword

func GetPassword(prompt string, debugmode bool) string

Breaking change: if DebugMode is true, we catch the passwd in cleartext

func GetStringSliceFromPrompt

func GetStringSliceFromPrompt(prompt string) []string

func GetStringValFromPrompt

func GetStringValFromPrompt(prompt string) string

Getting typed values from prompt

func GetTerminalSize

func GetTerminalSize() (int, int)

TERMINAL FUNCTIONS

func GetValueFromPrompt

func GetValueFromPrompt(prompt string) interface{}

This one is more generic in the sense that it should be used whenever We cannot know before run-time what type of value should be expected

func Glyph added in v2.4.2

func Glyph(kind string) string

Glyph returns the glyph string for a given kind. Supported kinds: "stop-eu", "stop-us", "fatal-collision", "fatal-skull", "go", "error", "info", "note", "scroll", "tip", "lightbulb", "thumbs-up", "thumbs-down".

func Green

func Green(sentence string) string

func GreenGoGlyph added in v2.4.2

func GreenGoGlyph() string

Go

func GreenOkGlyph added in v2.4.2

func GreenOkGlyph() string

func InfoGlyph added in v2.4.2

func InfoGlyph() string

func LightbulbGlyph added in v2.4.2

func LightbulbGlyph() string

func NoteGlyph added in v2.4.2

func NoteGlyph() string

func Pager

func Pager(lines []string, linesPerPage int)

Pager displays a slice of lines in a paginated manner.

func PagerFromFile

func PagerFromFile(filePath string, linesPerPage int) *cerr.CustomError

PagerFromFile reads a file and pages its content.

func PagerFromReader

func PagerFromReader(r io.Reader, linesPerPage int) *cerr.CustomError

PagerFromReader reads from an io.Reader and pages its content.

func PagerFromString

func PagerFromString(text string, linesPerPage int)

PagerFromString splits a full string into lines, then pages it.

func Red

func Red(sentence string) string

COLOR FUNCTIONS ===============

func RedErrorGlyph added in v2.4.2

func RedErrorGlyph() string

func ReverseString

func ReverseString(s string) (result string)

This function takes a string and returns its reverse Thus, "12345" becomes "54321"

func Right(input string) string

Right aligns the input string to the right side of the terminal.

func SI

func SI(nombre interface{}) string

This function was originally written in 1993, in C, by my friend Jean-François Gauthier (jief@brebis.dyndns.org) I've ported it in C# in 2011. It still loosely based on J.F.Gauthier's version, somehow; credit is given where credit is due This function transforms a multi-digit number in International Notation; 1234567 thus becomes 1,234,567

func ScrollGlyph added in v2.4.2

func ScrollGlyph() string

func ThumbsDownGlyph added in v2.4.2

func ThumbsDownGlyph() string

func ThumbsUpGlyph added in v2.4.2

func ThumbsUpGlyph() string

func TipGlyph added in v2.4.2

func TipGlyph() string

func WarningGlyph added in v2.4.1

func WarningGlyph() string

func White

func White(sentence string) string

func Yellow

func Yellow(sentence string) string

func YellowTipGlyph added in v2.4.2

func YellowTipGlyph() string

func YellowWarningGlyph added in v2.4.2

func YellowWarningGlyph() string

Types

type RepoInfo

type RepoInfo struct {
	Scheme        string // e.g., https, ssh ("" if unknown)
	Host          string // e.g., git.example.com
	Port          string // optional (if included in the URL)
	TopLevelOwner string // e.g., group or username
	FullOwnerPath string // e.g., group/subgroup
	Repo          string // e.g., repo (no .git)
	RawURL        string // original input, for debug
}

func ExtractRepoInfo

func ExtractRepoInfo(raw string) (RepoInfo, error)

ExtractRepoInfo: extracts various info from the URL or SSH-style URL Returns an error if the URL is invalid or if the repo name is missing

Directories

Path Synopsis
logging/logger.go helperFunctions - logging subpackage Refactor: Aug 13, 2025 (USER outside severity ladder) File: logging/logger.go
logging/logger.go helperFunctions - logging subpackage Refactor: Aug 13, 2025 (USER outside severity ladder) File: logging/logger.go

Jump to

Keyboard shortcuts

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