gocui: fake file potential example. can I send this to open()?

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-04-16 20:55:13 -05:00
parent 673c0336ec
commit c5f3a043b3
2 changed files with 20 additions and 6 deletions

View File

@ -3,22 +3,25 @@ package main
import (
"bytes"
"io"
"errors"
)
type FakeFile struct {
buffer bytes.Buffer
reader *bytes.Reader
buffer *bytes.Buffer
offset int64
}
func (f *FakeFile) Read(p []byte) (n int, err error) {
n, err = f.buffer.ReadAt(p, f.offset)
n, err = f.reader.ReadAt(p, f.offset)
f.offset += int64(n)
return n, err
}
func (f *FakeFile) Write(p []byte) (n int, err error) {
n, err = f.buffer.WriteAt(p, f.offset)
n, err = f.buffer.Write(p)
f.offset += int64(n)
f.reader.Reset(f.buffer.Bytes())
return n, err
}
@ -33,13 +36,23 @@ func (f *FakeFile) Seek(offset int64, whence int) (int64, error) {
case io.SeekEnd:
newOffset = int64(f.buffer.Len()) + offset
default:
return 0, io.ErrInvalidWhence
return 0, errors.New("Seek: whence not at start,current or end")
}
// never can get here right?
if newOffset < 0 {
return 0, io.ErrInvalidWhence
return 0, errors.New("Seek: offset < 0")
}
f.offset = newOffset
return f.offset, nil
}
func NewFakeFile() *FakeFile {
buf := &bytes.Buffer{}
return &FakeFile{
reader: bytes.NewReader(buf.Bytes()),
buffer: buf,
offset: 0,
}
}

View File

@ -45,6 +45,7 @@ func showMsg(g *gocui.Gui, v *gocui.View) error {
fmt.Fprintln(v, l)
}
// g.SetViewOnTop("msg")
g.SetViewOnBottom("msg")
g.SetViewBeneath("msg", "help", 24)
// g.SetViewOnBottom("msg")
return nil
}