Skip to content

Commit e361141

Browse files
authored
Merge pull request #8 from liamg/liamg-sort-out-writer
Add Fprintf
2 parents abf123f + 47eca5b commit e361141

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

parser.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ type parserState struct {
2323
type attrs uint8
2424

2525
const (
26-
bold uint8 = 1
27-
dim = 2
28-
underline = 4
29-
blink = 8
30-
reverse = 16
31-
hidden = 32
26+
bold uint8 = 1 << iota
27+
dim
28+
underline
29+
blink
30+
reverse
31+
hidden
3232
)
3333

3434
var resetAll = "\x1b[0m"
@@ -160,7 +160,7 @@ func (p *Parser) Parse(reader io.Reader) error {
160160
}
161161
return err
162162
}
163-
for _, r := range []rune(string(buffer[:n])) {
163+
for _, r := range string(buffer[:n]) {
164164

165165
if inTag {
166166
if r == '>' {

printf.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@ package tml
22

33
import (
44
"fmt"
5+
"io"
6+
"os"
57
)
68

79
// Printf works like fmt.Printf, but adds the option of using tags to apply colour or text formatting to the written text. For example "<red>some red text</red>".
810
// A full list of tags is available here: https://github.com/liamg/tml
911
func Printf(input string, a ...interface{}) error {
12+
return Fprintf(os.Stdout, input, a...)
13+
}
14+
15+
func Fprintf(w io.Writer, input string, a ...interface{}) error {
1016
format, err := Parse(input)
1117
if err != nil {
1218
return err
1319
}
14-
_, err = fmt.Printf(format, a...)
20+
_, err = fmt.Fprintf(w, format, a...)
1521
return err
1622
}
17-

println.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
package tml
22

3+
import (
4+
"io"
5+
"os"
6+
)
7+
38
// Println works like fmt.Println, but adds the option of using tags to apply colour or text formatting to the written text. For example "<red>some red text</red>".
49
// A full list of tags is available here: https://github.com/liamg/tml
510
func Println(input string) {
6-
Printf(input + "\n")
11+
Fprintln(os.Stdout, input)
12+
}
13+
14+
func Fprintln(w io.Writer, input string) {
15+
Fprintf(w, "%s\n", input)
716
}

0 commit comments

Comments
 (0)