helperFunctions

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: GPL-3.0 Imports: 15 Imported by: 0

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 3 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 3 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 Blue

func Blue(sentence string) string

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 GetIntValFromPrompt

func GetIntValFromPrompt(prompt string) int

func GetPassword

func GetPassword(prompt string) string

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

func SI(nombre interface{}) string

This function was originally written in 1993, in C, by my friend Jean-François Gauthier ([email protected]) 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

This section is empty.

Jump to

Keyboard shortcuts

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