Skip to content

Commit

Permalink
Merge pull request #43 from SquitchYT/dev-refactor
Browse files Browse the repository at this point in the history
Dev refactor
  • Loading branch information
SquitchYT authored Jan 9, 2024
2 parents f3a2c45 + d53319b commit fc01b8e
Show file tree
Hide file tree
Showing 39 changed files with 943 additions and 1,327 deletions.
522 changes: 94 additions & 428 deletions src-tauri/Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ infer = "0.12.0"
tokio = { version = "1.25.0", features = ["full"] }
dirs-next = "2.0.0"
uuid = "1.3.0"
remoteprocess = "0.4.11"
thiserror = "1.0.50"

[target.'cfg(unix)'.dependencies]
signal-hook = "0.3.17"
Expand All @@ -33,6 +33,7 @@ futures = "0.3.29"
lazy_static = "1.4.0"
regex = "1.8.4"
window-vibrancy = "0.3.2"
windows = { version = "0.52.0", features = ["Win32_Foundation", "Win32_System", "Win32_System_Threading", "Win32_System_ProcessStatus", "Win32_System_Diagnostics_ToolHelp", "Win32_System_Diagnostics"] }

[features]
default = ["custom-protocol"]
Expand Down
4 changes: 0 additions & 4 deletions src-tauri/src/command/app.rs

This file was deleted.

4 changes: 0 additions & 4 deletions src-tauri/src/command/mod.rs

This file was deleted.

7 changes: 0 additions & 7 deletions src-tauri/src/command/option.rs

This file was deleted.

112 changes: 0 additions & 112 deletions src-tauri/src/command/term.rs

This file was deleted.

6 changes: 0 additions & 6 deletions src-tauri/src/command/window.rs

This file was deleted.

7 changes: 7 additions & 0 deletions src-tauri/src/common/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod pty;
mod utils;
mod window;

pub use pty::*;
pub use utils::*;
pub use window::*;
171 changes: 171 additions & 0 deletions src-tauri/src/common/commands/pty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
use crate::common::payloads::{PtySendData, PtyTitleChanged};
use crate::common::states::Ptys;
use crate::configuration::deserialized::Option;
use std::sync::Arc;
use tauri::Manager;
use tokio::sync::Mutex;

use crate::common::error::PtyError;

use crate::pty::pty::Pty;

#[tauri::command]
pub async fn pty_open(
app: tauri::AppHandle,
id: String,
profile_uuid: String,
option: tauri::State<'_, Arc<Mutex<Option>>>,
ptys: tauri::State<'_, Ptys>,
) -> Result<(), PtyError> {
let id_cloned = id.clone();
let id_cloned_twice = id.clone();
let id_cloned_thrice = id.clone();
let app_cloned = app.clone();
let app_cloned_twice = app.clone();

let locked_option = option.lock().await;
let opening_profile = locked_option
.profiles
.iter()
.find(|profile| profile.uuid == profile_uuid)
.ok_or(PtyError::UnknownPty)?;

let should_update_title = opening_profile.terminal_options.title_is_running_process;

ptys.0.lock().await.insert(
id.clone(),
Pty::build_and_run(
&opening_profile.command,
move |readed| {
app.emit_all(
"js_pty_data",
PtySendData {
data: readed,
id: &id_cloned,
},
)
.ok();
},
move |tab_title| {
if should_update_title {
app_cloned
.emit_all(
"js_pty_title_update",
PtyTitleChanged {
id: &id_cloned_twice,
title: tab_title,
},
)
.ok();
}
},
move || {
app_cloned_twice
.emit_all("js_pty_closed", id_cloned_thrice)
.ok();
},
)
.await?,
);

Ok(())
}

#[tauri::command]
pub async fn pty_close(ptys: tauri::State<'_, Ptys>, id: String) -> Result<(), PtyError> {
ptys.0
.lock()
.await
.get(&id)
.ok_or(PtyError::UnknownPty)?
.kill()
.await
}

#[tauri::command]
pub async fn pty_write(
id: String,
content: String,
ptys: tauri::State<'_, Ptys>,
) -> Result<(), PtyError> {
ptys.0
.lock()
.await
.get_mut(&id)
.ok_or(PtyError::UnknownPty)?
.write(&content)
}

#[tauri::command]
pub async fn pty_resize(
ptys: tauri::State<'_, Ptys>,
id: String,
cols: u16,
rows: u16,
) -> Result<(), PtyError> {
ptys.0
.lock()
.await
.get(&id)
.ok_or(PtyError::UnknownPty)?
.resize(cols, rows)
.await
}

#[tauri::command]
pub async fn pty_get_closable(
ptys: tauri::State<'_, Ptys>,
app_config: tauri::State<'_, Arc<Mutex<Option>>>,
id: String,
) -> Result<bool, PtyError> {
let app_config = app_config.lock().await;

if app_config.close_confirmation.tab {
let locked_ptys = ptys.0.lock().await;
let pty = locked_ptys.get(&id).ok_or(PtyError::UnknownPty)?;

Ok(pty.closed.load(std::sync::atomic::Ordering::Relaxed)
|| app_config
.close_confirmation
.excluded_process
.contains(&*pty.title.lock().await))
} else {
Ok(true)
}
}

#[tauri::command]
pub async fn pty_get_title(ptys: tauri::State<'_, Ptys>, id: String) -> Result<String, PtyError> {
Ok(ptys
.0
.lock()
.await
.get_mut(&id)
.ok_or(PtyError::UnknownPty)?
.title
.lock()
.await
.to_string())
}

#[tauri::command]
pub async fn pty_pause(ptys: tauri::State<'_, Ptys>, id: String) -> Result<(), PtyError> {
ptys.0
.lock()
.await
.get(&id)
.ok_or(PtyError::UnknownPty)?
.pause();
Ok(())
}

#[tauri::command]
pub async fn pty_resume(ptys: tauri::State<'_, Ptys>, id: String) -> Result<(), PtyError> {
ptys.0
.lock()
.await
.get(&id)
.ok_or(PtyError::UnknownPty)?
.resume();
Ok(())
}
15 changes: 15 additions & 0 deletions src-tauri/src/common/commands/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::configuration::deserialized::Option;
use std::sync::Arc;
use tokio::sync::Mutex;

#[tauri::command]
pub fn utils_close_app(app: tauri::AppHandle) {
app.exit(0);
}

#[tauri::command]
pub async fn utils_get_configuration(
option: tauri::State<'_, Arc<Mutex<Option>>>,
) -> Result<Option, ()> {
Ok(option.lock().await.clone())
}
4 changes: 4 additions & 0 deletions src-tauri/src/common/commands/window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[tauri::command]
pub fn window_close(window: tauri::Window) {
window.close().ok();
}
22 changes: 22 additions & 0 deletions src-tauri/src/common/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[derive(Debug, thiserror::Error)]
pub enum PtyError {
#[error("There is no terminal corresponding to this ID.")]
UnknownPty,
#[error("A problem occurred while creating the terminal: {0}.")]
Creation(String),
#[error("A problem occurred while writing to the terminal: {0}.")]
Write(String),
#[error("A problem occurred while resizing the terminal: {0}.")]
Resize(String),
#[error("A problem occurred while closing the terminal: {0}.")]
Kill(String),
}

impl serde::Serialize for PtyError {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
serializer.serialize_str(self.to_string().as_ref())
}
}
Loading

0 comments on commit fc01b8e

Please sign in to comment.