Debian Codegraph @

examples/krm

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Common commands for the kitty terminal emulator
3
4 Kitty allows remote control if the proper flag is set in config.
5 This lets users configure settings on the fly, using some
6 commands. These commands tend to be a bit too long to quickly type
7 out, so this utility creates a frontend to manage them.
8
9 The currently supported commands are:
10 - kitty @ set-window-title {title} : Set OS window's title
11 - kitty @ resize-os-window --width {width}: Set OS window's width
12 - kitty @ resize-os-window --height {height}: Set OS window's height
13 """
14
15 from subprocess import run
16
17 from argparse import ArgumentParser, Namespace
18 import pytermgui as ptg
19
20
21 CONFIG_YAML = """\
22 config:
23 InputField:
24 styles:
25 fill: '[@236]{item}'
26 value: '[72]{item}'
27
28 Window:
29 styles:
30 border: &border-style '[60]{item}'
31 corner: *border-style
32
33 Button:
34 styles:
35 label: '[@236 72 bold]{item}'
36
37 Splitter:
38 chars:
39 separator: " "
40 """
41
42 COMMANDS = {
43 "title": ["kitty", "@", "set-window-title"],
44 "width": ["kitty", "@", "resize-os-window", "--width"],
45 "height": ["kitty", "@", "resize-os-window", "--height"],
46 }
47
48
49 def input_box(title: str, command: list) -> ptg.Container:
50 """Create a Container with title, containing an InputField"""
51
52 box = ptg.Container()
53 box.set_char("corner", ["x- " + title + " -", "x", "x", "x"])
54 box.set_char("border", ["| ", "-", " |", "-"])
55
56 style = ptg.MarkupFormatter("[243]{item}")
57 box.set_style("border", style)
58 box.set_style("corner", style)
59
60 field = ptg.InputField()
61 field.bind(ptg.keys.RETURN, lambda field, _: run(command + [field.value]))
62 box += field
63 return box
64
65
66 def execute_all(window: ptg.Window) -> None:
67 """Execute all `RETURN` bindings in window"""
68
69 for field, _ in window.selectables:
70 if not isinstance(field, ptg.InputField):
71 continue
72
73 field.execute_binding(ptg.keys.RETURN)
74
75
76 def parse_arguments() -> Namespace:
77 """Parse command line arguments"""
78
79 parser = ArgumentParser()
80 parser.add_argument("-t", "--title", help="Set window title")
81 parser.add_argument("-w", "--width", help="Set window width")
82 parser.add_argument("--height", help="Set window height")
83
84 return parser.parse_args()
85
86
87 def main() -> None:
88 """Main method"""
89
90 args = parse_arguments()
91 no_tui = bool(args.title) | bool(args.width) | bool(args.height)
92
93 if args.title:
94 run(COMMANDS["title"] + [args.title])
95
96 if args.width:
97 run(COMMANDS["width"] + [args.width])
98
99 if args.height:
100 run(COMMANDS["height"] + [args.height])
101
102 if no_tui:
103 return
104
105 with ptg.WindowManager() as manager:
106 loader = ptg.YamlLoader()
107 namespace = loader.load(CONFIG_YAML)
108
109 window = (
110 ptg.Window(width=70)
111 + "[210 bold]Kitty Remote"
112 + "[210 bold]------------"
113 + ""
114 + ptg.Label(
115 "[240 italic]> Press RETURN to run each command", parent_align=0
116 )
117 + ""
118 + input_box("Title", COMMANDS["title"])
119 + ""
120 + (
121 input_box("Width", COMMANDS["width"]),
122 input_box("Height", COMMANDS["height"]),
123 )
124 + ""
125 + ptg.Button("Submit all!", lambda *_: execute_all(window))
126 ).center()
127
128 window.box = "DOUBLE"
129 manager.add(window)
130
131 manager.run()
132
133
134 if __name__ == "__main__":
135 main()