mirror of
https://github.com/felixangell/phi.git
synced 2024-08-16 11:10:37 +03:00
simple file saving is now implemented
This commit is contained in:
parent
388153daca
commit
dd1f0a4fcd
@ -4,12 +4,13 @@ import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"github.com/felixangell/go-rope"
|
||||
"github.com/felixangell/phi-editor/cfg"
|
||||
"github.com/felixangell/strife"
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
"github.com/vinzmay/go-rope"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -27,6 +28,7 @@ type Buffer struct {
|
||||
contents []*rope.Rope
|
||||
curs *Cursor
|
||||
cfg *cfg.TomlConfig
|
||||
filePath string
|
||||
}
|
||||
|
||||
func NewBuffer(conf *cfg.TomlConfig) *Buffer {
|
||||
@ -40,25 +42,31 @@ func NewBuffer(conf *cfg.TomlConfig) *Buffer {
|
||||
contents: buffContents,
|
||||
curs: &Cursor{},
|
||||
cfg: config,
|
||||
filePath: "/tmp/phi_file_" + time.Now().String(), // TODO make this a randomly chosen temp file
|
||||
}
|
||||
|
||||
{
|
||||
contents, err := ioutil.ReadFile(cfg.CONFIG_FULL_PATH)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
lines := strings.Split(string(contents), "\n")
|
||||
for _, line := range lines {
|
||||
buff.appendLine(line)
|
||||
}
|
||||
}
|
||||
buff.OpenFile(cfg.CONFIG_FULL_PATH)
|
||||
|
||||
return buff
|
||||
}
|
||||
|
||||
func (b *Buffer) OnDispose() {
|
||||
func (b *Buffer) OpenFile(filePath string) {
|
||||
b.filePath = filePath
|
||||
|
||||
contents, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
lines := strings.Split(string(contents), "\n")
|
||||
for _, line := range lines {
|
||||
b.appendLine(line)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Buffer) OnDispose() {
|
||||
// hm!
|
||||
// os.Remove(b.fileHandle)
|
||||
}
|
||||
|
||||
func (b *Buffer) OnInit() {}
|
||||
@ -168,7 +176,7 @@ func (b *Buffer) processTextInput(r rune) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// TODO: shall we match single quotes and double quotes too?1
|
||||
// TODO: shall we match single quotes and double quotes too?
|
||||
|
||||
matchingPair := int(r)
|
||||
|
||||
|
@ -1,9 +1,36 @@
|
||||
package gui
|
||||
|
||||
import "log"
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
)
|
||||
|
||||
// NOTE: all shortcuts return a bool
|
||||
// this is whether or not they have
|
||||
// modified the buffer
|
||||
// if the buffer is modified it will be
|
||||
// re-rendered.
|
||||
|
||||
func Save(b *Buffer) bool {
|
||||
log.Println("Save: unimplemented!")
|
||||
|
||||
var buffer bytes.Buffer
|
||||
for idx, line := range b.contents {
|
||||
if idx > 0 {
|
||||
// TODO: this avoids a trailing newline
|
||||
// if we handle it like this? but if we have
|
||||
// say enforce_newline_at_eof or something we
|
||||
// might want to do this all the time
|
||||
buffer.WriteRune('\n')
|
||||
}
|
||||
buffer.WriteString(line.String())
|
||||
}
|
||||
|
||||
err := ioutil.WriteFile(b.filePath, buffer.Bytes(), 0775)
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
}
|
||||
log.Println("Wrote file '", b.filePath, "' to disk")
|
||||
return false
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user