Skip to content

Commit

Permalink
Merge pull request #473 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 12.124.0
  • Loading branch information
andyone authored May 12, 2024
2 parents 51537ee + acddb5b commit 64833b8
Show file tree
Hide file tree
Showing 10 changed files with 322 additions and 164 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Changelog

### 12.124.0

- `[env]` Add `Variable` struct for lazy environment reading
- `[fmtc]` Add support of `FMTC_NO_BOLD`, `FMTC_NO_ITALIC`, and `FMTC_NO_BLINK` environment variables

### 12.123.2

- `[terminal]` Fixed bug with output messages from `Error` and `Warn` to stdout instead of stderr
Expand Down
218 changes: 102 additions & 116 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.123.1"
const VERSION = "12.124.0"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down
49 changes: 49 additions & 0 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@ import (
// Env is map with environment values
type Env map[string]string

// Variable is environment variable for lazy reading
type Variable struct {
key string
value string
isRead bool
}

// ////////////////////////////////////////////////////////////////////////////////// //

// Var creates new environment variable struct
func Var(name string) *Variable {
return &Variable{key: name}
}

// Get return key-value map with environment values
func Get() Env {
env := make(Env)
Expand Down Expand Up @@ -54,6 +66,43 @@ func Which(name string) string {

// ////////////////////////////////////////////////////////////////////////////////// //

// Get returns environment variable value
func (v *Variable) Get() string {
if v == nil {
return ""
}

if !v.isRead {
v.value = os.Getenv(v.key)
v.isRead = true
}

return v.value
}

// Is returns true if environment variable value is equal to given one
func (v *Variable) Is(value string) bool {
return v.Get() == value
}

// String returns environment variable value as string
func (v *Variable) String() string {
return v.Get()
}

// Reset resets reading state of variable
func (v *Variable) Reset() *Variable {
if v == nil {
return nil
}

v.value, v.isRead = "", false

return v
}

// ////////////////////////////////////////////////////////////////////////////////// //

// Path return path as string slice
func (e Env) Path() []string {
return strings.Split(e["PATH"], ":")
Expand Down
18 changes: 18 additions & 0 deletions env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package env
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"os"
"testing"

. "github.com/essentialkaos/check"
Expand All @@ -28,6 +29,10 @@ var _ = Suite(&ENVSuite{})
// ////////////////////////////////////////////////////////////////////////////////// //

func (s *ENVSuite) TestEnv(c *C) {
if os.Getenv("EK_TEST_PORT") == "" {
os.Setenv("EK_TEST_PORT", "8080")
}

envs := Get()

c.Assert(envs["EK_TEST_PORT"], Equals, "8080")
Expand All @@ -42,4 +47,17 @@ func (s *ENVSuite) TestEnv(c *C) {

c.Assert(Which("cat"), Not(Equals), "")
c.Assert(Which("catABCD1234"), Equals, "")

var v *Variable

c.Assert(v.Get(), Equals, "")
c.Assert(v.String(), Equals, "")
c.Assert(v.Reset(), IsNil)

v = Var("EK_TEST_PORT")

c.Assert(v.Get(), Equals, "8080")
c.Assert(v.String(), Equals, "8080")
c.Assert(v.Is("8080"), Equals, true)
c.Assert(v.Reset(), NotNil)
}
40 changes: 34 additions & 6 deletions env/env_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,70 @@ package env
// ❗ Env is map with environment values
type Env map[string]string

// ❗ Variable is environment variable for lazy reading
type Variable struct {
key string
value string
isRead bool
}

// ////////////////////////////////////////////////////////////////////////////////// //

// ❗ Var creates new environment variable struct
func Var(name string) *Variable {
panic("UNSUPPORTED")
}

// ❗ Get return key-value map with environment values
func Get() Env {
panic("UNSUPPORTED")
return Env{}
}

// ❗ Which find full path to some app
func Which(name string) string {
panic("UNSUPPORTED")
return ""
}

// ////////////////////////////////////////////////////////////////////////////////// //

// ❗ Get returns environment variable value
func (v *Variable) Get() string {
panic("UNSUPPORTED")
}

// ❗ Is returns true if environment variable value is equal to given one
func (v *Variable) Is(value string) bool {
panic("UNSUPPORTED")
}

// ❗ String returns environment variable value as string
func (v *Variable) String() string {
panic("UNSUPPORTED")
}

// ❗ Reset resets reading state of variable
func (v *Variable) Reset() *Variable {
panic("UNSUPPORTED")
}

// ////////////////////////////////////////////////////////////////////////////////// //

// ❗ Path return path as string slice
func (e Env) Path() []string {
panic("UNSUPPORTED")
return nil
}

// ❗ GetS return environment variable value as string
func (e Env) GetS(name string) string {
panic("UNSUPPORTED")
return ""
}

// ❗ GetI return environment variable value as int
func (e Env) GetI(name string) int {
panic("UNSUPPORTED")
return 0
}

// ❗ GetF return environment variable value as float
func (e Env) GetF(name string) float64 {
panic("UNSUPPORTED")
return 0.0
}
6 changes: 6 additions & 0 deletions env/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ func ExampleWhich() {

fmt.Printf("Full path to echo binary is %s\n", echoPath)
}

func ExampleVar() {
v := Var("PATH")

fmt.Println(v.Get())
}
9 changes: 9 additions & 0 deletions fmtc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ If you are using SublimeText 4 (`4075+`), we strongly recommend that you install

![#colors](../.github/images/fmtc_highlight.png)

#### Environment variables

`fmtc` supports configuration via environment variables.

- `NO_COLOR` — disable all colors and modificators;
- `FMTC_NO_BOLD` — disable **bold** text;
- `FMTC_NO_ITALIC` — disable _italic_ text;
- `FMTC_NO_BLINK` — disable blinking text.

#### Modificators

| Name | Tag | Reset Tag | Code |
Expand Down
53 changes: 34 additions & 19 deletions fmtc/fmtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sync"

"github.com/essentialkaos/ek/v12/color"
"github.com/essentialkaos/ek/v12/env"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -75,15 +76,19 @@ var DisableColors = os.Getenv("NO_COLOR") != ""

// ////////////////////////////////////////////////////////////////////////////////// //

var colorsSupported bool // 16 colors support
var colors256Supported bool // 256 colors support
var colorsTCSupported bool // 24bit (TrueColor) colors support
var colorsSupportChecked bool
var isColorsSupported bool // 16 colors support
var isColors256Supported bool // 256 colors support
var isColorsTCSupported bool // 24bit (TrueColor) colors support
var isColorsSupportChecked bool

var boldDisableEnvVar = env.Var("FMTC_NO_BOLD")
var italicDisableEnvVar = env.Var("FMTC_NO_ITALIC")
var blinkDisableEnvVar = env.Var("FMTC_NO_BLINK")

var colorsMap *sync.Map

var term = os.Getenv("TERM")
var colorTerm = os.Getenv("COLORTERM")
var termEnvVar = env.Var("TERM")
var colorTermEnvVar = env.Var("COLORTERM")

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down Expand Up @@ -329,35 +334,35 @@ func Bell() {

// IsColorsSupported returns true if 16 colors is supported by terminal
func IsColorsSupported() bool {
if colorsSupportChecked {
return colorsSupported
if isColorsSupportChecked {
return isColorsSupported
}

checkForColorsSupport()

return colorsSupported
return isColorsSupported
}

// Is256ColorsSupported returns true if 256 colors is supported by terminal
func Is256ColorsSupported() bool {
if colorsSupportChecked {
return colors256Supported
if isColorsSupportChecked {
return isColors256Supported
}

checkForColorsSupport()

return colors256Supported
return isColors256Supported
}

// IsTrueColorSupported returns true if TrueColor (24-bit colors) is supported by terminal
func IsTrueColorSupported() bool {
if colorsSupportChecked {
return colorsTCSupported
if isColorsSupportChecked {
return isColorsTCSupported
}

checkForColorsSupport()

return colorsTCSupported
return isColorsTCSupported
}

// IsTag tests whether the given value is a valid color tag (or sequence
Expand Down Expand Up @@ -394,6 +399,13 @@ func tag2ANSI(tag string, clean bool) string {
var chars string

for _, key := range tag {
switch {
case key == '*' && !boldDisableEnvVar.Is(""),
key == '&' && !italicDisableEnvVar.Is(""),
key == '~' && !blinkDisableEnvVar.Is(""):
continue
}

code := codes[key]

switch {
Expand Down Expand Up @@ -654,24 +666,27 @@ func isValidNamedTag(tag string) bool {
}

func checkForColorsSupport() {
term := termEnvVar.Get()
colorTerm := colorTermEnvVar.Get()

switch {
case strings.Contains(term, "xterm"),
strings.Contains(term, "color"),
term == "screen":
colorsSupported = true
isColorsSupported = true
}

if strings.Contains(term, "256color") {
colors256Supported = true
isColors256Supported = true
}

if term == "iterm" || colorTerm == "truecolor" ||
strings.Contains(term, "truecolor") ||
strings.HasPrefix(term, "vte") {
colors256Supported, colorsTCSupported = true, true
isColors256Supported, isColorsTCSupported = true, true
}

colorsSupportChecked = true
isColorsSupportChecked = true
}

// ////////////////////////////////////////////////////////////////////////////////// //
Loading

0 comments on commit 64833b8

Please sign in to comment.