Skip to content

Commit

Permalink
refactoring log
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgatis committed Feb 10, 2024
1 parent 841f38f commit 70817f1
Show file tree
Hide file tree
Showing 17 changed files with 52 additions and 75 deletions.
12 changes: 3 additions & 9 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,14 @@ import (
var _ io.ByteWriter = (*Decoder)(nil)
var _ io.Writer = (*Decoder)(nil)

// Parser is an interface for an ansi sequence parser.
type Parser interface {
// Advance advances the parser with the given byte.
Advance(b byte)
}

// Decoder is a byte writer that decodes ANSI escape sequences.
type Decoder struct {
parser Parser
parser *vte.Parser
}

// NewDecoder creates a new Decoder.
func NewDecoder(handler Handler, logger Logger) *Decoder {
performer := NewPerformer(handler, logger)
func NewDecoder(handler Handler) *Decoder {
performer := NewPerformer(handler)
parser := vte.NewParser(performer)

return &Decoder{
Expand Down
10 changes: 1 addition & 9 deletions examples/ansilog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ import (
"github.com/danielgatis/go-ansicode"
)

var _ ansicode.Logger = (*logger)(nil)

type logger struct{}

func (l *logger) Tracef(format string, args ...interface{}) {
fmt.Printf("TRACE: "+format, args...)
}

var _ ansicode.Handler = (*handler)(nil)

type handler struct{}
Expand Down Expand Up @@ -267,7 +259,7 @@ func (*handler) IdentifyTerminal(b byte) {
}

func main() {
decoder := ansicode.NewDecoder(&handler{}, &logger{})
decoder := ansicode.NewDecoder(&handler{})

reader := bufio.NewReader(os.Stdin)
buff := make([]byte, 2048)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
require (
github.com/danielgatis/go-utf8 v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/glog v1.2.0
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ github.com/danielgatis/go-vte v1.0.7 h1:i408LmEBBvIVjJ95ZAPx+xcgXmOZNOzCxL1VCn4D
github.com/danielgatis/go-vte v1.0.7/go.mod h1:HBeSBT/XiLQRNEoYpBYrBeK2mSUkOfHvsVtb8LPmexQ=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/glog v1.2.0 h1:uCdmnmatrKCgMBlM4rMuJZWOkPDqdbZPnrMXDY4gI68=
github.com/golang/glog v1.2.0/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand Down
15 changes: 11 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package ansicode

// Logger is a interface for logging.
type Logger interface {
import (
"github.com/golang/glog"
)

// Tracef logs a message.
Tracef(format string, args ...interface{})
var log = &Logger{}

// Logger wraps the glog.Logger.
type Logger struct{}

// Info logs a message at Info level.
func (l *Logger) Tracef(args ...interface{}) {
glog.V(2).Info(args...)
}
13 changes: 0 additions & 13 deletions logger_test.go

This file was deleted.

10 changes: 4 additions & 6 deletions performer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,30 @@ package ansicode

// Performer is a interface for handling ANSI escape sequences.
type Performer struct {
logger Logger
handler Handler

hasPrecedingRune bool
precedingRune rune
}

// newPerformer creates a new Performer.
func NewPerformer(handler Handler, logger Logger) *Performer {
func NewPerformer(handler Handler) *Performer {
return &Performer{
handler: handler,
logger: logger,
}
}

// Unhook is used to handle unhook operations.
func (p *Performer) Unhook() {
p.logger.Tracef("Unhandled UNHOOK")
log.Tracef("Unhandled UNHOOK")
}

// Put is used to handle put operations.
func (p *Performer) Put(b byte) {
p.logger.Tracef("Unhandled PUT byte=%v", b)
log.Tracef("Unhandled PUT byte=%v", b)
}

// Hook is used to handle hook operations.
func (p *Performer) Hook(params [][]uint16, intermediates []byte, ignore bool, r rune) {
p.logger.Tracef("Unhandled HOOK params=%v intermediates=%v ignore=%v rune=%v", params, intermediates, ignore, r)
log.Tracef("Unhandled HOOK params=%v intermediates=%v ignore=%v rune=%v", params, intermediates, ignore, r)
}
18 changes: 9 additions & 9 deletions performer_csi.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func (p *Performer) CsiDispatch(params [][]uint16, intermediates []byte, ignore
if ok {
p.handler.UnsetMode(mode)
} else {
p.logger.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
log.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
}
}

Expand All @@ -287,7 +287,7 @@ func (p *Performer) CsiDispatch(params [][]uint16, intermediates []byte, ignore
if ok {
p.handler.UnsetMode(mode)
} else {
p.logger.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
log.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
}
}

Expand All @@ -304,7 +304,7 @@ func (p *Performer) CsiDispatch(params [][]uint16, intermediates []byte, ignore
for paramsIter.HasNext() {
param, ok := paramsIter.GetNext()
if !ok {
p.logger.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
log.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
continue
}

Expand Down Expand Up @@ -549,14 +549,14 @@ func (p *Performer) CsiDispatch(params [][]uint16, intermediates []byte, ignore
p.handler.SetTerminalCharAttribute(attrWithNamedColor(CharAttributeBackground, NamedColorBrightWhite))

default:
p.logger.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
log.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
}
}

case action == 'm' && len(intermediates) > 0 && intermediates[0] == '>':
n := paramsIter.GetNextOrDefault(0)
if n != 4 {
p.logger.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
log.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
return
}

Expand All @@ -573,13 +573,13 @@ func (p *Performer) CsiDispatch(params [][]uint16, intermediates []byte, ignore
p.handler.SetModifyOtherKeys(ModifyOtherKeysResetEnableAll)

default:
p.logger.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
log.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
}

case action == 'm' && len(intermediates) > 0 && intermediates[0] == '?':
n := paramsIter.GetNextOrDefault(0)
if n != 4 {
p.logger.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
log.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
return
}

Expand Down Expand Up @@ -619,7 +619,7 @@ func (p *Performer) CsiDispatch(params [][]uint16, intermediates []byte, ignore
p.handler.SetCursorStyle(CursorStyleSteadyBar)

default:
p.logger.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
log.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
}

case action == 'r' && len(intermediates) == 0:
Expand Down Expand Up @@ -688,7 +688,7 @@ func (p *Performer) CsiDispatch(params [][]uint16, intermediates []byte, ignore
p.handler.MoveBackwardTabs(int(n))

default:
p.logger.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
log.Tracef("Unhandled CSI params=%v intermediates=%v ignore=%v action=%v", params, intermediates, ignore, action)
}
}

Expand Down
3 changes: 1 addition & 2 deletions performer_csi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,8 @@ func TestPerformer_CsiDispatch(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := &handlerMock{}
logger := &loggerMock{}

performer := NewPerformer(handler, logger)
performer := NewPerformer(handler)
performer.precedingRune = 'X'
performer.hasPrecedingRune = true
performer.CsiDispatch(tt.args.params, tt.args.intermediates, tt.args.ignore, tt.args.action)
Expand Down
6 changes: 3 additions & 3 deletions performer_esc.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (p *Performer) EscDispatch(intermediates []byte, ignore bool, b byte) {
p.handler.ConfigureCharset(CharsetIndexG3, StandardCharsetASCII)

default:
p.logger.Tracef("Unhandled ESC B intermediates=%v ignore=%v byte=%v", intermediates, ignore, b)
log.Tracef("Unhandled ESC B intermediates=%v ignore=%v byte=%v", intermediates, ignore, b)
}

case b == 'D' && len(intermediates) == 0:
Expand Down Expand Up @@ -153,7 +153,7 @@ func (p *Performer) EscDispatch(intermediates []byte, ignore bool, b byte) {
p.handler.ConfigureCharset(CharsetIndexG3, StandardCharsetSpecialCharacterAndLineDrawing)

default:
p.logger.Tracef("Unhandled ESC B intermediates=%v ignore=%v byte=%v", intermediates, ignore, b)
log.Tracef("Unhandled ESC B intermediates=%v ignore=%v byte=%v", intermediates, ignore, b)
}

case b == '7' && len(intermediates) == 0:
Expand All @@ -175,6 +175,6 @@ func (p *Performer) EscDispatch(intermediates []byte, ignore bool, b byte) {
// Do nothing.

default:
p.logger.Tracef("Unhandled ESC pintermediates=%v ignore=%v byte=%v", intermediates, ignore, b)
log.Tracef("Unhandled ESC pintermediates=%v ignore=%v byte=%v", intermediates, ignore, b)
}
}
3 changes: 1 addition & 2 deletions performer_esc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,8 @@ func TestPerformer_EscDispatch(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := &handlerMock{}
logger := &loggerMock{}

performer := NewPerformer(handler, logger)
performer := NewPerformer(handler)
performer.EscDispatch(tt.args.intermediates, tt.args.ignore, tt.args.b)

assert.Equal(t, tt.want.mock, handler)
Expand Down
2 changes: 1 addition & 1 deletion performer_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ func (p *Performer) Execute(b byte) {
case c0SO:
p.handler.SetActiveCharset(1)
default:
p.logger.Tracef("Unhandled EXECUTE byte=%v", b)
log.Tracef("Unhandled EXECUTE byte=%v", b)
}
}
3 changes: 1 addition & 2 deletions performer_execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ func TestPerformer_Execute(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := &handlerMock{}
logger := &loggerMock{}

performer := NewPerformer(handler, logger)
performer := NewPerformer(handler)
performer.Execute(tt.args.r)

assert.Equal(t, tt.want.mock, handler)
Expand Down
16 changes: 8 additions & 8 deletions performer_osc.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ func (p *Performer) OscDispatch(params [][]byte, bellTerminated bool) {
return
}

p.logger.Tracef("Unhandled OSC params=%v bellTerminated=%v", params, bellTerminated)
log.Tracef("Unhandled OSC params=%v bellTerminated=%v", params, bellTerminated)

case "4":
if len(params) <= 1 || len(params)%2 == 0 {
p.logger.Tracef("Unhandled OSC params=%v bellTerminated=%v", params, bellTerminated)
log.Tracef("Unhandled OSC params=%v bellTerminated=%v", params, bellTerminated)
return
}

for i := 1; i < len(params); i += 2 {
ps, ok := parseNumber(params[i])
if !ok {
p.logger.Tracef("Unhandled OSC params=%v bellTerminated=%v", params, bellTerminated)
log.Tracef("Unhandled OSC params=%v bellTerminated=%v", params, bellTerminated)
continue
}

Expand All @@ -66,7 +66,7 @@ func (p *Performer) OscDispatch(params [][]byte, bellTerminated bool) {
prefix := fmt.Sprintf("4;%d", ps)
p.handler.SetDynamicColor(prefix, int(ps), terminator)
} else {
p.logger.Tracef("Unhandled OSC params=%v bellTerminated=%v", params, bellTerminated)
log.Tracef("Unhandled OSC params=%v bellTerminated=%v", params, bellTerminated)
}
}

Expand Down Expand Up @@ -98,7 +98,7 @@ func (p *Performer) OscDispatch(params [][]byte, bellTerminated bool) {

case "10", "11", "12":
if len(params) < 2 {
p.logger.Tracef("Unhandled OSC params=%v bellTerminated=%v", params, bellTerminated)
log.Tracef("Unhandled OSC params=%v bellTerminated=%v", params, bellTerminated)
return
}

Expand All @@ -112,7 +112,7 @@ func (p *Performer) OscDispatch(params [][]byte, bellTerminated bool) {
index := int(NamedColorForeground) + offset

if index > int(NamedColorCursor) {
p.logger.Tracef("Unhandled OSC params=%v bellTerminated=%v", params, bellTerminated)
log.Tracef("Unhandled OSC params=%v bellTerminated=%v", params, bellTerminated)
break
}

Expand All @@ -122,7 +122,7 @@ func (p *Performer) OscDispatch(params [][]byte, bellTerminated bool) {
} else if string(param) == "?" {
p.handler.SetDynamicColor(strconv.Itoa(dynamicCode), index, terminator)
} else {
p.logger.Tracef("Unhandled OSC params=%v bellTerminated=%v", params, bellTerminated)
log.Tracef("Unhandled OSC params=%v bellTerminated=%v", params, bellTerminated)
}

dynamicCode++
Expand Down Expand Up @@ -156,7 +156,7 @@ func (p *Performer) OscDispatch(params [][]byte, bellTerminated bool) {
p.handler.ResetColor(int(NamedColorCursor))

default:
p.logger.Tracef("Unhandled OSC params=%v bellTerminated=%v", params, bellTerminated)
log.Tracef("Unhandled OSC params=%v bellTerminated=%v", params, bellTerminated)
}
}

Expand Down
3 changes: 1 addition & 2 deletions performer_osc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ func TestPerformer_OscDispatch(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := &handlerMock{}
logger := &loggerMock{}

performer := NewPerformer(handler, logger)
performer := NewPerformer(handler)
performer.OscDispatch(tt.args.params, tt.args.bellTerminated)

assert.Equal(t, tt.want.mock, handler)
Expand Down
3 changes: 1 addition & 2 deletions performer_print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ func TestPerformer_Print(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := &handlerMock{}
logger := &loggerMock{}

performer := NewPerformer(handler, logger)
performer := NewPerformer(handler)
performer.Print(tt.args.r)

assert.Equal(t, tt.want.mock, handler)
Expand Down
5 changes: 2 additions & 3 deletions performer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
func TestNewPerformer(t *testing.T) {
type args struct {
handler Handler
logger Logger
}

type want struct {
Expand All @@ -21,12 +20,12 @@ func TestNewPerformer(t *testing.T) {
args args
want want
}{
{name: "new", args: args{handler: nil, logger: nil}, want: want{performer: &Performer{handler: nil}}},
{name: "new", args: args{handler: nil}, want: want{performer: &Performer{handler: nil}}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewPerformer(tt.args.handler, tt.args.logger)
got := NewPerformer(tt.args.handler)
assert.Equal(t, tt.want.performer, got)
})
}
Expand Down

0 comments on commit 70817f1

Please sign in to comment.