get multiple JSON messages working

This commit is contained in:
Kovid Goyal
2024-05-03 12:23:50 +05:30
parent 1646c297b3
commit 8a8158f287
3 changed files with 15 additions and 3 deletions

View File

@@ -40,10 +40,10 @@ def handle_io(from_kitten: BinaryIO, to_kitten: BinaryIO) -> None:
def send_to_kitten(x: Any) -> None: def send_to_kitten(x: Any) -> None:
to_kitten.write(json.dumps(x).encode()) to_kitten.write(json.dumps(x).encode())
to_kitten.write(b'\n') to_kitten.write(b'\n')
to_kitten.flush()
try: try:
send_to_kitten(create_family_groups(add_variable_data=True)) send_to_kitten(create_family_groups(add_variable_data=True))
to_kitten.write(as_json().encode())
for line in from_kitten: for line in from_kitten:
cmd = json.loads(line) cmd = json.loads(line)
action = cmd['action'] action = cmd['action']
@@ -54,6 +54,7 @@ def handle_io(from_kitten: BinaryIO, to_kitten: BinaryIO) -> None:
def main(argv: Sequence[str]) -> None: def main(argv: Sequence[str]) -> None:
import os
import subprocess import subprocess
import sys import sys
from threading import Thread from threading import Thread
@@ -62,7 +63,10 @@ def main(argv: Sequence[str]) -> None:
argv = list(argv) argv = list(argv)
if '--psnames' in argv: if '--psnames' in argv:
argv.remove('--psnames') argv.remove('--psnames')
p = subprocess.Popen([kitten_exe(), '__list_fonts__'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) pass_fds = []
if os.environ.get('KITTY_STDIO_FORWARDED'):
pass_fds.append(int(os.environ['KITTY_STDIO_FORWARDED']))
p = subprocess.Popen([kitten_exe(), '__list_fonts__'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, pass_fds=pass_fds)
Thread(target=handle_io, args=(p.stdout, p.stdin), daemon=True).start() Thread(target=handle_io, args=(p.stdout, p.stdin), daemon=True).start()
try: try:
raise SystemExit(p.wait()) raise SystemExit(p.wait())

View File

@@ -12,8 +12,16 @@ import (
var _ = fmt.Print var _ = fmt.Print
var debugprintln = tty.DebugPrintln var debugprintln = tty.DebugPrintln
var json_decoder *json.Decoder var json_decoder *json.Decoder
func json_decode(v any) error {
if err := json_decoder.Decode(v); err != nil {
return fmt.Errorf("Failed to decode JSON from kitty with error: %w", err)
}
return nil
}
func main() (rc int, err error) { func main() (rc int, err error) {
json_decoder = json.NewDecoder(os.Stdin) json_decoder = json.NewDecoder(os.Stdin)
lp, err := loop.New() lp, err := loop.New()

View File

@@ -187,7 +187,7 @@ func (h *handler) initialize() {
h.rl = readline.New(h.lp, readline.RlInit{DontMarkPrompts: true, Prompt: "Family: "}) h.rl = readline.New(h.lp, readline.RlInit{DontMarkPrompts: true, Prompt: "Family: "})
h.draw_screen() h.draw_screen()
go func() { go func() {
h.set_worker_error(json_decoder.Decode(&h.fonts)) h.set_worker_error(json_decode(&h.fonts))
h.lp.WakeupMainThread() h.lp.WakeupMainThread()
}() }()
} }