diff --git a/src/features/index.ts b/src/features/index.ts index a062a2d02..ecccffd69 100644 --- a/src/features/index.ts +++ b/src/features/index.ts @@ -37,6 +37,7 @@ import { activate as scratch } from "./scratch"; import { activate as libraries } from "./libraries"; import { activate as viewers } from "./viewers"; import { activate as notebook } from "./notebook"; +import { activate as pythonConsole } from "./python-console"; import { activate as web } from "./web"; import { activate as analysis } from "./analysis"; import { activate as services } from "./services"; @@ -91,6 +92,7 @@ const features: Array<[string, Activate]> = [ ["scratch", scratch], ["libraries", libraries], ["notebook", notebook], + ["python-console", pythonConsole], ["viewers", viewers], ["web", web], ["analysis", analysis], diff --git a/src/features/python-console/ConsoleView.tsx b/src/features/python-console/ConsoleView.tsx new file mode 100644 index 000000000..b756e0074 --- /dev/null +++ b/src/features/python-console/ConsoleView.tsx @@ -0,0 +1,142 @@ +/** Python Console tool window: output, the input box below it, and the console's variables beside them. */ +import { useEffect, useLayoutEffect, useRef, useState } from "react"; +import { executeAction } from "@/platform/actions"; +import { useConsoleFont } from "@/platform/consoleFont"; +import { useTx } from "@/platform/i18n"; +import { Button, EmptyState, IconButton, SplitGroup, SplitHandle, SplitPanel, T } from "@/ui"; +import { spanStyle } from "@/ui/ansi"; +import { clearOutput, execute, restartConsole, sendInput, startConsole, stopConsole, useConsole } from "./store"; +import styles from "./PythonConsole.module.css"; + +export function ConsoleView() { + const { status, header, output, variables, noInterpreter } = useConsole(); + const font = useConsoleFont(); + const t = useTx(); + const outputRef = useRef(null); + const follow = useRef(true); + + useEffect(() => void startConsole(), []); + useLayoutEffect(() => { + const el = outputRef.current; + if (el && follow.current) el.scrollTop = el.scrollHeight; + }, [output]); + + if (noInterpreter && status === "stopped") { + return ( + + + + + } + > + Select an interpreter for this project to use the Python Console. + + ); + } + + const fontStyle = { fontFamily: font.family, fontSize: font.size, lineHeight: `${font.rowHeight}px` }; + return ( +
+
+ + + + {header ?? (status === "starting" ? Starting… : null)} +
+ + +
{ + const el = e.currentTarget; + follow.current = el.scrollHeight - el.scrollTop - el.clientHeight < 4; + }} + > + {output.map((s, i) => ( + + {s.text} + + ))} +
+ +
+ + + {variables.length ? ( +
+ {variables.map((v) => ( +
+ {v.name} + {v.type} + {v.value} +
+ ))} +
+ ) : ( + + )} +
+
+
+ ); +} + +/** Enter runs the input (or, while code runs, answers its `input()`); Shift+Enter adds a line; ↑/↓ recall. */ +function ConsoleInput({ style, busy }: { style: React.CSSProperties; busy: boolean }) { + const [text, setText] = useState(""); + const browse = useRef(null); + const t = useTx(); + return ( +