From 8b7cd98a0eb482a5fa26daef90be7db5fd1be64d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 28 Oct 2024 14:01:42 +0530 Subject: [PATCH] When running a kitten that modifies the kitty config file if no config file exists create a commented out default config file and then modify it Fixes #7991 --- docs/changelog.rst | 2 ++ gen/go_code.py | 2 ++ tools/config/api.go | 25 ++++++++++++++++++++----- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 1fdb77f4a..5c44d8e7a 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -93,6 +93,8 @@ Detailed list of changes - Fix background image flashing when closing a tab (:iss:`7999`) +- When running a kitten that modifies the kitty config file if no config file exists create a commented out default config file and then modify it (:iss:`7991`) + 0.36.4 [2024-09-27] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/gen/go_code.py b/gen/go_code.py index 32bf2aec6..5610811c1 100755 --- a/gen/go_code.py +++ b/gen/go_code.py @@ -37,6 +37,7 @@ from kitty.cli import ( ) from kitty.conf.generate import gen_go_code from kitty.conf.types import Definition +from kitty.config import commented_out_default_config from kitty.guess_mime_type import known_extensions, text_mimes from kitty.key_encoding import config_mod_map from kitty.key_names import character_key_name_aliases, functional_key_name_aliases @@ -626,6 +627,7 @@ var RefMap = map[string]string{serialize_go_dict(ref_map['ref'])} var DocTitleMap = map[string]string{serialize_go_dict(ref_map['doc'])} var AllowedShellIntegrationValues = []string{{ {str(sorted(allowed_shell_integration_values))[1:-1].replace("'", '"')} }} var QueryNames = []string{{ {query_names} }} +var CommentedOutDefaultConfig = "{serialize_as_go_string(commented_out_default_config())}" var KittyConfigDefaults = struct {{ Term, Shell_integration, Select_by_word_characters, Url_excluded_characters, Shell string Wheel_scroll_multiplier int diff --git a/tools/config/api.go b/tools/config/api.go index fd73d849d..693478272 100644 --- a/tools/config/api.go +++ b/tools/config/api.go @@ -17,6 +17,7 @@ import ( "strings" "sync" + "kitty" "kitty/tools/utils" "github.com/shirou/gopsutil/v3/process" @@ -320,8 +321,15 @@ func (self Patcher) Patch(path, sentinel, content string, settings_to_comment_ou if err != nil && !errors.Is(err, fs.ErrNotExist) { return false, err } + add_at_top := "" + backup := true if raw == nil { - raw = []byte{} + cc := kitty.CommentedOutDefaultConfig + if idx := strings.Index(cc, "\n\n"); idx > 0 { + add_at_top = cc[:idx+2] + raw = []byte(cc[idx+2:]) + backup = false + } } pat := utils.MustCompile(fmt.Sprintf(`(?m)^\s*(%s)\b`, strings.Join(settings_to_comment_out, "|"))) text := pat.ReplaceAllString(utils.UnsafeBytesToString(raw), `# $1`) @@ -334,14 +342,21 @@ func (self Patcher) Patch(path, sentinel, content string, settings_to_comment_ou return addition }) if !replaced { - if text != "" { - text += "\n\n" + if add_at_top != "" { + ntext = add_at_top + addition + if text != "" { + ntext += "\n\n" + text + } + } else { + if text != "" { + text += "\n\n" + } + ntext = text + addition } - ntext = text + addition } nraw := utils.UnsafeStringToBytes(ntext) if !bytes.Equal(raw, nraw) { - if len(raw) > 0 && self.Write_backup { + if len(raw) > 0 && self.Write_backup && backup { _ = os.WriteFile(backup_path+".bak", raw, self.Mode) }