55from pathlib import Path
66from typing import List
77
8+ import toml
9+
810from cecli .commands .utils .base_command import BaseCommand
911from cecli .commands .utils .helpers import format_command_result
1012
@@ -13,15 +15,6 @@ class TerminalSetupCommand(BaseCommand):
1315 NORM_NAME = "terminal-setup"
1416 DESCRIPTION = "Configure terminal config files to support shift+enter for newline"
1517
16- # Configuration constants
17- ALACRITTY_BINDING = """
18- # Added by cecli terminal-setup command
19- [[keyboard.bindings]]
20- key = "Return"
21- mods = "Shift"
22- chars = "\\ n"
23- """
24-
2518 KITTY_BINDING = "\n # Added by cecli terminal-setup command\n map shift+enter send_text all \\ n\n "
2619
2720 WT_ACTION = {
@@ -98,50 +91,94 @@ def _backup_file(cls, file_path, io):
9891
9992 @classmethod
10093 def _update_alacritty (cls , path , io , dry_run = False ):
101- """Appends the TOML configuration if not already present ."""
94+ """Updates Alacritty TOML configuration with shift+enter binding ."""
10295 if not path .exists ():
10396 io .tool_output (f"Skipping Alacritty: File not found at { path } " )
10497 return False
10598
99+ # Define the binding to add
100+ new_binding = {"key" : "Return" , "mods" : "Shift" , "chars" : "\n " }
101+
106102 if dry_run :
107103 io .tool_output (f"DRY-RUN: Would check Alacritty config at { path } " )
108- io .tool_output (f"DRY-RUN: Would append binding:\n { cls .ALACRITTY_BINDING .strip ()} " )
109- # Simulate checking for duplicates
104+ io .tool_output (f"DRY-RUN: Would add binding: { new_binding } " )
110105 try :
111106 with open (path , "r" , encoding = "utf-8" ) as f :
112- content = f .read ()
113- if (
114- 'key = "Return"' in content
115- and 'mods = "Shift"' in content
116- and 'chars = "\\ n"' in content
117- ):
107+ data = toml .load (f )
108+
109+ # Check if binding already exists
110+ keyboard_section = data .get ("keyboard" , {})
111+ bindings = keyboard_section .get ("bindings" , [])
112+
113+ already_exists = False
114+ for binding in bindings :
115+ if (
116+ binding .get ("key" ) == "Return"
117+ and binding .get ("mods" ) == "Shift"
118+ and binding .get ("chars" ) == "\n "
119+ ):
120+ already_exists = True
121+ break
122+
123+ if already_exists :
118124 io .tool_output ("DRY-RUN: Alacritty already configured." )
119125 return False
120126 else :
121127 io .tool_output ("DRY-RUN: Would update Alacritty config." )
122128 return True
129+ except toml .TomlDecodeError :
130+ io .tool_output ("DRY-RUN: Error: Could not parse Alacritty TOML file." )
131+ return False
123132 except Exception as e :
124133 io .tool_output (f"DRY-RUN: Error reading file: { e } " )
125134 return False
126135
127136 cls ._backup_file (path , io )
128137
129- with open (path , "r" , encoding = "utf-8" ) as f :
130- content = f .read ()
138+ try :
139+ with open (path , "r" , encoding = "utf-8" ) as f :
140+ data = toml .load (f )
131141
132- # Simple check to avoid duplicates
133- if (
134- 'key = "Return"' in content
135- and 'mods = "Shift"' in content
136- and 'chars = "\\ n"' in content
137- ):
138- io .tool_output ("Alacritty already configured." )
139- return False
142+ # Ensure keyboard section exists
143+ if "keyboard" not in data :
144+ data ["keyboard" ] = {}
140145
141- with open (path , "a" , encoding = "utf-8" ) as f :
142- f .write (cls .ALACRITTY_BINDING )
143- io .tool_output ("Updated Alacritty config." )
144- return True
146+ # Ensure bindings array exists
147+ if "bindings" not in data ["keyboard" ]:
148+ data ["keyboard" ]["bindings" ] = []
149+
150+ # Check if binding already exists
151+ bindings = data ["keyboard" ]["bindings" ]
152+ already_exists = False
153+ for binding in bindings :
154+ if (
155+ binding .get ("key" ) == "Return"
156+ and binding .get ("mods" ) == "Shift"
157+ and binding .get ("chars" ) == "\n "
158+ ):
159+ already_exists = True
160+ break
161+
162+ if already_exists :
163+ io .tool_output ("Alacritty already configured." )
164+ return False
165+
166+ # Add the new binding
167+ data ["keyboard" ]["bindings" ].append (new_binding )
168+
169+ # Write back to file
170+ with open (path , "w" , encoding = "utf-8" ) as f :
171+ toml .dump (data , f )
172+
173+ io .tool_output ("Updated Alacritty config." )
174+ return True
175+
176+ except toml .TomlDecodeError :
177+ io .tool_output ("Error: Could not parse Alacritty TOML file. Is it valid TOML?" )
178+ return False
179+ except Exception as e :
180+ io .tool_output (f"Error updating Alacritty config: { e } " )
181+ return False
145182
146183 @classmethod
147184 def _update_kitty (cls , path , io , dry_run = False ):
0 commit comments