cleanups to match go standard layout structure; more tiny refactorings

This commit is contained in:
Felix Angell 2021-01-09 13:30:16 +00:00
parent 5f8fcc654b
commit 0f7f1bf077
28 changed files with 80 additions and 202 deletions

2
.gitignore vendored
View File

@ -3,6 +3,8 @@ tests/
phi-editor
phi
phi.exe
main
main.exe
fmt.sh
.idea/
vendor/

View File

@ -1,7 +1,7 @@
package buff
import (
"github.com/felixangell/phi/lex"
"github.com/felixangell/phi/internal/lex"
"log"
"strconv"
)

View File

@ -2,10 +2,10 @@ package buff
import (
"fmt"
"github.com/felixangell/phi/cfg"
"github.com/felixangell/phi/gui"
"github.com/felixangell/phi/lex"
"github.com/felixangell/phi/piecetable"
"github.com/felixangell/phi/internal/cfg"
"github.com/felixangell/phi/internal/gui"
"github.com/felixangell/phi/internal/lex"
"github.com/felixangell/phi/pkg/piecetable"
"github.com/felixangell/strife"
"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/veandco/go-sdl2/sdl"

View File

@ -5,8 +5,8 @@ import (
"log"
"path/filepath"
"github.com/felixangell/phi/cfg"
"github.com/felixangell/phi/gui"
"github.com/felixangell/phi/internal/cfg"
"github.com/felixangell/phi/internal/gui"
"github.com/felixangell/strife"
)

View File

@ -3,7 +3,7 @@ package buff
import (
"fmt"
"github.com/felixangell/phi/lex"
"github.com/felixangell/phi/internal/lex"
)
func CloseBuffer(v *BufferView, commands []*lex.Token) BufferDirtyState {

View File

@ -1,7 +1,7 @@
package buff
import (
"github.com/felixangell/phi/cfg"
"github.com/felixangell/phi/internal/cfg"
"github.com/felixangell/strife"
)

View File

@ -1,6 +1,6 @@
package buff
import "github.com/felixangell/phi/lex"
import "github.com/felixangell/phi/internal/lex"
func DeleteLine(v *BufferView, _ []*lex.Token) BufferDirtyState {
b := v.getCurrentBuff()

View File

@ -4,7 +4,7 @@ import (
"log"
"os"
"github.com/felixangell/phi/lex"
"github.com/felixangell/phi/internal/lex"
)
func ExitPhi(v *BufferView, _ []*lex.Token) BufferDirtyState {

View File

@ -4,9 +4,9 @@ import (
"log"
"strings"
"github.com/felixangell/phi/cfg"
"github.com/felixangell/phi/gui"
"github.com/felixangell/phi/lex"
"github.com/felixangell/phi/internal/cfg"
"github.com/felixangell/phi/internal/gui"
"github.com/felixangell/phi/internal/lex"
"github.com/felixangell/strife"
"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/veandco/go-sdl2/sdl"

View File

@ -10,7 +10,7 @@ import (
"path/filepath"
"github.com/atotto/clipboard"
"github.com/felixangell/phi/lex"
"github.com/felixangell/phi/internal/lex"
)
func ShowPalette(v *BufferView, _ []*lex.Token) BufferDirtyState {

View File

@ -6,9 +6,9 @@ import (
"runtime"
"unicode"
"github.com/felixangell/phi/cfg"
"github.com/felixangell/phi/gui"
"github.com/felixangell/phi/lex"
"github.com/felixangell/phi/internal/cfg"
"github.com/felixangell/phi/internal/gui"
"github.com/felixangell/phi/internal/lex"
"github.com/felixangell/strife"
"github.com/fsnotify/fsnotify"
"github.com/veandco/go-sdl2/sdl"

60
internal/editor/editor.go Normal file
View File

@ -0,0 +1,60 @@
package editor
import (
"github.com/felixangell/phi/internal/buff"
"github.com/felixangell/phi/internal/cfg"
"github.com/felixangell/strife"
"io/ioutil"
"log"
"os"
)
type PhiEditor struct {
running bool
defaultFont *strife.Font
mainView *buff.BufferView
}
func NewPhiEditor() *PhiEditor {
return &PhiEditor{running: true}
}
func (n *PhiEditor) Resize(w, h int) {
n.mainView.Resize(w, h)
}
func (n *PhiEditor) HandleEvent(_ strife.StrifeEvent) {}
func (n *PhiEditor) ApplyConfig(conf *cfg.TomlConfig) {
mainView := buff.NewView(int(1280.0*cfg.ScaleFactor), int(720.0*cfg.ScaleFactor), conf)
args := os.Args
if len(args) > 1 {
// TODO check these are files
// that actually exist here?
for _, arg := range args[1:] {
mainView.AddBuffer().OpenFile(arg)
}
} else {
// we have no args, open up a scratch file
tempFile, err := ioutil.TempFile("", "phi-editor-")
if err != nil {
log.Println("Failed to create temp file", err.Error())
os.Exit(1)
}
mainView.AddBuffer().OpenFile(tempFile.Name())
}
n.mainView = mainView
n.defaultFont = conf.Editor.LoadedFont
}
func (n *PhiEditor) Update() bool {
return n.mainView.OnUpdate()
}
func (n *PhiEditor) Render(ctx *strife.Renderer) {
ctx.SetFont(n.defaultFont)
n.mainView.OnRender(ctx)
}

184
main.go
View File

@ -1,184 +0,0 @@
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"time"
"github.com/felixangell/phi/buff"
"github.com/felixangell/phi/cfg"
"github.com/felixangell/strife"
)
const (
PRINT_FPS bool = true
)
type PhiEditor struct {
running bool
defaultFont *strife.Font
mainView *buff.BufferView
}
func (n *PhiEditor) resize(w, h int) {
n.mainView.Resize(w, h)
}
func (n *PhiEditor) handleEvent(evt strife.StrifeEvent) {
}
func (n *PhiEditor) init(conf *cfg.TomlConfig) {
mainView := buff.NewView(int(1280.0*cfg.ScaleFactor), int(720.0*cfg.ScaleFactor), conf)
args := os.Args
if len(args) > 1 {
// TODO check these are files
// that actually exist here?
for _, arg := range args[1:] {
mainView.AddBuffer().OpenFile(arg)
}
} else {
// we have no args, open up a scratch file
tempFile, err := ioutil.TempFile("", "phi-editor-")
if err != nil {
log.Println("Failed to create temp file", err.Error())
os.Exit(1)
}
mainView.AddBuffer().OpenFile(tempFile.Name())
}
n.mainView = mainView
n.defaultFont = conf.Editor.LoadedFont
}
func (n *PhiEditor) dispose() {
}
func (n *PhiEditor) update() bool {
return n.mainView.OnUpdate()
}
func (n *PhiEditor) render(ctx *strife.Renderer) {
ctx.SetFont(n.defaultFont)
n.mainView.OnRender(ctx)
}
func main() {
runtime.LockOSThread()
config := cfg.Setup()
windowConfig := strife.DefaultConfig()
windowConfig.Accelerated = config.Render.Accelerated
windowConfig.Alias = config.Render.Aliased
windowConfig.VerticalSync = config.Render.VerticalSync
ww, wh := float32(640.0), float32(360.0)
dpi, defDpi := strife.GetDisplayDPI(0)
cfg.ScaleFactor = float64(dpi / defDpi)
scaledWidth := int((ww * dpi) / defDpi)
scaledHeight := int((wh * dpi) / defDpi)
window := strife.SetupRenderWindow(scaledWidth, scaledHeight, windowConfig)
window.AllowHighDPI()
window.SetTitle("Hello world!")
window.SetResizable(true)
editor := &PhiEditor{running: true}
window.HandleEvents(func(evt strife.StrifeEvent) {
switch event := evt.(type) {
case *strife.CloseEvent:
window.Close()
case *strife.WindowResizeEvent:
fmt.Println("window resize is unimplemented: size", event.Width, event.Height)
default:
editor.handleEvent(evt)
}
})
if err := window.Create(); err != nil {
panic(err)
}
{
size := 16
switch runtime.GOOS {
case "windows":
size = 64
case "darwin":
size = 512
case "linux":
size = 96
default:
log.Println("unrecognized runtime ", runtime.GOOS)
}
iconFile := fmt.Sprintf("icon%d.png", size)
icon, err := strife.LoadImage(filepath.Join(cfg.IconDirPath, iconFile))
if err != nil {
log.Println("Failed to load icon ", err.Error())
} else {
window.SetIconImage(icon)
defer icon.Destroy()
}
}
editor.init(&config)
lastDebugRender := time.Now()
frames, updates := 0, 0
fps, ups := frames, updates
ctx := window.GetRenderContext()
ctx.Clear()
editor.render(ctx)
ctx.Display()
for {
window.PollEvents()
if window.CloseRequested() {
break
}
shouldRender := editor.update()
if shouldRender || config.Render.AlwaysRender {
ctx.Clear()
editor.render(ctx)
// this is only printed on each
// render...
ctx.SetColor(strife.White)
ctx.Text(fmt.Sprintf("fps: %d, ups %d", fps, ups), int(scaledWidth-256), int(scaledHeight-128))
ctx.Display()
frames++
}
updates++
if time.Now().Sub(lastDebugRender) >= time.Second {
lastDebugRender = time.Now()
fps, ups = frames, updates
frames, updates = 0, 0
}
if config.Render.ThrottleCpuUsage {
// todo put in the config how long
// we sleep for!
time.Sleep(16)
}
}
editor.dispose()
}