-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from SquitchYT/dev-refactor
Dev refactor
- Loading branch information
Showing
39 changed files
with
943 additions
and
1,327 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
Oops, something went wrong.