Refactor remote control commands into individual modules

Also add type information
This commit is contained in:
Kovid Goyal
2020-03-08 08:39:26 +05:30
parent a0321376d5
commit 9b32f18109
41 changed files with 2044 additions and 1540 deletions

View File

@@ -6,6 +6,7 @@ import os
import shutil
import subprocess
import sys
from typing import List, Tuple
from kitty.cli import parse_args
from kitty.cli_stub import PanelCLIOptions
@@ -43,19 +44,19 @@ Syntax: :italic:`name=value`. For example: :option:`kitty +kitten panel -o` font
'''.format
args = None
args = PanelCLIOptions()
help_text = 'Use a command line program to draw a GPU accelerated panel on your X11 desktop'
usage = 'program-to-run'
def parse_panel_args(args):
def parse_panel_args(args: List[str]) -> Tuple[PanelCLIOptions, List[str]]:
return parse_args(args, OPTIONS, usage, help_text, 'kitty +kitten panel', result_class=PanelCLIOptions)
def call_xprop(*cmd, silent=False):
cmd = ['xprop'] + list(cmd)
def call_xprop(*cmd: str, silent=False):
cmd_ = ['xprop'] + list(cmd)
try:
cp = subprocess.run(cmd, stdout=subprocess.DEVNULL if silent else None)
cp = subprocess.run(cmd_, stdout=subprocess.DEVNULL if silent else None)
except FileNotFoundError:
raise SystemExit('You must have the xprop program installed')
if cp.returncode != 0:
@@ -94,29 +95,33 @@ def create_right_strut(win_id, width, height):
create_strut(win_id, right=width, right_end_y=height)
window_width = window_height = 0
def setup_x11_window(win_id):
call_xprop(
'-id', str(win_id), '-format', '_NET_WM_WINDOW_TYPE', '32a',
'-set', '_NET_WM_WINDOW_TYPE', '_NET_WM_WINDOW_TYPE_DOCK'
)
func = globals()['create_{}_strut'.format(args.edge)]
func(win_id, initial_window_size_func.width, initial_window_size_func.height)
func(win_id, window_width, window_height)
def initial_window_size_func(opts, *a):
from kitty.fast_data_types import glfw_primary_monitor_size, set_smallest_allowed_resize
def initial_window_size(cell_width, cell_height, dpi_x, dpi_y, xscale, yscale):
global window_width, window_height
monitor_width, monitor_height = glfw_primary_monitor_size()
if args.edge in {'top', 'bottom'}:
h = initial_window_size_func.height = cell_height * args.lines + 1
initial_window_size_func.width = monitor_width
h = window_height = cell_height * args.lines + 1
window_width = monitor_width
set_smallest_allowed_resize(100, h)
else:
w = initial_window_size_func.width = cell_width * args.columns + 1
initial_window_size_func.height = monitor_height
w = window_width = cell_width * args.columns + 1
window_height = monitor_height
set_smallest_allowed_resize(w, 100)
return initial_window_size_func.width, initial_window_size_func.height
return window_width, window_height
return initial_window_size

View File

@@ -4,10 +4,11 @@
import sys
from typing import Optional
from kitty.cli import parse_args
from kitty.cli_stub import ResizeCLIOptions
from kitty.cmds import cmap, parse_subcommand_cli
from kitty.cli_stub import RCOptions, ResizeCLIOptions
from kitty.rc.base import parse_subcommand_cli, command_for_name
from kitty.constants import version
from kitty.key_encoding import CTRL, RELEASE, key_defs as K
from kitty.remote_control import encode_send, parse_rc_args
@@ -16,7 +17,7 @@ from ..tui.handler import Handler
from ..tui.loop import Loop
from ..tui.operations import styled
global_opts = None
global_opts = RCOptions()
ESCAPE = K['ESCAPE']
N = K['N']
S = K['S']
@@ -26,7 +27,7 @@ W = K['W']
class Resize(Handler):
print_on_fail = None
print_on_fail: Optional[str] = None
def __init__(self, opts):
self.opts = opts
@@ -40,7 +41,7 @@ class Resize(Handler):
self.draw_screen()
def do_window_resize(self, is_decrease=False, is_horizontal=True, reset=False, multiplier=1):
resize_window = cmap['resize-window']
resize_window = command_for_name('resize_window')
increment = self.opts.horizontal_increment if is_horizontal else self.opts.vertical_increment
increment *= multiplier
if is_decrease:
@@ -48,7 +49,7 @@ class Resize(Handler):
axis = 'reset' if reset else ('horizontal' if is_horizontal else 'vertical')
cmdline = [resize_window.name, '--self', '--increment={}'.format(increment), '--axis=' + axis]
opts, items = parse_subcommand_cli(resize_window, cmdline)
payload = resize_window(global_opts, opts, items)
payload = resize_window.message_to_kitty(global_opts, opts, items)
send = {'cmd': resize_window.name, 'version': version, 'payload': payload, 'no_response': False}
self.write(encode_send(send))