@setoelkahfi / svara / commits / 4c5ea4f

feat: added external terminal support

mellbacon committed Nov 10, 2023 at 13:34 UTC 4c5ea4f5eaf412b146a9a2d8db95c77286327bc4
2 files changed +35 -3
src-tauri/src/main.rs
+21 -1
@@ -49,6 +49,25 @@ fn open_in_default(path: &str) {
49 }
50 }
51
52 +#[tauri::command]
53 +fn open_terminal(path: &str) {
54 + if env::consts::OS == "windows" {
55 + // programs for windows: [cmd, powershell, wt]
56 + // programs for ubuntu: [gnome-terminal]
57 + // .args(["/C", "start", "wt"])
58 + Command::new("cmd")
59 + .args(["/C", "wt", "-d", path])
60 + .spawn()
61 + .unwrap();
62 + }
63 + else {
64 + Command::new("gnome-terminal")
65 + .arg(format!("--working-directory={}", path).as_str())
66 + .spawn()
67 + .unwrap();
68 + }
69 +}
70 +
71 #[tauri::command]
72 fn is_file(path: &str) -> bool {
73 fs::metadata(path).unwrap().is_file()
@@ -331,7 +350,8 @@ fn main() {
350 read_file,
351 write_file,
352 open_in_default,
334 - is_supported
353 + is_supported,
354 + open_terminal
355 ])
356 .plugin(tauri_plugin_fs_watch::init())
357 .plugin(tauri_plugin_store::Builder::default().build())
src/lib/Statusbar.svelte
+14 -2
@@ -4,6 +4,9 @@
4 import { getVersion } from '@tauri-apps/api/app';
5 import { onMount } from "svelte";
6 import { Terminal } from "carbon-icons-svelte";
7 + import { homeDir } from '@tauri-apps/api/path';
8 + import { filetree } from "./FileTree.svelte";
9 + import { invoke } from "@tauri-apps/api";
10
11 let appVersion = "";
12 onMount(async () => {
@@ -11,8 +14,16 @@
14 })
15
16 const tools = [
14 - {name: "Terminal", content: "terminal", icon: Terminal, action: (t) => toggleBottomPanel(t)}
17 + {name: "Terminal", content: null, icon: Terminal, action: spawnTerminal}
18 ]
19 +
20 + async function spawnTerminal() {
21 + let workingdir = await homeDir();
22 + if ($filetree.length !== 0) {
23 + workingdir = $filetree[0].path;
24 + }
25 + invoke("open_terminal", {path: workingdir});
26 + }
27 </script>
28 <script lang="ts" context="module">
29 import { writable } from "svelte/store";
@@ -37,7 +48,8 @@
48 </div>
49 <div class="editor-tools">
50 {#each tools as tool}
40 - <span class="tool" title={tool.name} on:click={() => {tool.action(tool)}}>
51 + <!-- svelte-ignore a11y-click-events-have-key-events -->
52 + <span class="tool" title={tool.name} on:click={() => {tool.action()}}>
53 <svelte:component this={tool.icon}></svelte:component>
54 </span>
55 {/each}