phi/cfg/config.go

129 lines
2.8 KiB
Go
Raw Normal View History

2016-11-14 10:24:19 +03:00
package cfg
import (
"errors"
"log"
"runtime"
"strconv"
"strings"
)
2016-11-14 11:14:53 +03:00
type TomlConfig struct {
Editor EditorConfig `toml:"editor"`
Cursor CursorConfig `toml:"cursor"`
Render RenderConfig `toml:"render"`
Theme ThemeConfig `toml:"theme"`
Associations map[string]FileAssociations `toml:"file_associations"`
Commands map[string]Command `toml:"commands"`
Syntax map[string]map[string]SyntaxCriteria `toml:"syntax"`
// this maps ext => language
// when we have file associations from
// the Associations field we take
// each extension and put them here
// pointing it to the language.
// basically the reverse/opposite
associations map[string]string
}
func (t *TomlConfig) GetLanguageFromExt(ext string) (string, error) {
if val, ok := t.associations[ext]; ok {
return val, nil
}
return "", errors.New("no language for extension '" + ext + "'")
}
type FileAssociations struct {
Extensions []string
}
var DEFUALT_TOML_CONFIG string = getDefaultConfig()
func getDefaultConfig() string {
switch strings.ToLower(runtime.GOOS) {
case "windows":
return DEFAULT_WINDOWS_TOML_CONFIG
case "linux":
return DEFAULT_LINUX_TOML_CONFIG
case "darwin":
return DEFAULT_MAC_TOML_CONFIG
}
// fallback is a windows config.
return DEFAULT_WINDOWS_TOML_CONFIG
}
2016-11-27 20:21:03 +03:00
type SyntaxCriteria struct {
2018-04-14 03:57:01 +03:00
Colour int `toml:"colouring"`
Match []string `toml:"match"`
Pattern string `toml:"pattern"`
}
type Command struct {
Shortcut string
}
type CursorConfig struct {
Flash_Rate int64
Reset_Delay int64
Draw bool
Flash bool
Block_Width string
}
func (c CursorConfig) GetCaretWidth() int {
if c.Block_Width == "block" {
return -1
}
if c.Block_Width == "" {
return -1
}
value, err := strconv.ParseInt(c.Block_Width, 10, 32)
if err != nil {
panic(err)
}
return int(value)
}
type RenderConfig struct {
2018-02-28 16:23:07 +03:00
Aliased bool
Accelerated bool
Throttle_Cpu_Usage bool
Always_Render bool
}
// todo make this more extendable...
// e.g. .phi-editor/themes with TOML
// themes in them and we can select
// the default theme in the EditorConfig
// instead.
type ThemeConfig struct {
Background int32
Foreground int32
Cursor int32
Cursor_Invert int32
2016-11-14 11:14:53 +03:00
}
type EditorConfig struct {
Tab_Size int
Hungry_Backspace bool
Tabs_Are_Spaces bool
Match_Braces bool
Maintain_Indentation bool
Highlight_Line bool
2016-11-14 10:24:19 +03:00
}
2016-11-14 11:14:53 +03:00
func NewDefaultConfig() *TomlConfig {
log.Println("Loading default configuration... this should never happen")
2016-11-14 11:14:53 +03:00
return &TomlConfig{
Editor: EditorConfig{},
Theme: ThemeConfig{
Background: 0x002649,
Foreground: 0xf2f4f6,
Cursor: 0xf2f4f6,
Cursor_Invert: 0xffffff,
},
2016-11-14 10:24:19 +03:00
}
}