Skip to content

Commit

Permalink
Merge pull request #5 from liamg/liamg-add-disable-option
Browse files Browse the repository at this point in the history
Add func to disable all formatting output
  • Loading branch information
liamg authored Nov 11, 2019
2 parents 9825b59 + b3edfef commit f4e54be
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
18 changes: 18 additions & 0 deletions disable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tml

import "sync"

var disableFormatting bool
var formattingLock sync.RWMutex

func DisableFormatting() {
formattingLock.Lock()
defer formattingLock.Unlock()
disableFormatting = true
}

func EnableFormatting() {
formattingLock.Lock()
defer formattingLock.Unlock()
disableFormatting = false
}
16 changes: 16 additions & 0 deletions disable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tml

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestParseWithFormattingDisabled(t *testing.T) {
DisableFormatting()
defer EnableFormatting()
output, err := Parse("plain <red>red <bold>bold red</bold></red> plain <green>green</green> plain")
require.Nil(t, err)
assert.Equal(t, "plain red bold red plain green plain", output)
}
31 changes: 23 additions & 8 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,30 +93,42 @@ func (p *Parser) handleTag(name string) bool {
if strings.HasPrefix(name, "/") {
name = name[1:]
if _, isFg := fgTags[name]; isFg {
p.writer.Write([]byte(p.state.setFg(resetFg)))
if !disableFormatting {
p.writer.Write([]byte(p.state.setFg(resetFg)))
}
return true
} else if _, isBg := bgTags[name]; isBg {
p.writer.Write([]byte(p.state.setBg(resetBg)))
if !disableFormatting {
p.writer.Write([]byte(p.state.setBg(resetBg)))
}
return true
} else if attr, isAttr := attrTags[name]; isAttr {
p.writer.Write([]byte(p.state.setAttr(-int8(attr))))
if !disableFormatting {
p.writer.Write([]byte(p.state.setAttr(-int8(attr))))
}
return true
}
return false
}

if esc, ok := fgTags[name]; ok {
p.writer.Write([]byte(p.state.setFg(esc)))
if !disableFormatting {
p.writer.Write([]byte(p.state.setFg(esc)))
}
return true
}

if esc, ok := bgTags[name]; ok {
p.writer.Write([]byte(p.state.setBg(esc)))
if !disableFormatting {
p.writer.Write([]byte(p.state.setBg(esc)))
}
return true
}

if attr, ok := attrTags[name]; ok {
p.writer.Write([]byte(p.state.setAttr(int8(attr))))
if !disableFormatting {
p.writer.Write([]byte(p.state.setAttr(int8(attr))))
}
return true
}

Expand All @@ -126,9 +138,12 @@ func (p *Parser) handleTag(name string) bool {
// Parse takes input from the reader and converts any provided tags to the relevant ANSI escape codes for output to parser's writer.
func (p *Parser) Parse(reader io.Reader) error {

formattingLock.RLock()
defer formattingLock.RUnlock()

buffer := make([]byte, 1024)

if p.IncludeLeadingResets {
if p.IncludeLeadingResets && !disableFormatting {
if _, err := p.writer.Write([]byte(resetAll)); err != nil {
return err
}
Expand Down Expand Up @@ -169,7 +184,7 @@ func (p *Parser) Parse(reader io.Reader) error {
}
}

if p.IncludeTrailingResets {
if p.IncludeTrailingResets && !disableFormatting {
p.writer.Write([]byte(resetAll))
}

Expand Down

0 comments on commit f4e54be

Please sign in to comment.