bel 2023-10-29 09:08:44 -06:00
parent ce5bf71f6b
commit 622c173264
1 changed files with 24 additions and 1 deletions

View File

@ -3,10 +3,13 @@ package ledger
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
"unicode"
@ -33,7 +36,27 @@ func (files Files) TempGetLastNLines(n int) ([]string, error) {
}
func (files Files) TempSetLastNLines(n int, lines []string) error {
panic("e")
p := files.paths()[0]
w, err := ioutil.TempFile(os.TempDir(), path.Base(p))
if err != nil {
return err
}
defer w.Close()
r, err := os.Open(p)
if err != nil {
return err
}
defer r.Close()
if _, err := peekLastNLines(w, r, n); err != nil {
return err
}
for i := range lines {
fmt.Fprintln(w, lines[i])
}
return errors.New(w.Name())
}
func peekLastNLines(w io.Writer, r *bufio.Reader, n int) ([]string, error) {