From ff0076531b0df54a8ab5799980d2f5c17b8c21a3 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Sun, 10 Nov 2024 00:48:29 +0800 Subject: [PATCH] move "use" --- src/main.rs | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0bf5477..77dcb2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,19 @@ -use std::process::exit; - static PID_FILE: &str = "/tmp/ffmpeg_audio_recording.pid"; static TEMP_FILE: &str = "/tmp/ffmpeg_audio_recording.ogg"; static API_ENDPOINT: &str = "http://127.0.0.1:5000/v1/audio/transcriptions"; static AUTH_TOKEN: &str = "woshimima"; +use clipboard::ClipboardContext; +use clipboard::ClipboardProvider; use ctrlc; use nix::sys::signal; use nix::sys::signal::Signal; use nix::unistd::Pid; +use reqwest::blocking::Client; +use std::fs; +use std::fs::File; +use std::io::{Read, Write}; +use std::process::exit; use std::sync::mpsc::channel; fn main() { @@ -33,12 +38,11 @@ fn main() { } fn upload_audio() -> String { - use reqwest::blocking::Client; - let client = Client::new(); let form = reqwest::blocking::multipart::Form::new() - .file("file", TEMP_FILE).unwrap() + .file("file", TEMP_FILE) + .unwrap() .text("response_format", "text") .text("prompt", get_clipboard_content()) .text("model", "whisper-1"); @@ -87,13 +91,9 @@ fn start_recording() { ffmpeg.wait().unwrap(); println!("ffmpeg exited"); - } fn read_pid() -> Option { - use std::fs::File; - use std::io::Read; - let mut file = match File::open(PID_FILE) { Ok(ctx) => ctx, Err(_) => { @@ -110,23 +110,19 @@ fn read_pid() -> Option { } fn kill_and_delete_pid() { - use std::fs; let pid = read_pid().unwrap(); let pid = pid.trim(); let pid = pid.parse::().unwrap(); match signal::kill(Pid::from_raw(pid), Signal::SIGINT) { - Ok(_) => {}, + Ok(_) => {} Err(_) => { send_notification("Error to kill", "Error to kill pid, cleaning"); - }, + } } fs::remove_file(PID_FILE).unwrap(); } fn set_pid() { - use std::fs::File; - use std::io::Write; - let mut file = File::create(PID_FILE).unwrap(); file.write_all(format!("{}", std::process::id()).as_bytes()) .unwrap(); @@ -148,9 +144,6 @@ fn send_notification(title: &str, content: &str) { } fn get_clipboard_content() -> String { - use clipboard::ClipboardContext; - use clipboard::ClipboardProvider; - match ClipboardContext::new().and_then(|mut ctx| ctx.get_contents()) { Ok(content) => content, Err(error) => { @@ -164,9 +157,6 @@ fn get_clipboard_content() -> String { } fn set_clipboard_content(content: &str) { - use clipboard::ClipboardContext; - use clipboard::ClipboardProvider; - match ClipboardContext::new().and_then(|mut ctx| ctx.set_contents(content.to_owned())) { Ok(_) => {} Err(error) => {