mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-24 01:08:10 +02:00
Create shell integration rc file if it doesnt exist
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from typing import Optional
|
from typing import Optional, Union
|
||||||
|
|
||||||
from .constants import shell_integration_dir
|
from .constants import shell_integration_dir
|
||||||
from .fast_data_types import get_options
|
from .fast_data_types import get_options
|
||||||
@@ -19,13 +19,25 @@ posix_template = '''
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
def atomic_write(path: str, data: Union[str, bytes]) -> None:
|
||||||
|
tmp = path + '_ksi_tmp'
|
||||||
|
with open(tmp, 'w' + ('b' if isinstance(data, bytes) else '')) as f:
|
||||||
|
f.write(data)
|
||||||
|
os.rename(tmp, path)
|
||||||
|
|
||||||
|
|
||||||
def setup_integration(shell_name: str, rc_path: str, template: str = posix_template) -> None:
|
def setup_integration(shell_name: str, rc_path: str, template: str = posix_template) -> None:
|
||||||
import re
|
import re
|
||||||
rc_path = os.path.realpath(rc_path)
|
rc_path = os.path.realpath(rc_path)
|
||||||
if not os.access(rc_path, os.W_OK, effective_ids=os.access in os.supports_effective_ids):
|
if not os.access(rc_path, os.W_OK, effective_ids=os.access in os.supports_effective_ids):
|
||||||
return
|
return
|
||||||
with open(rc_path) as f:
|
try:
|
||||||
rc = f.read()
|
with open(rc_path) as f:
|
||||||
|
rc = f.read()
|
||||||
|
except FileNotFoundError:
|
||||||
|
rc = ''
|
||||||
|
except Exception:
|
||||||
|
raise
|
||||||
home = os.path.expanduser('~') + '/'
|
home = os.path.expanduser('~') + '/'
|
||||||
path = os.path.join(shell_integration_dir, f'kitty.{shell_name}')
|
path = os.path.join(shell_integration_dir, f'kitty.{shell_name}')
|
||||||
if path.startswith(home):
|
if path.startswith(home):
|
||||||
@@ -36,10 +48,7 @@ def setup_integration(shell_name: str, rc_path: str, template: str = posix_templ
|
|||||||
'', rc, flags=re.DOTALL | re.MULTILINE)
|
'', rc, flags=re.DOTALL | re.MULTILINE)
|
||||||
newrc = newrc.rstrip() + '\n\n' + integration
|
newrc = newrc.rstrip() + '\n\n' + integration
|
||||||
if newrc != rc:
|
if newrc != rc:
|
||||||
tmp = rc_path + '_ksi_tmp'
|
atomic_write(rc_path, newrc)
|
||||||
with open(tmp, 'w') as f:
|
|
||||||
f.write(newrc)
|
|
||||||
os.rename(tmp, rc_path)
|
|
||||||
|
|
||||||
|
|
||||||
def setup_zsh_integration() -> None:
|
def setup_zsh_integration() -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user