https://github.com/biomathcode/tauri-functions
Utility functions for tauri
https://github.com/biomathcode/tauri-functions
Last synced: about 1 month ago
JSON representation
Utility functions for tauri
- Host: GitHub
- URL: https://github.com/biomathcode/tauri-functions
- Owner: biomathcode
- License: mit
- Created: 2023-12-27T14:46:26.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-27T15:02:53.000Z (about 2 years ago)
- Last Synced: 2025-02-08T02:44:51.080Z (about 1 year ago)
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tauri-functions
Utility functions for tauri
### Export File
```tsx
import { save } from "@tauri-apps/api/dialog";
import { writeFile } from "@tauri-apps/api/fs";
const SaveFile = async (filename: string, content: string, extension: string) => {
const filepath = await save({
defaultPath: filename,
filters: [
{
name: filename,
extensions: [extension],
},
],
});
if (filepath) {
await writeFile(filepath, content);
}
}
```
### Copy to Clipboard
```tsx
import { writeText } from "@tauri-apps/api/clipboard";
const copyToClipboard = async (text: string) => {
try {
// Use the clipboard API to write text to the clipboard
await writeText(text);
// Optionally, you can notify the user that the text has been copied
console.log("Text copied to clipboard:", text);
} catch (error) {
console.error("Error copying to clipboard:", error);
}
};
```
### FileSize
```ts
export function bytesToSize(bytes: number): string {
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
if (bytes === 0) return "n/a";
const i = Math.min(
Math.floor(Math.log(bytes) / Math.log(1024)),
sizes.length - 1
);
if (i === 0) return `${bytes} ${sizes[i]}`;
return `${(bytes / 1024 ** i).toFixed(1)} ${sizes[i]}`;
}
const len = new TextEncoder().encode("File content").length;
console.log("size: ", bytesToSize(len));
```