hedzr/is (-go)
`is` detectors and its basic supports in golang
Intro to is
is
provides numerous detectors for checking the states of environment (build, executive, ...).
Features
is.State(which) bool
: the universal detector entry - viaRegisterStateGetter(state string, getter func() bool)
to add your own ones. Since v0.5.11is.Env()
holds a global struct for CLI app basic states, such as: verbose/quiet/debug/trace....DebugMode
/DebugLevel
,TraceMode
/TraceLevel
,ColorMode
, ...
is.InDebugging() bool
,is.InTesting() bool
, andis.InTracing() bool
, ....is.DebugBuild() bool
.is.K8sBuild() bool
,is.DockerBuild() bool
, ....is.ColoredTty() bool
, ....is.Color()
to get an indexer for the functions in our term/color subpackage, ...- Terminal Colorizer, Detector, unescape tools.
- stringtool:
RandomStringPure
, case-converters ... - basics: closable, closer, signals.
- easier
Press any key to exit...
prompt:is.Signals().Catch()
- easier
- exec: Run, RunWithOutput, Sudo, ...
- go1.23.7+ required since v0.7.0
go 1.22.7+ required
To using environment detecting utilities better and smoother, some terminal (and stringtool, basics) tools are bundled together.
Since v0.6.0, is.InDebugging()
checks if the running process' parent is dlv
.
The old DebugMode
and DebugBuild
are still work:
InDebugging
: checks this process is being debugged bydlv
.DebugBuild
:-tags=delve
is set at building.DebugMode
:--debug
is specified at command line.
Since v0.8.27, basics.Signals().Catcher().WaitFor()
wants ctx
param passed in.
Usages
Result is similar with:
NOTE that is.Signals().Catch()
will produce a prompt and enter a infinite loop to wait for user's keystroke pressed.
Lists
The partials:
-
is.InDebugging / InDebugMode
-
is.DebuggerAttached (relyes on delve tag)
-
is.InTracing / InTestingT
-
is.InTesting / InTestingT
-
is.InDevelopingTime
-
is.InVscodeTerminal
-
is.InK8s
-
is.InIstio
-
is.InDocker / InDockerEnvSimple
-
Build
- is.K8sBuild
- is.IstioBuild
- is.DockerBuild
- is.VerboseBuild
- is.DebugBuild
- buildtags.IsBuildTagExists
-
States / Env
- VerboseModeEnabled
- GetVerboseLevel / SetVerboseMode / SetVerboseLevel
- QuietModeEnabled
- GetQuietLevel / SetQuietMode / SetQuietLevel
- NoColorMode
- GetNoColorLevel / SetNoColorMode / SetNoColorLevel
- DebugMode
- GetDebugLevel / SetDebugMode / SetDebugLevel
- Tracing
- TraceMode
- GetTraceLevel / SetTraceMode / SetTraceLevel
-
Terminal / Tty
- is.Terminal(file)
- is.TerminalFd(fd)
- is.Tty(wr)
- is.ColoredTty(wr)
- is.AnsiEscaped(s) (
IsTtyEscaped(s)) - StripEscapes(s)
- ReadPassword
- GetTtySize
- is.GetTtySizeByName(filename) (cols,rows,err)
- is.GetTtySizeByFile(file) (cols,rows,err)
- is.GetTtySizeByFd(fd) (cols,rows,err)
- StartupByDoubleClick() bool
-
[Special] Terminal / Color
- escaping tools: GetCPT()/GetCPTC()/GetCPTNC()
- Highlight, Dimf, Text, Dim, ToDim, ToHighlight, ToColor, ...
-
Basics
- closers
- Peripheral, Closable, Closer
- RegisterClosable
- RegisterClosers
- RegisterCloseFns
is.Signals().Catcher()
- is.FileExists(filepath)
- is.ToBool, StringToBool
- closers
Build Tags
Some functions want special buildtags presented. These are including:
verbose
: See VerboseBuild, ...delve
: See DebugBuild, ...k8s
: See K8sBuildistio
: See IstioBuilddocker
: See DockerBuild- ...
buildtags.IsBuildTagExists(tag) bool
Colorizes
The test codes:
Result:
And more:
Cursor
Since v0.8+, A new color.Cursor
object can be initialized by color.New()
, which support format the colorful text with streaming calls, for console/tty.
See the online docs for more usages.
The examples are:
color
subpackage
Package color provides a wrapped standard output device like printf but with colored enhancements.
The main types are Cursor and Translator.
Cursor allows formatting colorful text and moving cursor to another coordinate.
New will return a Cursor object.
RowsBlock is another cursor controller, which can treat the current line and following lines as a block and updating these lines repeatedly. This feature will help the progressbar writers or the continuous lines updater.
Translator is a text and tiny HTML tags translator to convert these markup text into colorful console text sequences. GetCPT can return a smart translator which translate colorful text or strip the ansi escaped sequence from result text if states.Env().IsNoColorMode()
is true.
Color is an interface type to represent a terminal color object, which can be serialized to ansi escaped sequence directly by [Color.Color].
To create a Color object, there are several ways:
- by NewColor16, or use Color16 constants directly like FgBlack, BgGreen, ...
- by NewColor256 to make a 8-bit 256-colors object
- by NewColor16m to make a true-color object
- by NewControlCode or ControlCode constants
- by NewFeCode or FeCode constants
- by NewSGR or use CSIsgr constants directly like SGRdim, SGRstrike, ...
- by NewStyle to make a compounded object
- ...
Integrated with cmdr
Closers
The Closers()
collects all closable objects and allow shutting down them at once.
Signals
Signals()
could catch OS signals and entering a infinite loop.
For example, a tcp server could be:
some packages has stayed in progress so the above codes is just a skeleton (from go-socketlib/_examples/new-loop/main.go/v1).
Contributions
Kindly welcome, please issue me first for keeping this repo smaller.
License
under Apache 2.0
How is this guide?
Last updated on