├── set_colors.py └── password.py /set_colors.py: -------------------------------------------------------------------------------- 1 | from kitty.cmds import set_colors 2 | from kitty.config import parse_config 3 | from kitty.constants import config_dir 4 | from kitty.rgb import color_as_int, Color 5 | import os 6 | 7 | def main(args): 8 | pass 9 | 10 | def handle_result(args, data, target_window_id, boss): 11 | colors = {} 12 | 13 | for spec in args[1:]: 14 | if '=' in spec: 15 | colors.update(parse_config((spec.replace('=', ' '),))) 16 | else: 17 | with open(os.path.join(config_dir, spec), encoding='utf-8', errors='replace') as f: 18 | colors.update(parse_config(f)) 19 | 20 | colors = {k: color_as_int(v) for k, v in colors.items() if isinstance(v, Color)} 21 | set_colors(boss, boss.window_id_map.get(target_window_id), 22 | { 'all': False, 'match_window': False, 'match_tab': False, 'reset': False, 'configured': False, 'colors': colors }) 23 | 24 | handle_result.no_ui = True 25 | 26 | if __name__ == '__main__': 27 | import sys 28 | main(sys.argv) 29 | -------------------------------------------------------------------------------- /password.py: -------------------------------------------------------------------------------- 1 | from termios import error, tcgetattr, ECHO, ICANON, ISIG, ECHOCTL, ECHOKE 2 | import re 3 | import subprocess 4 | import sys 5 | 6 | def main(args): 7 | if not sys.stdin.isatty(): 8 | data = sys.stdin.read().strip().split('\n')[-1] 9 | return data 10 | else: 11 | print("\a") 12 | print("Tried to read a tty") 13 | 14 | def handle_result(args, data, target_window_id, boss): 15 | w = boss.window_id_map.get(target_window_id) 16 | if w is not None: 17 | fd = w.child.child_fd 18 | try: 19 | c_lflag = tcgetattr(fd)[3] 20 | except error as err: 21 | errmsg = 'getecho() may not be called on this platform' 22 | if err.args[0] == errno.EINVAL: 23 | raise IOError(err.args[0], '%s: %s.' % (err.args[1], errmsg)) 24 | raise 25 | 26 | def send_password(): 27 | password = subprocess.run(args[1:], capture_output=True, text=True, check=True).stdout 28 | w.paste(password + '\r') 29 | 30 | def is_set(flag): 31 | return bool(c_lflag & flag) 32 | 33 | # print(f'c_lflag={c_lflag} icanon={c_lflag & ICANON} echo={c_lflag & ECHO} echoctl={c_lflag & ECHOCTL} echoke={c_lflag & ECHOKE} isig={c_lflag & ISIG} data={repr(data)}') 34 | if is_set(ISIG) and is_set(ICANON) and not is_set(ECHO): 35 | # ruby -rio/console -e 'STDIN.noecho { |io| puts io.gets.inspect }' 36 | # sudo 37 | send_password() 38 | elif not is_set(ISIG) and not is_set(ICANON) and not is_set(ECHO) and re.match(r"Password.*:$", data): 39 | # ssh -t # same socket mode for kinit and cat :( 40 | send_password() 41 | else: 42 | print("\a") 43 | print("Ooops. Are you at a password prompt?") 44 | 45 | handle_result.type_of_input = 'text' 46 | 47 | if __name__ == '__main__': 48 | import sys 49 | main(sys.argv) 50 | --------------------------------------------------------------------------------