helperFunctions

package module
v1.12.2 Latest Latest
Warning

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

Go to latest
Published: May 27, 2025 License: GPL-3.0 Imports: 22 Imported by: 4

README

helperFunctions

Miscellaneous helper functions


This package provides functions that I have been using all over my various tools but the obvious struck: instead of ducplicating 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 AutoPager added in v1.11.1

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 Center added in v1.8.0

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 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 GetBoolValFromPrompt

func GetBoolValFromPrompt(prompt string) bool

func GetFStype added in v1.5.1

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 Green

func Green(sentence string) string

func Pager added in v1.11.0

func Pager(lines []string, linesPerPage int)

Pager displays a slice of lines in a paginated manner.

func PagerFromFile added in v1.11.0

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

PagerFromFile reads a file and pages its content.

func PagerFromReader added in v1.11.0

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

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

func PagerFromString added in v1.11.0

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 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 White

func White(sentence string) string

func Yellow

func Yellow(sentence string) string

Types

type RepoInfo added in v1.10.0

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 added in v1.10.0

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

Jump to

Keyboard shortcuts

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