From a73d459769c6eebeb8b1421a9e18d6964340df3b Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Sat, 21 Oct 2023 14:27:50 +0300 Subject: [PATCH 1/2] [protip] Disabling tips using environment vairable (PROTIP=0) --- CHANGELOG.md | 4 ++++ ek.go | 2 +- protip/protip.go | 10 +++++++++- protip/protip_test.go | 8 ++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 322c6151..6ee19188 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/ek.go b/ek.go index e2617cc3..4b7de9f7 100644 --- a/ek.go +++ b/ek.go @@ -20,7 +20,7 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "12.83.0" +const VERSION = "12.83.1" // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/protip/protip.go b/protip/protip.go index 8e1c8f8d..dcb3bf2d 100644 --- a/protip/protip.go +++ b/protip/protip.go @@ -3,6 +3,7 @@ package protip import ( "math/rand" + "os" "github.com/essentialkaos/ek/v12/fmtc" "github.com/essentialkaos/ek/v12/fmtutil/panel" @@ -40,6 +41,8 @@ var Options = panel.Options{panel.TOP_LINE, panel.BOTTOM_LINE} // ////////////////////////////////////////////////////////////////////////////////// // +var disabled = os.Getenv("PROTIP") == "0" + var collection *Tips // ////////////////////////////////////////////////////////////////////////////////// // @@ -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) { @@ -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 } diff --git a/protip/protip_test.go b/protip/protip_test.go index 61ba6c05..0fbd3aab 100644 --- a/protip/protip_test.go +++ b/protip/protip_test.go @@ -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) From 3b0c81ad8cd2b67e048a7ce5652cb31412d82f4e Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Sat, 21 Oct 2023 14:29:53 +0300 Subject: [PATCH 2/2] Fix typos --- CHANGELOG.md | 2 +- usage/usage_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ee19188..31ec3fa2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### 12.83.1 -* `[protip]` Disabling tips using environment vairable (`PROTIP=0`) +* `[protip]` Disabling tips using environment variable (`PROTIP=0`) ### 12.83.0 diff --git a/usage/usage_test.go b/usage/usage_test.go index dcc782d1..09a4b774 100644 --- a/usage/usage_test.go +++ b/usage/usage_test.go @@ -146,7 +146,7 @@ func (s *UsageSuite) TestUsage(c *C) { c.Assert(info.GetOption("u:unknown").String(), Equals, "") } -func (s *UsageSuite) TestDeattachedPrint(c *C) { +func (s *UsageSuite) TestDetachedPrint(c *C) { cmd := &Command{Name: "test", Desc: "Test command", ColorTag: "{#99}"} opt := &Option{Long: "test", Short: "T", Desc: "Test option", ColorTag: "{#99}"}