chore: moved file reading to rust
mellbacon committed
Aug 2, 2023 at 14:44 UTC
7d8c83fcff7678fee24d1ac7455092200b02bfd9
5 files changed
+16
-4
src-tauri/Cargo.lock
+1
@@ -1582,6 +1582,7 @@ dependencies = [
1582
name = "nucleus"
1583
version = "0.2.1"
1584
dependencies = [
1585
+ "encoding_rs",
1586
"serde",
1587
"serde_json",
1588
"tauri",
src-tauri/Cargo.toml
+1
@@ -19,6 +19,7 @@ tauri = { version = "1.2.5", features = ["clipboard-write-text", "dialog-ask", "
19
tauri-plugin-fs-watch = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
20
trash = "3.0.2"
21
tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
22
+encoding_rs = "0.8.32"
23
24
[dev-dependencies]
25
windows = "0.32.0"
src-tauri/src/main.rs
+10
-2
@@ -6,7 +6,7 @@
6
use std::{env, fs};
7
use std::process::Command;
8
9
-use tauri::{Manager};
9
+use tauri::Manager;
10
11
#[tauri::command]
12
fn open_in_explorer(path: &str) {
@@ -51,9 +51,17 @@ fn delete_file(path: &str, perm: bool) {
51
}
52
}
53
54
+#[tauri::command]
55
+fn read_file(path: &str) -> std::string::String {
56
+ let bytes = fs::read(path).unwrap();
57
+ let content = encoding_rs::UTF_8.decode(&bytes);
58
+ println!("{:?}", content);
59
+ content.0.to_string()
60
+}
61
+
62
fn main() {
63
tauri::Builder::default()
56
- .invoke_handler(tauri::generate_handler![open_in_explorer, delete_file, attempt_file_access, is_file, is_folder])
64
+ .invoke_handler(tauri::generate_handler![open_in_explorer, delete_file, attempt_file_access, is_file, is_folder, read_file])
65
.plugin(tauri_plugin_fs_watch::init())
66
.plugin(tauri_plugin_store::Builder::default().build())
67
.run(tauri::generate_context!())
src/lib/File.ts
+1
@@ -157,6 +157,7 @@ export async function saveFile(saveAs = false) {
157
tab.label = newPath.split(path.sep).pop();
158
}
159
// write changes to the file
160
+ // TODO: need to find a way to save files in different encodings. it saves as utf 8 by default (very annoying)
161
fs.writeFile(tab.path, tab.content.getFileContent());
162
const fileType = await path.extname(tab.path);
163
tab.content.updateFileInfo({
src/lib/Tab/Tab.ts
+3
-2
@@ -1,6 +1,6 @@
1
import { writable } from "svelte/store";
2
import Editor from "../Editor.svelte";
3
-import { path as p, fs, dialog } from "@tauri-apps/api";
3
+import { path as p, dialog, invoke } from "@tauri-apps/api";
4
import { saveFile } from "../File";
5
6
export class Tab {
@@ -72,7 +72,8 @@ export class Tab {
72
}
73
let fileContent = "";
74
try {
75
- fileContent = await fs.readTextFile(path); //TODO: Fix performance issues/loading times on large files
75
+ fileContent = await invoke("read_file", {path: path}) //TODO: Fix performance issues/loading times on large files
76
+ //console.log( await invoke("read_file", {path: path}));
77
} catch (error) {
78
console.warn("Can't read file content. Setting to empty string. Error: ", error);
79
}