Skip to content

Commit

Permalink
[protip] Disabling tips using environment vairable (PROTIP=0)
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 21, 2023
1 parent 583b4c6 commit a73d459
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### 12.83.1

* `[protip]` Disabling tips using environment vairable (`PROTIP=0`)

### 12.83.0

* `[protip]` Added new package for showing usage tips
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.83.0"
const VERSION = "12.83.1"

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

Expand Down
10 changes: 9 additions & 1 deletion protip/protip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package protip

import (
"math/rand"
"os"

"github.com/essentialkaos/ek/v12/fmtc"
"github.com/essentialkaos/ek/v12/fmtutil/panel"
Expand Down Expand Up @@ -40,6 +41,8 @@ var Options = panel.Options{panel.TOP_LINE, panel.BOTTOM_LINE}

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

var disabled = os.Getenv("PROTIP") == "0"

var collection *Tips

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -50,6 +53,11 @@ func Add(tips ...*Tip) {
collection = &Tips{}
}

// Don't add if tips are disabled by env vars
if disabled {
return
}

for _, tip := range tips {
if tip == nil || tip.Title == "" ||
tip.Message == "" || !fmtc.IsTag(tip.ColorTag) {
Expand All @@ -66,7 +74,7 @@ func Add(tips ...*Tip) {

// Show shows random tip if required
func Show(force bool) bool {
if collection == nil || len(collection.data) == 0 {
if collection == nil || len(collection.data) == 0 || disabled {
return false
}

Expand Down
8 changes: 8 additions & 0 deletions protip/protip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ func (s *TipSuite) TestAdd(c *C) {
c.Assert(collection.data, HasLen, 0)

tip.Message = "Test message"

disabled = true

Add(tip)
c.Assert(collection.data, HasLen, 0)

disabled = false

Add(tip)
c.Assert(collection.data, HasLen, 1)
c.Assert(collection.data[0].Weight, Equals, 0.5)
Expand Down

0 comments on commit a73d459

Please sign in to comment.