Skip to content

Commit

Permalink
Add view tags (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofreczek authored Nov 26, 2020
1 parent 48fa782 commit b1eea4b
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ View consists of:
* title
* component styles - styles are applied to the components by matching first of component tags with style ids
* additional styling (i.e. line color)
* tags - if specified, view will contain only components tagged with one of the view tags. When no tag is defined, all components will be included in the rendered view.

In order to instantiate default view, use the view builder:
```go
Expand All @@ -110,6 +111,7 @@ v := view.NewView().
WithFontColor(color.Black).
Build(),
).
WithTag("TAG").
Build()
```

Expand All @@ -124,6 +126,8 @@ view:
background_color: ffffffff
font_color: 000000ff
border_color: 000000ff
tags:
- TAG
```
```go
Expand Down
35 changes: 35 additions & 0 deletions pkg/view/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,31 @@ func (v View) render(s model.Structure) string {
sb.WriteString(buildSkinParamRectangle(s.id, s.backgroundColor, s.fontColor, s.borderColor))
}

excludedComponentIds := map[string]struct{}{}
for _, c := range s.Components {
if !v.hasTag(c.Tags...) {
excludedComponentIds[c.ID] = struct{}{}
}
}

for _, c := range s.Components {
_, excluded := excludedComponentIds[c.ID]
if excluded {
continue
}
sb.WriteString(buildComponent(c))
}

for src, to := range s.Relations {
for trg, _ := range to {
_, srcExcluded := excludedComponentIds[src]
if srcExcluded {
continue
}
_, trgExcluded := excludedComponentIds[trg]
if trgExcluded {
continue
}
sb.WriteString(buildComponentConnection(src, trg, v.lineColor))
}
}
Expand All @@ -38,3 +57,19 @@ func (v View) RenderTo(s model.Structure, w io.Writer) error {
_, err := w.Write([]byte(out))
return err
}

func (v View) hasTag(tags ...string) bool {
if len(v.tags) == 0 {
return true
}

for _, vt := range v.tags {
for _, t := range tags {
if t == vt {
return true
}
}
}

return false
}
10 changes: 10 additions & 0 deletions pkg/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ import (

type View struct {
title string
tags []string
componentStyles map[string]ComponentStyle
lineColor color.Color
}

func newView(
title string,
tags []string,
componentStyles map[string]ComponentStyle,
lineColor color.Color,
) View {
return View{
title: title,
tags: tags,
componentStyles: componentStyles,
lineColor: lineColor,
}
Expand All @@ -33,6 +36,7 @@ func NewView() *Builder {
return &Builder{
View: View{
title: "",
tags: make([]string, 0),
componentStyles: make(map[string]ComponentStyle),
lineColor: color.Black,
},
Expand Down Expand Up @@ -60,6 +64,11 @@ func (b *Builder) WithTitle(t string) *Builder {
return b
}

func (b *Builder) WithTag(t string) *Builder {
b.tags = append(b.tags, t)
return b
}

func (b *Builder) WithComponentStyle(s ComponentStyle) *Builder {
b.componentStyles[s.id] = s
return b
Expand All @@ -75,6 +84,7 @@ func (b *Builder) WithLineColor(c color.Color) *Builder {
func (b Builder) Build() View {
return newView(
b.title,
b.tags,
b.componentStyles,
b.lineColor,
)
Expand Down
Loading

0 comments on commit b1eea4b

Please sign in to comment.