| 1 | import { CommandLineIcon } from "@heroicons/react/24/outline"; |
| 2 | import { useContext } from "react"; |
| 3 | import { lightGray, vscForeground } from "../.."; |
| 4 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 5 | import { isJetBrains } from "../../../util"; |
| 6 | |
| 7 | interface RunInTerminalButtonProps { |
| 8 | command: string; |
| 9 | } |
| 10 | |
| 11 | export function RunInTerminalButton({ command }: RunInTerminalButtonProps) { |
| 12 | const ideMessenger = useContext(IdeMessengerContext); |
| 13 | |
| 14 | if (isJetBrains()) { |
| 15 | // JetBrains plugin doesn't currently have a way to run the command in the terminal for the user |
| 16 | return null; |
| 17 | } |
| 18 | |
| 19 | // Extract just the command line (the line after $ or the first line) |
| 20 | function extractCommand(cmd: string): string { |
| 21 | // If the command contains a $ prompt, extract the line after it |
| 22 | if (cmd.includes("$")) { |
| 23 | const match = cmd.match(/\$\s*([^\n]+)/); |
| 24 | return match ? match[1].trim() : ""; |
| 25 | } |
| 26 | |
| 27 | // Otherwise, just take the first line |
| 28 | return cmd.split("\n")[0].trim(); |
| 29 | } |
| 30 | |
| 31 | function runInTerminal() { |
| 32 | // Extract just the command line |
| 33 | const extractedCommand = extractCommand(command); |
| 34 | void ideMessenger.post("runCommand", { command: extractedCommand }); |
| 35 | } |
| 36 | |
| 37 | return ( |
| 38 | <div |
| 39 | className={`text-lightgray flex items-center border-none bg-transparent text-xs text-[${vscForeground}] cursor-pointer outline-none hover:brightness-125`} |
| 40 | onClick={runInTerminal} |
| 41 | > |
| 42 | <div |
| 43 | className="max-2xs:hidden flex items-center gap-1 transition-colors duration-200 hover:brightness-125" |
| 44 | style={{ color: lightGray }} |
| 45 | > |
| 46 | <> |
| 47 | <CommandLineIcon className="h-3 w-3 hover:brightness-125" /> |
| 48 | <span className="text-lightgray max-sm:hidden">Run</span> |
| 49 | </> |
| 50 | </div> |
| 51 | </div> |
| 52 | ); |
| 53 | } |