From 4b622bd13d9e0b3e6b1bf46baecb34d0e25d0770 Mon Sep 17 00:00:00 2001 From: Francisco Salgueiro Date: Sun, 5 Nov 2023 22:27:54 +0000 Subject: [PATCH] open pgn file with cli argument --- src-tauri/Cargo.lock | 66 +++++++++++++++++++++++++ src-tauri/Cargo.toml | 2 +- src-tauri/tauri.conf.json | 11 ++++- src/App.tsx | 44 +++++++++++++++-- src/components/boards/BoardAnalysis.tsx | 3 +- 5 files changed, 119 insertions(+), 7 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 4ecf42ed..3dec9def 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -245,6 +245,17 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1181e1e0d1fce796a03db1ae795d67167da795f9cf4a39c37589e85ef57f26d3" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -609,6 +620,30 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b0fc239e0f6cb375d2402d48afb92f76f5404fd1df208a41930ec81eda078bea" +[[package]] +name = "clap" +version = "3.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" +dependencies = [ + "atty", + "bitflags 1.3.2", + "clap_lex", + "indexmap", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + [[package]] name = "cocoa" version = "0.24.1" @@ -1700,6 +1735,15 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.2.6" @@ -2628,6 +2672,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "os_str_bytes" +version = "6.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" + [[package]] name = "overload" version = "0.1.1" @@ -4058,6 +4108,7 @@ dependencies = [ "anyhow", "base64 0.21.2", "bytes", + "clap", "cocoa", "dirs-next", "embed_plist", @@ -4326,6 +4377,21 @@ dependencies = [ "utf-8", ] +[[package]] +name = "termcolor" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6093bad37da69aab9d123a8091e4be0aa4a03e4d601ec641c327398315f62b64" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" + [[package]] name = "thin-slice" version = "0.1.1" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 00d4ca83..088a0379 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -16,7 +16,7 @@ tauri-build = { version = "1.5", features = [] } tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" } serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } -tauri = { version = "1.5", features = ["api-all", "updater"] } +tauri = { version = "1.5", features = [ "cli", "api-all", "updater"] } zip = "0.6.2" tokio = { version = "1.33", features = ["full"] } futures-util = "0.3.24" diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 38ada594..a46bbc0a 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -84,6 +84,15 @@ "maximized": true, "visible": false } - ] + ], + "cli": { + "args": [ + { + "name": "file", + "index": 1, + "takesValue": true + } + ] + } } } \ No newline at end of file diff --git a/src/App.tsx b/src/App.tsx index ea97dd02..c39a2bcb 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -17,6 +17,7 @@ import DatabaseView from "@/components/databases/DatabaseView"; import HomePage from "@/components/home/HomePage"; import { getVersion } from "@tauri-apps/api/app"; import { attachConsole } from "tauri-plugin-log-api"; +import { getMatches } from '@tauri-apps/api/cli' import { Outlet, @@ -27,12 +28,15 @@ import { } from "react-router-dom"; import { useEffect } from "react"; import DatabasesPage from "./components/databases/DatabasesPage"; -import { useAtomValue } from "jotai"; -import { pieceSetAtom, primaryColorAtom } from "./atoms/atoms"; +import { useAtom, useAtomValue } from "jotai"; +import { currentTabAtom, pieceSetAtom, primaryColorAtom } from "./atoms/atoms"; import "@/styles/chessgroundBaseOverride.css"; import "@/styles/chessgroundColorsOverride.css"; import { commands } from "./bindings"; +import { count_pgn_games, read_games } from "./utils/db"; +import { parsePGN } from "./utils/chess"; +import { getGameName } from "./utils/treeReducer"; const router = createBrowserRouter( createRoutesFromElements( @@ -82,15 +86,49 @@ export default function App() { const toggleColorScheme = (value?: ColorScheme) => setColorScheme(value || (colorScheme === "dark" ? "light" : "dark")); const pieceSet = useAtomValue(pieceSetAtom); + const [, setCurrentTab] = useAtom(currentTabAtom); + useEffect(() => { (async () => { await commands.closeSplashscreen(); const detach = await attachConsole(); + + const matches = await getMatches(); + if (matches.args["file"].occurrences > 0) { + if (typeof matches.args["file"].value !== "string") return; + const file = matches.args["file"].value; + + router.navigate("/boards", { replace: true }); + const count = await count_pgn_games(file); + const input = (await read_games(file, 0, 0))[0]; + + const fileInfo = { + metadata: { + tags: [], + type: "game" as const, + }, + name: file, + path: file, + numGames: count, + } ; + const tree = await parsePGN(input); + setCurrentTab((prev) => { + sessionStorage.setItem(prev.value, JSON.stringify(tree)); + return { + ...prev, + name: `${getGameName(tree.headers)} (Imported)`, + file: fileInfo, + gameNumber: 0, + type: "analysis", + }; + }); + } + return () => { detach(); }; })(); - }); + }, []); return (