removed all the config related shit; starting to make it so that we load

with defaults and pick up config files instead rather than building them
on startup
This commit is contained in:
Felix Angell 2021-01-09 13:40:30 +00:00
parent dc930e115f
commit 9c9053304f
7 changed files with 11 additions and 647 deletions

View File

@ -189,7 +189,7 @@ type selection struct {
ex, ey int
}
func (s *selection) renderAt(ctx *strife.Renderer, xOff int, yOff int) {
func (s *selection) renderAt(ctx *strife.Renderer, _ int, _ int) {
ctx.SetColor(strife.Blue)
yd := (s.ey - s.sy) + 1

View File

@ -126,7 +126,7 @@ type EditorConfig struct {
}
func NewDefaultConfig() *TomlConfig {
log.Println("Loading default configuration... this should never happen")
log.Println("Loading default configuration")
return &TomlConfig{
Editor: &EditorConfig{},
Theme: &ThemeConfig{
@ -135,5 +135,8 @@ func NewDefaultConfig() *TomlConfig {
Cursor: 0xf2f4f6,
CursorInvert: 0xffffff,
},
// TODO syntax defaults
associations: map[string]*LanguageSyntaxConfig{},
}
}

View File

@ -1,102 +0,0 @@
// +build !linux,!darwin
package cfg
var DEFUALT_TOML_CONFIG = `[editor]
tab_size = 4
hungry_backspace = true
tabs_are_spaces = true
match_braces = false
maintain_indentation = true
highlight_line = true
font_face = "Consola"
font_size = 20
show_line_numbers = true
[render]
aliased = true
accelerated = true
throttle_cpu_usage = true
vertical_sync = false
always_render = true
syntax_highlighting = true
[file_associations]
[file_associations.toml]
extensions = [".toml"]
[file_associations.c]
extensions = [".c", ".h", ".cc"]
[file_associations.go]
extensions = [".go"]
[file_associations.md]
extensions = [".md"]
[theme]
background = 0x002649
foreground = 0xf2f4f6
cursor = 0xf2f4f6
cursor_invert = 0x000000
gutter_background = 0x002649
gutter_foreground = 0xf2f4f6
highlight_line_background = 0x000000
[theme.palette]
outline = 0xebedef
background = 0xffffff
foreground = 0x000000
cursor = 0xf2f4f6
render_shadow = true
shadow_color = 0x000000
[theme.palette.suggestion]
background = 0xebedef
foreground = 0x3a3839
selected_background = 0xc7cbd1
selected_foreground = 0x3a3839
[cursor]
flash_rate = 400
reset_delay = 400
draw = true
flash = true
[commands]
[commands.undo]
shortcut = "ctrl+z"
[commands.redo]
shortcut = "ctrl+y"
[commands.save]
shortcut = "ctrl+s"
[commands.page_down]
shortcut = "pgdown"
[commands.page_up]
shortcut = "pgup"
[commands.focus_left]
shortcut = "ctrl+left"
[commands.focus_right]
shortcut = "ctrl+right"
[commands.show_palette]
shortcut = "ctrl+p"
[commands.paste]
shortcut = "ctrl+v"
[commands.exit]
shortcut = "ctrl+q"
[commands.close_buffer]
shortcut = "ctrl+w"
[commands.delete_line]
shortcut = "ctrl+d"
`

View File

@ -1,102 +0,0 @@
// +build linux
package cfg
var DEFUALT_TOML_CONFIG = `[editor]
tab_size = 4
hungry_backspace = true
tabs_are_spaces = true
match_braces = false
maintain_indentation = true
highlight_line = true
font_face = "Courier"
font_size = 20
show_line_numbers = true
[render]
aliased = true
accelerated = true
throttle_cpu_usage = true
vertical_sync = false
always_render = true
syntax_highlighting = true
[file_associations]
[file_associations.toml]
extensions = [".toml"]
[file_associations.c]
extensions = [".c", ".h", ".cc"]
[file_associations.go]
extensions = [".go"]
[file_associations.md]
extensions = [".md"]
[theme]
background = 0x002649
foreground = 0xf2f4f6
cursor = 0xf2f4f6
cursor_invert = 0x000000
gutter_background = 0x002649
gutter_foreground = 0xf2f4f6
highlight_line_background = 0x000000
[theme.palette]
outline = 0xebedef
background = 0xffffff
foreground = 0x000000
cursor = 0xf2f4f6
render_shadow = true
shadow_color = 0x000000
[theme.palette.suggestion]
background = 0xebedef
foreground = 0x3a3839
selected_background = 0xc7cbd1
selected_foreground = 0x3a3839
[cursor]
flash_rate = 400
reset_delay = 400
draw = true
flash = true
[commands]
[commands.undo]
shortcut = "ctrl+z"
[commands.redo]
shortcut = "ctrl+y"
[commands.exit]
shortcut = "ctrl+q"
[commands.page_down]
shortcut = "pgdown"
[commands.page_up]
shortcut = "pgup"
[commands.show_palette]
shortcut = "ctrl+p"
[commands.focus_left]
shortcut = "ctrl+left"
[commands.focus_right]
shortcut = "ctrl+right"
[commands.save]
shortcut = "ctrl+s"
[commands.paste]
shortcut = "ctrl+v"
[commands.close_buffer]
shortcut = "ctrl+w"
[commands.delete_line]
shortcut = "ctrl+d"
`

View File

@ -1,102 +0,0 @@
// +build darwin
package cfg
var DEFUALT_TOML_CONFIG = `[editor]
tab_size = 4
hungry_backspace = true
tabs_are_spaces = true
match_braces = false
maintain_indentation = true
highlight_line = true
font_face = "Arial Unicode"
font_size = 20
show_line_numbers = true
[render]
aliased = true
accelerated = true
vertical_sync = false
throttle_cpu_usage = true
always_render = true
syntax_highlighting = true
[file_associations]
[file_associations.toml]
extensions = [".toml"]
[file_associations.c]
extensions = [".c", ".h", ".cc"]
[file_associations.go]
extensions = [".go"]
[file_associations.md]
extensions = [".md"]
[theme]
background = 0x002649
foreground = 0xf2f4f6
cursor = 0xf2f4f6
cursor_invert = 0x000000
gutter_background = 0x002649
gutter_foreground = 0xf2f4f6
highlight_line_background = 0x000000
[theme.palette]
outline = 0xebedef
background = 0xffffff
foreground = 0x000000
cursor = 0xf2f4f6
render_shadow = true
shadow_color = 0x000000
[theme.palette.suggestion]
background = 0xebedef
foreground = 0x3a3839
selected_background = 0xc7cbd1
selected_foreground = 0x3a3839
[cursor]
flash_rate = 400
reset_delay = 400
draw = true
flash = true
[commands]
[commands.undo]
shortcut = "super+z"
[commands.redo]
shortcut = "super+y"
[commands.exit]
shortcut = "super+q"
[commands.save]
shortcut = "super+s"
[commands.page_down]
shortcut = "ctrl+down"
[commands.page_up]
shortcut = "ctrl+up"
[commands.show_palette]
shortcut = "super+p"
[commands.focus_left]
shortcut = "super+left"
[commands.focus_right]
shortcut = "super+right"
[commands.paste]
shortcut = "super+v"
[commands.close_buffer]
shortcut = "super+w"
[commands.delete_line]
shortcut = "super+d"
`

View File

@ -1,335 +0,0 @@
package cfg
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/felixangell/strife"
"github.com/pelletier/go-toml"
)
// TODO:
// - make the $HOME/.phi-editor folder if it doesn't exist
// - make the $HOME/.phi-editor/config.toml file if it doesn't exist
// - write a default toml file
//
const (
ConfigDirPath = "/.phi-editor/"
ConfigTomlFile = "config.toml"
)
var FontFolder = ""
// this is the absolute path to the
// config.toml file. todo rename/refactor
var ConfigFullPath = ""
// the absolute path to the config directory
// rename/refactor due here too!
var configDirAbsPath = ""
var IconDirPath = ""
// TODO we only had double key combos
// e.g. cmd+s. we want to handle things
// like cmd+alt+s
type shortcutRegister struct {
Supers map[string]string
Controls map[string]string
}
var Shortcuts = &shortcutRegister{
Supers: map[string]string{},
Controls: map[string]string{},
}
func loadSyntaxDef(lang string) *LanguageSyntaxConfig {
languagePath := filepath.Join(configDirAbsPath, "syntax", lang+".toml")
log.Println("Loading lang from ", languagePath)
syntaxTomlData, err := ioutil.ReadFile(languagePath)
if err != nil {
log.Println("Failed to load highlighting for language '"+lang+"' from path: ", languagePath)
return nil
}
conf := &LanguageSyntaxConfig{}
if err := toml.Unmarshal(syntaxTomlData, conf); err != nil {
panic(err)
}
log.Println("Loaded syntax definition for language", lang)
return conf
}
func findFontFolder() string {
// TODO
return "/usr/share/fonts/"
}
func configureAndValidate(conf *TomlConfig) {
// fonts
log.Println("Configuring fonts")
{
// the font path has not been set
// so we have to figure out what it is.
if len(conf.Editor.FontPath) == 0 {
switch runtime.GOOS {
case "windows":
FontFolder = filepath.Join(os.Getenv("WINDIR"), "fonts")
case "darwin":
FontFolder = "/Library/Fonts/"
case "linux":
FontFolder = findFontFolder()
}
// and set it accordingly.
conf.Editor.FontPath = FontFolder
}
// we only support ttf at the moment.
fontPath := filepath.Join(conf.Editor.FontPath, conf.Editor.FontFace) + ".ttf"
if _, err := os.Stat(fontPath); os.IsNotExist(err) {
log.Fatal("No such font '" + fontPath + "'")
}
// load the font!
font, err := strife.LoadFont(fontPath, conf.Editor.FontSize)
if err != nil {
panic(err)
}
conf.Editor.LoadedFont = font
}
// config & validate the keyboard shortcuts
log.Println("Configuring keyboard shortcuts")
{
// keyboard commands
for commandName, cmd := range conf.Commands {
shortcut := cmd.Shortcut
vals := strings.Split(shortcut, "+")
// TODO handle conflicts
switch vals[0] {
case "super":
Shortcuts.Supers[vals[1]] = commandName
case "ctrl":
Shortcuts.Controls[vals[1]] = commandName
}
}
}
log.Println("Syntax Highlighting")
{
var syntaxSet []*LanguageSyntaxConfig
conf.associations = map[string]*LanguageSyntaxConfig{}
for lang, extSet := range conf.Associations {
log.Println(lang, "=>", extSet.Extensions)
languageConfig := loadSyntaxDef(lang)
// check for errors here
syntaxSet = append(syntaxSet, languageConfig)
for _, ext := range extSet.Extensions {
log.Println("registering", ext, "as", lang)
conf.associations[ext] = languageConfig
}
}
// go through each language
// and store the matches keywords
// as a hashmap for faster lookup
// in addition to this we compile any
// regular expressions if necessary.
for _, language := range syntaxSet {
for _, syn := range language.Syntax {
syn.MatchList = map[string]bool{}
if syn.Pattern != "" {
regex, err := regexp.Compile(syn.Pattern)
if err != nil {
log.Println(err.Error())
continue
}
syn.CompiledPattern = regex
} else {
for _, item := range syn.Match {
if _, ok := syn.MatchList[item]; ok {
log.Println("Warning duplicate match item '" + item + "'")
continue
}
syn.MatchList[item] = true
}
}
}
}
}
}
func Setup() TomlConfig {
log.Println("Setting up Phi Editor")
home := os.Getenv("HOME")
if runtime.GOOS == "windows" {
home = os.Getenv("USERPROFILE")
}
ConfigDir := filepath.Join(home, ConfigDirPath)
configDirAbsPath = ConfigDir
ConfigPath := filepath.Join(ConfigDir, ConfigTomlFile)
// this folder is where we store all of the language syntax
SyntaxConfigDir := filepath.Join(ConfigDir, "syntax")
ConfigFullPath = ConfigPath
// if the user doesn't have a /.phi-editor
// directory we create it for them.
if _, err := os.Stat(ConfigDir); os.IsNotExist(err) {
if err := os.Mkdir(ConfigDir, 0775); err != nil {
panic(err)
}
}
// ----
// downloads the icon from github
// and puts it into the phi-editor config folder.
IconDirPath = filepath.Join(ConfigDir, "icons")
if _, err := os.Stat(IconDirPath); os.IsNotExist(err) {
if err := os.Mkdir(IconDirPath, 0775); err != nil {
panic(err)
}
log.Println("setting up the icons folder")
// https://raw.githubusercontent.com/felixangell/phi/gh-pages/images/icon128.png
downloadIcon := func(iconSize int) {
log.Println("downloading the phi icon ", iconSize, "x", iconSize, " png image.")
file, err := os.Create(filepath.Join(IconDirPath, fmt.Sprintf("icon%d.png", iconSize)))
if err != nil {
log.Println(err.Error())
return
}
defer func() {
if err := file.Close(); err != nil {
panic(err)
}
}()
// Generated by curl-to-Go: https://mholt.github.io/curl-to-go
resp, err := http.Get(fmt.Sprintf("https://raw.githubusercontent.com/felixangell/phi/gh-pages/images/icon%d.png", iconSize))
if err != nil {
log.Println("Failed to download icon", iconSize, "!", err.Error())
return
}
defer func() {
if err := resp.Body.Close(); err != nil {
panic(err)
}
}()
_, err = io.Copy(file, resp.Body)
if err != nil {
log.Println(err.Error())
}
}
size := getIconSize()
// download the icon and
// write it to the phi-editor folder.
downloadIcon(size)
}
// try make the syntax config folder.
if _, err := os.Stat(SyntaxConfigDir); os.IsNotExist(err) {
if err := os.Mkdir(SyntaxConfigDir, 0775); err != nil {
panic(err)
}
// load all of the default language syntax
for name, syntaxDef := range DefaultSyntaxSet {
languagePath := filepath.Join(SyntaxConfigDir, name+".toml")
if _, err := os.Stat(languagePath); os.IsNotExist(err) {
file, err := os.Create(languagePath)
if err != nil {
panic(err)
}
if _, err := file.Write([]byte(syntaxDef)); err != nil {
panic(err)
}
log.Println("Wrote syntax for language '" + name + "'")
if err := file.Close(); err != nil {
panic(err)
}
}
}
}
// make sure a config.toml file exists in the
// phi-editor directory.
if _, err := os.Stat(ConfigPath); os.IsNotExist(err) {
configFile, fileCreateErr := os.Create(ConfigPath)
if fileCreateErr != nil {
panic(fileCreateErr)
}
defer func() {
if err := configFile.Close(); err != nil {
panic(err)
}
}()
_, writeErr := configFile.Write([]byte(DEFUALT_TOML_CONFIG))
if writeErr != nil {
panic(writeErr)
}
if err := configFile.Sync(); err != nil {
panic(err)
}
}
if _, err := os.Open(ConfigPath); err != nil {
panic(err)
}
configTomlData, err := ioutil.ReadFile(ConfigPath)
if err != nil {
panic(err)
}
conf := TomlConfig{}
if err := toml.Unmarshal(configTomlData, &conf); err != nil {
panic(err)
}
configureAndValidate(&conf)
return conf
}
func getIconSize() int {
size := 16
switch runtime.GOOS {
case "windows":
size = 64
case "darwin":
size = 512
case "linux":
size = 96
}
return size
}

View File

@ -4,12 +4,14 @@ type LanguageSyntaxConfig struct {
Syntax map[string]*SyntaxCriteria `toml:"syntax"`
}
type DefaultSyntax map[string]string
func NewLanguageSyntaxConfig() *LanguageSyntaxConfig {
return &LanguageSyntaxConfig{Syntax: map[string]*SyntaxCriteria{}}
}
var DefaultSyntaxSet = DefaultSyntax{}
var DefaultSyntaxSet = map[string]string{}
func RegisterSyntax(name string, s string) {
DefaultSyntaxSet[name] = s
func RegisterSyntax(name string, syntaxTomlDef string) {
DefaultSyntaxSet[name] = syntaxTomlDef
}
func init() {