mukan-ignite/ignite/pkg/truncatedbuffer/truncatedbuffer.go
Mukan Erkin Törük 26b204bd04
Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
feat: fork Ignite CLI v29 as Mukan Ignite — remove cosmos-sdk restrictions
2026-05-11 03:31:37 +03:00

49 lines
1 KiB
Go

package truncatedbuffer
import (
"bytes"
)
// TruncatedBuffer contains a bytes buffer that has a limited capacity.
// The buffer is truncated on Write if the length reaches the maximum capacity.
// Only the first bytes are preserved.
type TruncatedBuffer struct {
buf *bytes.Buffer
cap int
}
// NewTruncatedBuffer returns a new TruncatedBuffer.
// If the provided cap is 0, the truncated buffer has no limit for truncating.
func NewTruncatedBuffer(c int) *TruncatedBuffer {
return &TruncatedBuffer{
buf: &bytes.Buffer{},
cap: c,
}
}
// GetBuffer returns the buffer.
func (b TruncatedBuffer) GetBuffer() *bytes.Buffer {
return b.buf
}
// GetCap returns the maximum capacity of the buffer.
func (b TruncatedBuffer) GetCap() int {
return b.cap
}
// Write implements io.Writer.
func (b *TruncatedBuffer) Write(p []byte) (n int, err error) {
n, err = b.buf.Write(p)
if err != nil {
return n, err
}
// Check surplus bytes
surplus := b.buf.Len() - b.cap
if b.cap > 0 && surplus > 0 {
b.buf.Truncate(b.cap)
}
return n, nil
}