gopy/bind/printer.go

72 lines
1.3 KiB
Go
Raw Normal View History

2015-07-24 14:16:31 +00:00
// Copyright 2015 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bind
import (
"bytes"
"fmt"
)
type printer struct {
buf *bytes.Buffer
indentEach []byte
indentText []byte
needIndent bool
}
func (p *printer) writeIndent() error {
if !p.needIndent {
return nil
}
p.needIndent = false
_, err := p.buf.Write(p.indentText)
return err
}
2015-07-29 06:36:45 +00:00
func (p *printer) Read(b []byte) (n int, err error) {
return p.buf.Read(b)
}
2015-07-24 14:16:31 +00:00
func (p *printer) Write(b []byte) (n int, err error) {
wrote := 0
for len(b) > 0 {
if err := p.writeIndent(); err != nil {
return wrote, err
}
i := bytes.IndexByte(b, '\n')
if i < 0 {
break
}
n, err = p.buf.Write(b[0 : i+1])
wrote += n
if err != nil {
return wrote, err
}
b = b[i+1:]
p.needIndent = true
}
if len(b) > 0 {
n, err = p.buf.Write(b)
wrote += n
}
return wrote, err
}
func (p *printer) Printf(format string, args ...interface{}) {
if _, err := fmt.Fprintf(p, format, args...); err != nil {
panic(fmt.Sprintf("printer: %v", err))
}
}
func (p *printer) Indent() {
p.indentText = append(p.indentText, p.indentEach...)
}
func (p *printer) Outdent() {
if len(p.indentText) > len(p.indentEach)-1 {
p.indentText = p.indentText[len(p.indentEach):]
}
}