Skip to content

Commit

Permalink
Decode CLI output with current code page
Browse files Browse the repository at this point in the history
Signed-off-by: xychen <xychen@listenai.com>
  • Loading branch information
xychen committed May 20, 2024
1 parent e930ec9 commit 1de5d25
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ tauri-plugin-updater = "2.0.0-beta.5"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serialport = "4.3.0"
windows = { version = "0.56.0", features = ["Win32_Globalization"] }
codepage = "0.1.1"
encoding_rs = "0.8.34"

[features]
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!
Expand Down
23 changes: 22 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

use serialport::available_ports;

#[cfg(windows)]
use codepage::to_encoding;
#[cfg(windows)]
use windows::Win32::Globalization::GetACP;

#[tauri::command]
fn list_ports() -> Vec<String> {
match available_ports() {
Expand All @@ -23,6 +28,22 @@ fn list_ports() -> Vec<String> {
}
}

#[tauri::command]
fn decode(data: Vec<u8>) -> String {
#[cfg(windows)]
{
let code_page = unsafe { GetACP() as u32 };
if let Some(encoding) = to_encoding(code_page.try_into().unwrap()) {
let (decoded, _, had_errors) = encoding.decode(&data);
if !had_errors {
return decoded.into_owned();
}
}
}

String::from_utf8_lossy(&data).to_string()
}

fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_dialog::init())
Expand All @@ -35,7 +56,7 @@ fn main() {
.plugin(tauri_plugin_updater::Builder::new().build())?;
Ok(())
})
.invoke_handler(tauri::generate_handler![list_ports])
.invoke_handler(tauri::generate_handler![list_ports, decode])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
1 change: 1 addition & 0 deletions src/tauri-invoke.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ declare module '@tauri-apps/api/core' {
import { invoke, type InvokeArgs } from '@tauri-apps/api/core';
function invoke<T>(cmd: string, args?: InvokeArgs): Promise<T>;
function invoke(cmd: 'list_ports'): Promise<string[]>;
function invoke(cmd: 'decode', args: { data: ArrayLike<number> }): Promise<string>;
}
9 changes: 5 additions & 4 deletions src/utils/cskburn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Command } from '@tauri-apps/plugin-shell';
import { invoke } from '@tauri-apps/api/core';

type ICSKBurnEventHandlers = Partial<{
onOutput: (output: string) => void;
Expand Down Expand Up @@ -73,14 +74,14 @@ export async function cskburn(
}
}

command.stdout.on('data', (data: Uint8Array) => {
const line = new TextDecoder('utf-8').decode(new Uint8Array(data));
command.stdout.on('data', async (data: Uint8Array) => {
const line = await invoke('decode', { data });
outputs.push(line);
handleOutput(line.trim());
});

command.stderr.on('data', (data: Uint8Array) => {
const line = new TextDecoder('utf-8').decode(new Uint8Array(data));
command.stderr.on('data', async (data: Uint8Array) => {
const line = await invoke('decode', { data });
outputs.push(line);
handleOutput(line.trim());
});
Expand Down

0 comments on commit 1de5d25

Please sign in to comment.