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
This commit is contained in:
Kovid Goyal 2024-10-28 14:01:42 +05:30
parent 8bca84ed66
commit 8b7cd98a0e
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 24 additions and 5 deletions

View file

@ -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]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View file

@ -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

View file

@ -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)
}