Skip to content

Commit

Permalink
json config; fix decrease font; save font size; better cur_fn placeme…
Browse files Browse the repository at this point in the history
…nt; ~ for home in file path
  • Loading branch information
medvednikov committed Nov 6, 2024
1 parent 029ccdb commit 1ae38e8
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 176 deletions.
2 changes: 1 addition & 1 deletion TODO.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- list of all open files (similar to ctrl p)
+ list of all open files (similar to ctrl p)
- use ui.textbox
- print error message on the actual line
- remember line pos for each file on t
171 changes: 91 additions & 80 deletions config.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module main

import os
import gx
import toml
// import toml
import json

// The different kinds of cursors
Expand All @@ -19,16 +19,16 @@ enum Cursor {
// TODO: Load user config from file
struct Config {
mut:
settings toml.Doc
// settings toml.Doc
dark_mode bool
cursor_style Cursor
text_size int
line_height int
char_width int
tab_size int
tab int
text_size int = min_text_size
line_height int = 20
char_width int = 8
tab_size int = 4
tab int = int(`\t`) // TODO read from config file?
backspace_go_up bool
vcolor gx.Color
vcolor gx.Color // v selection background color
split_color gx.Color
bgcolor gx.Color // base00
errorbgcolor gx.Color // base08
Expand Down Expand Up @@ -56,25 +56,39 @@ mut:
green_cfg gx.TextCfg
red_color gx.Color // base08
red_cfg gx.TextCfg
disable_mouse bool
disable_fmt bool
disable_mouse bool = true
// Config.json
disable_fmt bool
}

/*
// json
struct Config2 {
disable_fmt bool
text_size int
line_height int
char_width int
}
*/

/*
fn (mut config Config) set_settings(path string) {
config.settings = toml.parse_file(path) or { toml.parse_text('') or { panic(err) } }
}
*/

// reload_config reloads the config from config.toml file
fn (mut config Config) reload_config() {
// set_default_color_values?
fn (mut config Config) set_default_values() {
config.init_colors()

config.set_cursor_style()
config.set_text_size()
config.set_line_height()
config.set_char_width()
config.set_tab()
config.set_backspace_behaviour()
config.set_disable_mouse()
// config.set_text_size()
// config.set_line_height()
// config.set_char_width()
// config.set_tab()
// config.set_backspace_behaviour()
// config.set_disable_mouse()
config.set_vcolor()
config.set_split()
config.set_bgcolor()
Expand All @@ -95,11 +109,11 @@ fn (mut config Config) reload_config() {
}

fn (mut config Config) init_colors() {
toml_dark_mode := config.settings.value('editor.dark_mode').bool()
config.dark_mode = toml_dark_mode || '-dark' in os.args
config.dark_mode ||= '-dark' in os.args
}

fn (mut config Config) set_cursor_style() {
/*
toml_cursor_style := config.settings.value('editor.cursor').string()
if toml_cursor_style != 'toml.Any(toml.Null{})' {
match toml_cursor_style {
Expand All @@ -110,44 +124,14 @@ fn (mut config Config) set_cursor_style() {
}
return
}
*/
config.cursor_style = .block
}

fn (mut config Config) set_text_size() {
toml_text_size := config.settings.value('editor.text_size').int()
config.text_size = if toml_text_size > 0 { toml_text_size } else { 18 }
}

fn (mut config Config) set_line_height() {
toml_line_height := config.settings.value('editor.line_height').int()
config.line_height = if toml_line_height > 0 { toml_line_height } else { 20 }
}

fn (mut config Config) set_char_width() {
toml_char_width := config.settings.value('editor.char_width').int()
config.char_width = if toml_char_width > 0 { toml_char_width } else { 8 }
}

fn (mut config Config) set_tab() {
toml_tab_size := config.settings.value('editor.tab_size').int()
config.tab_size = if toml_tab_size > 0 { toml_tab_size } else { 4 }

// TODO: read this in from the config file
config.tab = int(`\t`)
}

fn (mut config Config) set_backspace_behaviour() {
toml_backspace_behaviour := config.settings.value('editor.backspace_go_up').bool()
config.backspace_go_up = toml_backspace_behaviour
}

fn (mut config Config) set_disable_mouse() {
config.disable_mouse = config.settings.value('editor.disable_mouse').bool()
config.disable_mouse = true // TODO remove once mouse bugs are fixed
}

// Convert a toml key color (in hex) to a gx.Color type
/*
fn (config Config) get_toml_color(base string) !gx.Color {
toml_hex := config.settings.value('colors.base${base}').string()
if toml_hex != 'toml.Any(toml.Null{})' {
toml_red := ('0x' + toml_hex[0..2]).u8()
Expand All @@ -159,6 +143,7 @@ fn (config Config) get_toml_color(base string) !gx.Color {
return error('Couldn\'t read base${base} from the settings file')
}
*/

fn (mut config Config) set_vcolor() {
if !config.dark_mode {
Expand All @@ -178,24 +163,25 @@ fn (mut config Config) set_split() {

// base 00
fn (mut config Config) set_bgcolor() {
config.bgcolor = config.get_toml_color('00') or {
if config.dark_mode {
gx.rgb(30, 30, 30)
} else {
gx.rgb(245, 245, 245)
}
// config.bgcolor = config.get_toml_color('00') or {
config.bgcolor = if config.dark_mode {
gx.rgb(30, 30, 30)
} else {
gx.rgb(245, 245, 245)
}
//}
}

// base 01
fn (mut config Config) set_errorbgcolor() {
config.errorbgcolor = config.get_toml_color('01') or { gx.rgb(240, 0, 0) }
// config.errorbgcolor = config.get_toml_color('01') or { gx.rgb(240, 0, 0) }
config.errorbgcolor = gx.rgb(240, 0, 0)
}

// base 0B
fn (mut config Config) set_string() {
config.string_color = config.get_toml_color('0B') or { gx.rgb(179, 58, 44) }

// config.string_color = config.get_toml_color('0B') or { gx.rgb(179, 58, 44) }
config.string_color = gx.rgb(179, 58, 44)
config.string_cfg = gx.TextCfg{
size: config.text_size
color: config.string_color
Expand All @@ -204,7 +190,8 @@ fn (mut config Config) set_string() {

// base 0E
fn (mut config Config) set_key() {
config.key_color = config.get_toml_color('0E') or { gx.rgb(74, 103, 154) }
// config.key_color = config.get_toml_color('0E') or { gx.rgb(74, 103, 154) }
config.key_color = gx.rgb(74, 103, 154)

config.key_cfg = gx.TextCfg{
size: config.text_size
Expand All @@ -214,7 +201,8 @@ fn (mut config Config) set_key() {

// base 0F
fn (mut config Config) set_lit() {
config.lit_color = config.get_toml_color('0F') or { gx.rgb(7, 103, 154) }
// config.lit_color = config.get_toml_color('0F') or { gx.rgb(7, 103, 154) }
config.lit_color = gx.rgb(7, 103, 154)

config.lit_cfg = gx.TextCfg{
size: config.text_size
Expand All @@ -225,28 +213,27 @@ fn (mut config Config) set_lit() {
// base 04
fn (mut config Config) set_title() {
// config.title_color = config.get_toml_color('04') or { gx.rgb(40, 40, 40) }
config.title_color = config.get_toml_color('04') or { gx.rgb(0, 0, 0) }
config.title_color = gx.rgb(0, 0, 0)
}

// base 05
fn (mut config Config) set_cursor() {
config.cursor_color = config.get_toml_color('05') or {
if !config.dark_mode {
gx.black
} else {
gx.white
}
// config.cursor_color = config.get_toml_color('05') or {
config.cursor_color = if !config.dark_mode {
gx.black
} else {
gx.white
}
//}
}

// base 05 (again)
fn (mut config Config) set_txt() {
config.text_color = config.get_toml_color('05') or {
if !config.dark_mode {
gx.black
} else {
gx.rgb(212, 212, 212)
}
// config.text_color = config.get_toml_color('05') or {
config.text_color = if !config.dark_mode {
gx.black
} else {
gx.rgb(212, 212, 212)
}

config.txt_cfg = gx.TextCfg{
Expand All @@ -257,7 +244,8 @@ fn (mut config Config) set_txt() {

// base 03
fn (mut config Config) set_comment() {
config.comment_color = config.get_toml_color('03') or { gx.dark_gray }
// config.comment_color = config.get_toml_color('03') or { gx.dark_gray }
config.comment_color = gx.dark_gray

config.comment_cfg = gx.TextCfg{
size: config.text_size
Expand Down Expand Up @@ -291,7 +279,8 @@ fn (mut config Config) set_minus() {

// base 01
fn (mut config Config) set_line_nr() {
config.line_nr_color = config.get_toml_color('01') or { gx.dark_gray }
// config.line_nr_color = config.get_toml_color('01') or { gx.dark_gray }
config.line_nr_color = gx.dark_gray

config.line_nr_cfg = gx.TextCfg{
size: config.text_size
Expand All @@ -302,7 +291,8 @@ fn (mut config Config) set_line_nr() {

// base 0B
fn (mut config Config) set_green() {
config.green_color = config.get_toml_color('0B') or { gx.green }
// config.green_color = config.get_toml_color('0B') or { gx.green }
config.green_color = gx.green

config.green_cfg = gx.TextCfg{
size: config.text_size
Expand All @@ -311,7 +301,8 @@ fn (mut config Config) set_green() {
}

fn (mut config Config) set_red() {
config.red_color = config.get_toml_color('08') or { gx.red }
// config.red_color = config.get_toml_color('08') or { gx.red }
config.red_color = gx.red
config.red_cfg = gx.TextCfg{
size: config.text_size
color: config.red_color
Expand All @@ -321,9 +312,29 @@ fn (mut config Config) set_red() {
fn (mut ved Ved) load_config2() {
if os.exists(config_path2) {
if conf2 := json.decode(Config, os.read_file(config_path2) or { return }) {
println('AXAXAXAXA ${conf2}')
ved.cfg = conf2
/*
ved.cfg.disable_fmt = conf2.disable_fmt
ved.cfg.text_size = conf2.text_size
ved.cfg.line_height = conf2.line_height
ved.cfg.char_width = conf2.char_width
*/
// ved.cfg.disable_fmt = conf2.disable_fmt
} else {
println(err)
}
}
}

fn (mut ved Ved) save_config2() {
/*
config2 := Config2{
text_size: ved.cfg.text_size
disable_fmt: ved.cfg.disable_fmt
line_height: ved.cfg.line_height
char_width: ved.cfg.char_width
}
*/
os.write_file(config_path2, json.encode_pretty(ved.cfg)) or { panic(err) }
}
Loading

0 comments on commit 1ae38e8

Please sign in to comment.