-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tui] refactor tui main_event_loop to use DI, for input & output devi…
…ce & return state - Introduce OutputDevice struct - Introduce InputDevice struct - Clean up Flush trait
- Loading branch information
1 parent
de56b48
commit d32fc10
Showing
45 changed files
with
1,270 additions
and
782 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 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 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,43 @@ | ||
/* | ||
* Copyright (c) 2024 R3BL LLC | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
use crossterm::event::EventStream; | ||
use futures_util::{FutureExt, StreamExt}; | ||
use miette::IntoDiagnostic; | ||
|
||
use crate::{CrosstermEventResult, PinnedInputStream}; | ||
|
||
pub struct InputDevice { | ||
pub resource: PinnedInputStream<CrosstermEventResult>, | ||
} | ||
|
||
impl InputDevice { | ||
pub fn new_event_stream() -> InputDevice { | ||
InputDevice { | ||
resource: Box::pin(EventStream::new()), | ||
} | ||
} | ||
} | ||
|
||
impl InputDevice { | ||
pub async fn next(&mut self) -> miette::Result<crossterm::event::Event> { | ||
match self.resource.next().fuse().await { | ||
Some(it) => it.into_diagnostic(), | ||
None => miette::bail!("Failed to get next event from input source."), | ||
} | ||
} | ||
} |
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 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,74 @@ | ||
/* | ||
* Copyright (c) 2024 R3BL LLC | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
use std::sync::Arc; | ||
|
||
use crate::{SafeRawTerminal, SendRawTerminal, StdMutex}; | ||
|
||
pub type LockedOutputDevice<'a> = &'a mut dyn std::io::Write; | ||
|
||
/// Macro to simplify locking and getting a mutable reference to the output device. | ||
/// | ||
/// Usage example: | ||
/// ```rust | ||
/// use r3bl_core::{output_device_as_mut, OutputDevice, LockedOutputDevice}; | ||
/// let device = OutputDevice::new_stdout(); | ||
/// let mut_ref: LockedOutputDevice<'_> = output_device_as_mut!(device); | ||
/// let _ = mut_ref.write_all(b"Hello, world!\n"); | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! output_device_as_mut { | ||
($device:expr) => { | ||
&mut *$device.lock() | ||
}; | ||
} | ||
|
||
/// This struct represents an output device that can be used to write to the terminal. It | ||
/// is safe to clone. In order to write to it, see the examples in [Self::lock()] or | ||
/// [output_device_as_mut] macro. | ||
#[derive(Clone)] | ||
pub struct OutputDevice { | ||
pub resource: SafeRawTerminal, | ||
} | ||
|
||
impl OutputDevice { | ||
pub fn new_stdout() -> Self { | ||
Self { | ||
resource: Arc::new(StdMutex::new(std::io::stdout())), | ||
} | ||
} | ||
} | ||
|
||
impl OutputDevice { | ||
/// Locks the output device for writing. To use it, use the following code: | ||
/// | ||
/// ```rust | ||
/// use r3bl_core::{OutputDevice, LockedOutputDevice}; | ||
/// | ||
/// let device = OutputDevice::new_stdout(); | ||
/// let mut_ref: LockedOutputDevice<'_> = &mut *device.lock(); | ||
/// let _ = mut_ref.write_all(b"Hello, world!\n"); | ||
/// ``` | ||
/// | ||
/// This method returns a [`std::sync::MutexGuard`] which provides a mechanism to | ||
/// access the underlying resource in a thread-safe manner. The `MutexGuard` ensures | ||
/// that the resource is locked for the duration of the guard's lifetime, preventing | ||
/// other threads from accessing it simultaneously. | ||
pub fn lock(&self) -> std::sync::MutexGuard<'_, SendRawTerminal> { | ||
self.resource.lock().unwrap() | ||
} | ||
} |
File renamed without changes.
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 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 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 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 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 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,50 @@ | ||
/* | ||
* Copyright (c) 2024 R3BL LLC | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
use std::sync::Arc; | ||
|
||
use r3bl_core::{OutputDevice, StdMutex}; | ||
|
||
use crate::StdoutMock; | ||
|
||
impl StdoutMock { | ||
pub fn new_output_device() -> (OutputDevice, StdoutMock) { | ||
let stdout_mock = StdoutMock::default(); | ||
let this = OutputDevice { | ||
resource: Arc::new(StdMutex::new(stdout_mock.clone())), | ||
}; | ||
(this, stdout_mock) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use r3bl_core::{output_device_as_mut, LockedOutputDevice}; | ||
|
||
use crate::StdoutMock; | ||
|
||
#[test] | ||
fn test_output_device() { | ||
let (device, mock) = StdoutMock::new_output_device(); | ||
let mut_ref: LockedOutputDevice<'_> = output_device_as_mut!(device); | ||
let _ = mut_ref.write_all(b"Hello, world!\n"); | ||
assert_eq!( | ||
mock.get_copy_of_buffer_as_string_strip_ansi(), | ||
"Hello, world!\n" | ||
); | ||
} | ||
} |
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 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 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 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
Oops, something went wrong.