2024-01-18 00:08:37 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"io"
|
2024-11-13 21:32:44 -06:00
|
|
|
|
|
|
|
"github.com/awesome-gocui/gocui"
|
2024-01-18 00:08:37 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type FakeFile struct {
|
|
|
|
reader *bytes.Reader
|
|
|
|
buffer *bytes.Buffer
|
|
|
|
offset int64
|
2024-11-13 21:32:44 -06:00
|
|
|
view *gocui.View
|
2024-01-18 00:08:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FakeFile) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = f.reader.ReadAt(p, f.offset)
|
|
|
|
f.offset += int64(n)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2024-11-14 09:50:27 -06:00
|
|
|
var fakecount int = 0
|
|
|
|
|
2024-01-18 00:08:37 -06:00
|
|
|
func (f *FakeFile) Write(p []byte) (n int, err error) {
|
|
|
|
n, err = f.buffer.Write(p)
|
|
|
|
f.offset += int64(n)
|
|
|
|
f.reader.Reset(f.buffer.Bytes())
|
2024-11-13 21:32:44 -06:00
|
|
|
f.view.Write(p)
|
2024-11-14 09:50:27 -06:00
|
|
|
fakecount += 1
|
|
|
|
if fakecount > 20 {
|
|
|
|
fakecount = 0
|
|
|
|
f.view.Clear()
|
|
|
|
}
|
2024-01-18 00:08:37 -06:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FakeFile) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
newOffset := f.offset
|
|
|
|
|
|
|
|
switch whence {
|
|
|
|
case io.SeekStart:
|
|
|
|
newOffset = offset
|
|
|
|
case io.SeekCurrent:
|
|
|
|
newOffset += offset
|
|
|
|
case io.SeekEnd:
|
|
|
|
newOffset = int64(f.buffer.Len()) + offset
|
|
|
|
default:
|
|
|
|
return 0, errors.New("Seek: whence not at start,current or end")
|
|
|
|
}
|
|
|
|
// never can get here right?
|
|
|
|
|
|
|
|
if newOffset < 0 {
|
|
|
|
return 0, errors.New("Seek: offset < 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.offset = newOffset
|
|
|
|
return f.offset, nil
|
|
|
|
}
|
|
|
|
|
2024-11-13 21:32:44 -06:00
|
|
|
func NewFakeFile(v *gocui.View) *FakeFile {
|
2024-01-18 00:08:37 -06:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
return &FakeFile{
|
|
|
|
reader: bytes.NewReader(buf.Bytes()),
|
|
|
|
buffer: buf,
|
|
|
|
offset: 0,
|
2024-11-13 21:32:44 -06:00
|
|
|
view: v,
|
2024-01-18 00:08:37 -06:00
|
|
|
}
|
|
|
|
}
|