move "use"

This commit is contained in:
2024-11-10 00:48:29 +08:00
parent 37c409dcc1
commit ff0076531b

View File

@@ -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<String> {
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<String> {
}
fn kill_and_delete_pid() {
use std::fs;
let pid = read_pid().unwrap();
let pid = pid.trim();
let pid = pid.parse::<i32>().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) => {