Documentation
¶
Index ¶
- func AnsiEscaped(s string) bool
- func Bash() bool
- func Closers() closerS
- func Color() color.Index
- func ColoredTty(w io.Writer) bool
- func CygwinTerminal(fd uintptr) bool
- func Darwin() bool
- func DarwinIntel() bool
- func DarwinSilicon() bool
- func DebugBuild() bool
- func DebugMode() bool
- func DebuggerAttached() bool
- func Detected(stateName string) (state bool)
- func DockerBuild() bool
- func Env() states.CmdrMinimal
- func FileExists(filepath string) bool
- func Fish() bool
- func GetDebugLevel() int
- func GetNoColorLevel() int
- func GetQuietLevel() int
- func GetTraceLevel() int
- func GetTtySize() (cols, rows int)
- func GetTtySizeByFd(fd uintptr) (cols, rows int, err error)
- func GetTtySizeByFile(f *os.File) (cols, rows int, err error)
- func GetTtySizeByName(fn string) (cols, rows int, err error)
- func GetVerboseLevel() int
- func InBenchmark() bool
- func InDebugMode() bool
- func InDebugging() bool
- func InDevelopingTime() (status bool)
- func InDocker() bool
- func InDockerEnvSimple() (status bool)
- func InIstio() bool
- func InK8s() bool
- func InK8sYN() bool
- func InTesting() bool
- func InTestingT(args []string) bool
- func InTracing() bool
- func InVscodeTerminal() bool
- func IstioBuild() bool
- func K8sBuild() bool
- func Linux() bool
- func NoColorMode() bool
- func Powershell() bool
- func QuietModeEnabled() bool
- func ReadFile(filename string) ([]byte, error)
- func ReadPassword() (text string, err error)
- func RegisterStateGetter(state string, getter func() bool)
- func Root() bool
- func SetDebugLevel(hits int)
- func SetDebugMode(b bool)
- func SetNoColorLevel(hits int)
- func SetNoColorMode(b bool)
- func SetQuietLevel(hits int)
- func SetQuietMode(b bool)
- func SetTraceLevel(hits int)
- func SetTraceMode(b bool)
- func SetVerboseLevel(hits int)
- func SetVerboseMode(b bool)
- func ShellName() string
- func Signals() signalS
- func StartupByDoubleClick() bool
- func State(stateName string) (state bool)
- func States() states.CmdrMinimal
- func StringToBool(val string, defaultVal ...bool) (ret bool)
- func StripEscapes(str string) (strCleaned string)
- func Terminal(f *os.File) bool
- func TerminalFd(fd uintptr) bool
- func ToBool(val any, defaultVal ...bool) (ret bool)
- func TraceMode() bool
- func Tracing() bool
- func Tty(w io.Writer) bool
- func TtyEscaped(s string) bool
- func Unix() bool
- func UpdateEnvWith(env states.CmdrMinimal)
- func VerboseBuild() bool
- func VerboseModeEnabled() bool
- func Windows() bool
- func WindowsWSL() bool
- func Zsh() bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AnsiEscaped ¶ added in v0.5.9
AnsiEscaped detects a string if it contains ansi color escaped sequences
func Bash ¶ added in v0.5.27
func Bash() bool
Bash returns true if application is running under a Bash shell.
func Closers ¶ added in v0.5.7
func Closers() closerS
Closers returns the container includes all registered closable objects.
The simplest ways is using package level Close function:
func main() { defer is.Closers().Close() // others statements ... is.Closers().RegisterCloseFns(func(){ sth.Close() }) // more statements... }
func Color ¶ added in v0.5.20
Color returns an indexer for term/color subpackage.
For example, call the Translator to convert the html tags to color codes in a string:
is.Color().GetColorTranslator().Translate("<b>bold</b>")
func ColoredTty ¶ added in v0.5.1
ColoredTty detects a writer if it is a colorful tty device.
A colorful tty device can receive ANSI escaped sequences and draw its.
func CygwinTerminal ¶ added in v0.5.27
CygwinTerminal return true if the file descriptor is a cygwin or msys2 terminal. This is also always false on this environment.
func Darwin ¶ added in v0.5.27
func Darwin() bool
Darwin returns true if running under macOS platform, including both Intel and Silicon.
func DarwinIntel ¶ added in v0.5.27
func DarwinIntel() bool
DarwinIntel returns true if running under Apple Intel Machines.
func DarwinSilicon ¶ added in v0.5.27
func DarwinSilicon() bool
DarwinSilicon returns true if running under Apple Silicon.
func DebugBuild ¶
func DebugBuild() bool
func DebuggerAttached ¶
func DebuggerAttached() bool
DebuggerAttached returns status if debugger attached or is a debug build.
See also InDebugging.
func Detected ¶ added in v0.5.11
Detected returns the detected state associated with the given state name.
= State, see also it.
func DockerBuild ¶
func DockerBuild() bool
func Env ¶
func Env() states.CmdrMinimal
func FileExists ¶ added in v0.5.7
FileExists detects if a file or a directory is existed.
func Fish ¶ added in v0.5.27
func Fish() bool
Fish returns true if application is running under a Fish shell.
func GetDebugLevel ¶
func GetDebugLevel() int
func GetNoColorLevel ¶
func GetNoColorLevel() int
func GetQuietLevel ¶
func GetQuietLevel() int
func GetTraceLevel ¶
func GetTraceLevel() int
func GetTtySize ¶
func GetTtySize() (cols, rows int)
GetTtySize returns the window size in columns and rows in the active console window. The return value of this function is in the order of cols, rows.
func GetTtySizeByFd ¶ added in v0.5.9
GetTtySizeByFd retrieve terminal window size by fd (file-descriptor). such as [os.Stdout.Fd()]
func GetTtySizeByFile ¶ added in v0.5.9
GetTtySizeByFile retrieve terminal window size by *os.File object. such as os.Stdout
func GetTtySizeByName ¶ added in v0.5.9
GetTtySizeByName retrieve terminal window size by device name. such as "/dev/tty"
func GetVerboseLevel ¶
func GetVerboseLevel() int
func InBenchmark ¶ added in v0.6.2
func InBenchmark() bool
func InDebugMode ¶
func InDebugMode() bool
InDebugMode and DebugMode returns true if:
- a debug build, see the DebugBuild.
- SetDebugMode(true) called.
**NOTE** Since v0.6.0, InDebugMode does not check DebuggerAttached.
func InDebugging ¶
func InDebugging() bool
InDebugging and DebuggerAttached returns status if golang debugger 'dlv' is attached.
**Since v0.6.0**, InDebugging checks if the parent process is 'dlv' or not. It supports Linux, Darwin and Windows currently.
If you're looking for old behavior, DebugBuild() returns if the executable is built with delve tag:
When you runs `go build` with `-tags=delve` options. eg: go run -tags=delve ./cli go build -tags=delve -o my-app ./cli The executable will hold a 'isdelve' tag. For more details please goto https://stackoverflow.com/questions/47879070/how-can-i-see-if-the-goland-debugger-is-running-in-the-program
Performance Note:
For security reason InDebugging checks the debuggers lively. It might take unexpected times for detecting.
Debug States:
- is.InDebugging, loaded by a golang debugger (dlv) at runtime?
- is.DebugBuild, build tags decide it
- is.DebugMode, set by command line flag `--debug`
func InDevelopingTime ¶
func InDevelopingTime() (status bool)
InDevelopingTime detects whether is in developing time (debugging or testing).
If the main program has been built as an executable binary, we would assume which is not in developing time.
If GetDebugMode() is true, that's in developing time too.
func InDocker ¶
func InDocker() bool
InDocker detects if the service is running under docker environment.
We tests these two conditions:
- find if `/.dockerenv` exists or not.
- `docker` in buildtags
func InDockerEnvSimple ¶
func InDockerEnvSimple() (status bool)
InDockerEnvSimple detects whether is running within docker container environment.
InDockerEnvSimple finds if `/.dockerenv` exists or not.
func InIstio ¶
func InIstio() bool
InIstio detects if the service is running under istio injected.
### IMPORTANT
To make this detector work properly, you must mount a DownwordAPI volume to your container/pod. See also:
func InTestingT ¶
InTestingT detects whether is running under 'go test' mode
func InTracing ¶
func InTracing() bool
InTracing tests if trace.IsEnabled() or env.traceMode (cli app trace mode via --trace).
See the github.com/is/states/trace package.
func InVscodeTerminal ¶
func InVscodeTerminal() bool
InVscodeTerminal tests if running under visual studio code integrated terminal
func IstioBuild ¶
func IstioBuild() bool
func NoColorMode ¶
func NoColorMode() bool
func Powershell ¶ added in v0.5.27
func Powershell() bool
Powershell returns true if application is running under a Windows Powershell shell.
Not testing yet.
func QuietModeEnabled ¶
func QuietModeEnabled() bool
func ReadPassword ¶
ReadPassword reads the password from stdin with safe protection
func RegisterStateGetter ¶ added in v0.5.11
RegisterStateGetter allows integrating your own detector into State(name) bool.
For example:
func customState() bool { reutrn ... } is.RegisterStateGetter("custom", customState) println(is.State("custom"))
func Root ¶ added in v0.5.27
func Root() bool
Root returns true if current user is 'root' or user is in sudo mode.
For windows it's always false.
func SetDebugLevel ¶
func SetDebugLevel(hits int)
func SetDebugMode ¶
func SetDebugMode(b bool)
func SetNoColorLevel ¶
func SetNoColorLevel(hits int)
func SetNoColorMode ¶
func SetNoColorMode(b bool)
func SetQuietLevel ¶
func SetQuietLevel(hits int)
func SetQuietMode ¶
func SetQuietMode(b bool)
func SetTraceLevel ¶
func SetTraceLevel(hits int)
func SetTraceMode ¶
func SetTraceMode(b bool)
func SetVerboseLevel ¶
func SetVerboseLevel(hits int)
func SetVerboseMode ¶
func SetVerboseMode(b bool)
func ShellName ¶ added in v0.5.27
func ShellName() string
ShellName returns current SHELL's name.
For Windows, it could be "cmd.exe" or "powershell.exe". For Linux or Unix or Darwin, it returns environment variable $SHELL. Else it's empty "".
func Signals ¶ added in v0.5.7
func Signals() signalS
Signals returns a signals-helper so that you can catch them, and raise them later.
Typically usage is `catcher := is.Signals().Catch(); ...`.
By default, catcher will listen on standard signals set: os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT.
But you can change them with:
is.Signals().Catch(os.Kill, os.Interrupt)
or:
is.Signals().Catch().WithSignals(os.Interrupt, os.Kill)
For example:
package main import ( "context" "fmt" "os" "sync" "github.com/hedzr/env" "github.com/hedzr/go-socketlib/net" logz "logslog" ) func main() { logz.SetLevel(logz.DebugLevel) server := net.NewServer(":7099") defer server.Close() ctx, cancel := context.WithCancel(context.Background()) defer cancel() catcher := is.Signals().Catch() catcher.WithVerboseFn(func(msg string, args ...any) { logz.Debug(fmt.Sprintf("[verbose] %s", fmt.Sprintf(msg, args...))) }). WithOnSignalCaught(func(sig os.Signal, wg *sync.WaitGroup) { println() logz.Debug("signal caught", "sig", sig) if err := server.Shutdown(); err != nil { logz.Error("server shutdown error", "err", err) } cancel() }). Wait(func(stopChan chan<- os.Signal, wgShutdown *sync.WaitGroup) { logz.Debug("entering looper's loop...") server.WithOnShutdown(func(err error) { wgShutdown.Done() }) err := server.StartAndServe(ctx) if err != nil { logz.Fatal("server serve failed", "err", err) } }) }
func StartupByDoubleClick ¶ added in v0.5.9
func StartupByDoubleClick() bool
StartupByDoubleClick detects if windows golang executable file is running via double click or from cmd/shell terminator
func States ¶
func States() states.CmdrMinimal
States or Env returns a minimal environment settings for a typical CLI app.
See also states.CmdrMinimal.
func StringToBool ¶ added in v0.5.9
StringToBool converts a string to boolean value.
func StripEscapes ¶
StripEscapes removes any ansi color escaped sequences from a string
func Terminal ¶ added in v0.5.9
Terminal returns whether the given file descriptor is a terminal. Terminal detects if a file is a terminal device (tty)
func TerminalFd ¶ added in v0.5.9
TerminalFd detects if a file-descriptor is a terminal device (tty)
func Tty ¶ added in v0.5.1
Tty detects a writer if it is abstracting from a tty (console, terminal) device.
func TtyEscaped ¶ added in v0.5.1
TtyEscaped detects a string if it contains ansi color escaped sequences Deprecated v0.5.3, use HasAnsiEscaped
func Unix ¶ added in v0.5.27
func Unix() bool
Unix returns true for Linux, Darwin, and Others Unix-like Platforms.
[NOTE] for unix platforms, more assetions and tests needed.
func UpdateEnvWith ¶
func UpdateEnvWith(env states.CmdrMinimal)
func VerboseBuild ¶
func VerboseBuild() bool
func VerboseModeEnabled ¶
func VerboseModeEnabled() bool
func Windows ¶ added in v0.5.27
func Windows() bool
Windows returns true for Microsoft Windows Platform.
func WindowsWSL ¶ added in v0.5.27
func WindowsWSL() bool
WindowsWSL return true if running under Windows WSL env.
Types ¶
This section is empty.
Directories
¶
Path | Synopsis |
---|---|
_examples
|
|
Package term provides support functions for dealing with terminals, as commonly found on UNIX systems.
|
Package term provides support functions for dealing with terminals, as commonly found on UNIX systems. |
color
Package color provides a wrapped standard output device like printf but with colored enhancements.
|
Package color provides a wrapped standard output device like printf but with colored enhancements. |