Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 Add rainbow colouring of steps label #290

Merged
merged 1 commit into from
Sep 8, 2019

Conversation

chmouel
Copy link
Member

@chmouel chmouel commented Sep 5, 2019

We were showing the same blue color for every steps, let's now cycle of a list of
colors.

Demo: https://asciinema.org/a/hbJk1PqMFE8hleRpDsraPw0WB

Closes #285

Release Notes

Steps are now labelled with different colours instead of the same one 🎨

@tekton-robot tekton-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 5, 2019
@tekton-robot
Copy link
Contributor

The following is the coverage report on pkg/.
Say /test pull-tekton-cli-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/cmd/taskrun/log_writer.go 93.3% 93.8% 0.4
pkg/formatted/color.go Do not exist 63.6%

Copy link
Contributor

@gavinfish gavinfish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this😄

@tekton-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: chmouel, gavinfish

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link
Member

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍🏼 one comment though 😅

func (rc *RainbowColors) Get(label string) *color.Color {
if _, ok := rc.cached[label]; !ok {
rc.cached[label] = rc.all[rc.current]
rc.current++
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be a race here if this is called in different goroutine 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i tough about that but since we werent using it i didn't follow tough, what do you suggest? external lib for locking etc...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and fyi i did test it with -race and it was running well but i'll just sync/lock/release it in case off,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chmouel right, we might not be in the case of race condition in the cli, but if somebody uses this might hit it

@tekton-robot
Copy link
Contributor

The following is the coverage report on pkg/.
Say /test pull-tekton-cli-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/cmd/taskrun/log_writer.go 93.3% 93.8% 0.4
pkg/formatted/color.go Do not exist 69.2%

if _, ok := rc.cached[label]; !ok {
rc.cached[label] = rc.all[rc.current]
rc.current++
if rc.current >= len(rc.all) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: usually this is done using rc.current = (rc.current +1) % len(rc.all) without having the need for an if

}

func (rc *RainbowColors) Get(label string) *color.Color {
mux.Lock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mux must be part of the rc and not a package level variable; this allows for mulitple RainbowColors to be used without having one instance to wait for another instance.


func (rc *RainbowColors) Get(label string) *color.Color {
mux.Lock()
defer mux.Unlock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is better to use Rlock and RWlocks


"github.com/fatih/color"
)

var (
mux sync.Mutex
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mux should be instance variable and not package var since it only locks the rc.cached and not a package level variable.

Copy link
Member

@sthaha sthaha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ Neat!

some comments about sync.Mutex that needs addressing.

@tekton-robot tekton-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 5, 2019
@tekton-robot
Copy link
Contributor

The following is the coverage report on pkg/.
Say /test pull-tekton-cli-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/cmd/taskrun/log_writer.go 93.3% 93.8% 0.4
pkg/formatted/color.go Do not exist 78.9%

Copy link
Member

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/meow boxes

@tekton-robot
Copy link
Contributor

@vdemeester: cat image

In response to this:

/lgtm
/meow boxes

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@tekton-robot tekton-robot added the lgtm Indicates that a PR is ready to be merged. label Sep 5, 2019
@vdemeester
Copy link
Member

vdemeester commented Sep 5, 2019

/hold
To let @sthaha review it 👼

@tekton-robot tekton-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 5, 2019
}

var (
allColors = []color.Attribute{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

red is avoided since we keep it for error messages, (probably should have put this in the comment instead of here)


"github.com/fatih/color"
)

type counter struct {
Copy link
Member

@sthaha sthaha Sep 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I mentione RLock and Lock() I was hinting to having a single mutex in the rainbow. which will be RLocked for checking if the item is in cache and if not, Lock() to add the item in cache. but i like your implementation to create a round robin counter but for the counter itself, Get and increase isn't quite as useful as just a next which returns the next value.

please consider one of these implemention for counter. 1. that uses atomic, the other that uses a mutex

type atomicCounter struct {
  value     uint32
  threshold int
}

func (c *atomicCounter) next() int {
  v := atomic.AddUint32(&c.value, 1)
  next := int(v-1) % c.threshold
  atomic.CompareAndSwapUint32(&c.value, uint32(c.threshold), 0)
  return next

}

type counter struct {
  value     int
  threshold int
  m         sync.Mutex
}

func (c *counter) next() int {
  c.m.Lock()
  defer c.m.Unlock()

  v := c.value
  c.value = (v + 1) % c.threshold
  return v

}

and the rainbow would be some what like this

type rainbow struct {
  cache   sync.Map
  counter counter
}

func newRainbow() *rainbow {
  return &rainbow{
    counter: counter{threshold: len(palette)},
  }

}

func (r *rainbow) colorFor(x string) color {
  if value, ok := r.cache.Load(x); ok {
    return value.(color)
  }

  clr := palette[r.counter.next()]
  r.cache.Store(x, clr)
  return clr
}

We were showing the same blue color for every steps, let's now cycle of a list of
colors.

Signed-off-by: Chmouel Boudjnah <[email protected]>
@tekton-robot tekton-robot removed the lgtm Indicates that a PR is ready to be merged. label Sep 6, 2019
@tekton-robot
Copy link
Contributor

The following is the coverage report on pkg/.
Say /test pull-tekton-cli-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/formatted/color.go Do not exist 58.8%

Copy link
Member

@sthaha sthaha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me expect for the Fprintf

Copy link
Member

@sthaha sthaha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@tekton-robot tekton-robot added the lgtm Indicates that a PR is ready to be merged. label Sep 8, 2019
@sthaha sthaha removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 8, 2019
@tekton-robot tekton-robot merged commit 6950ae0 into tektoncd:master Sep 8, 2019
@chmouel chmouel deleted the issue-285-rainbow-colours branch September 8, 2019 16:36
chmouel added a commit that referenced this pull request Sep 26, 2019
#290 | [Chmouel Boudjnah] Add rainbow colouring of the steps | 2019/09/08-05:36
#292 | [danielhelfand] add SilenceUsage: true to root.go | 2019/09/08-22:11
#293 | [Carlos Santana] Update install instructions in readme to 0.3.1 | 2019/09/08-23:39
#279 | [16yuki0702] Add deletion scenario to e2e test | 2019/09/09-02:17
#279 | [16yuki0702] Fix to use -f flag | 2019/09/09-02:17
#300 | [Chmouel Boudjnah] Use a older version of thrift | 2019/09/09-08:48
#300 | [Chmouel Boudjnah] Don't run go mod vendor on every build | 2019/09/09-08:48
#301 | [Chmouel Boudjnah] E2E tests fixes | 2019/09/09-12:00
#303 | [danielhelfand] add taskrun describe command | 2019/09/10-02:15
#304 | [16yuki0702] Fix document typos | 2019/09/10-04:45
null | [Piyush Garg] Fix interactive prompt with --last | 2019/09/10-07:04
null | [16yuki0702] Add canceling feature to taskrun command | 2019/09/10-08:23
null | [Chmouel Boudjnah] Improve release script | 2019/09/11-10:35
null | [Sunil Thaha] Doc: Improve taskrun help example | 2019/09/12-02:09
null | [Piyush Garg] Fix -n not working with pipeline logs | 2019/09/12-02:23
null | [Piyush Garg] Show descriptive failure message | 2019/09/12-03:36
null | [danielhelfand] change error message returned by task for namespace | 2019/09/16-06:48
null | [Chmouel Boudjnah] Add a Tasrun to build rpm. | 2019/09/17-01:52
null | [Chmouel Boudjnah] Add information about copr rpm package repo in README | 2019/09/17-01:52
null | [Chmouel Boudjnah] Use khuram spec files which does a proper source build | 2019/09/17-01:52
null | [Chmouel Boudjnah] Update README with distros supported for dnf install | 2019/09/17-01:52
null | [Nikhil Thomas] Fix CI breakage | 2019/09/19-23:57
null | [Andrea Frittoli] Fix the logs command when creating a pipelinerun | 2019/09/20-01:57
null | [16yuki0702] Adding missing copyright header | 2019/09/23-04:47
null | [danielhelfand] correct copr package section typos | 2019/09/23-08:38
null | [danielhelfand] align pipeline describe output with other describe commands | 2019/09/24-04:58
null | [Chmouel Boudjnah] Add no colour option | 2019/09/24-05:24
null | [Chmouel Boudjnah] Disable nocolour test | 2019/09/24-05:24
null | [Chmouel Boudjnah] Generate documentation | 2019/09/24-05:24
null | [danielhelfand] align output with task describe command | 2019/09/24-05:36
null | [cappyzawa] fix misspell based on https://goreportcard.com/report/github.com/tektoncd/cli#misspell | 2019/09/24-06:20
null | [danielhelfand] add check to taskrun cancel for already executed taskrun | 2019/09/24-07:56
null | [Pradeep Kumar] Common method to get last run for a pipeline | 2019/09/24-08:17
null | [Pradeep Kumar] correts imports order | 2019/09/24-08:17
null | [Andrea Frittoli] Improve the error message on pipeline create | 2019/09/24-09:40
null | [Andrea Frittoli] Fix the logs command when creating a pipelinerun | 2019/09/24-09:40
null | [danielhelfand] add check to pipelinerun cancel for already executed pipelinerun | 2019/09/25-09:11
null | [Vincent Demeester] Bump tektoncd/pipeline to 0.7.x | 2019/09/25-10:02
null | [Vincent Demeester] bump github.com/AlecAivazis/survey/v2 to v2.0.4 | 2019/09/25-10:02
null | [Khurram Baig] Add Vendored Dependencies to RPM Spec | 2019/09/26-03:03

Signed-off-by: Chmouel Boudjnah <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cla: yes lgtm Indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rainbow colours with "tkn * logs" command
6 participants