Skip to content

Commit

Permalink
[all] Move common types for input event and terminal output from term…
Browse files Browse the repository at this point in the history
…inal_async into core
  • Loading branch information
nazmulidris committed Oct 19, 2024
1 parent f0b162d commit 3654803
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 31 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ bincode = { version = "1.3.3" }
# on top of `sled` which currently does not support access across multiple processes.
kv = { version = "0.24.0", features = ["json-value", "bincode-value"] }

# Async stream for DI and testing.
futures-core = "0.3.30"
async-stream = "0.3.5"

[dev-dependencies]
# for assert_eq! macro
pretty_assertions = "1.4.0"
Expand Down
2 changes: 2 additions & 0 deletions core/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
pub mod common_enums;
pub mod common_math;
pub mod common_result_and_error;
pub mod type_aliases;
pub mod miette_setup_global_report_handler;

// Re-export.
pub use common_enums::*;
pub use common_math::*;
pub use common_result_and_error::*;
pub use type_aliases::*;
pub use miette_setup_global_report_handler::*;
35 changes: 35 additions & 0 deletions core/src/common/type_aliases.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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::{io::Error, pin::Pin, sync::Arc};

use crossterm::event::Event;
use futures_core::Stream;

/// Disambiguate the type of `StdMutex` from stdlib and tokio to avoid conflicts.
pub type StdMutex<T> = std::sync::Mutex<T>;

/// Type alias for a `Send`-able output device (raw terminal, SharedWriter, etc).
pub type SendRawTerminal = dyn std::io::Write + Send;
/// Type alias for a `Send`-able raw terminal wrapped in an `Arc<StdMutex>`.
pub type SafeRawTerminal = Arc<StdMutex<SendRawTerminal>>;

/// Type alias for crossterm streaming (input) event result.
pub type CrosstermEventResult = Result<Event, Error>;
/// Type alias for a pinned stream that is async safe. `T` is usually
/// [CrosstermEventResult].
pub type PinnedInputStream<T> = Pin<Box<dyn Stream<Item = T>>>;
2 changes: 1 addition & 1 deletion terminal_async/examples/spinner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ use std::{io::{stderr, Write},
sync::Arc,
time::Duration};

use r3bl_core::StdMutex;
use r3bl_terminal_async::{Spinner,
SpinnerColor,
SpinnerStyle,
SpinnerTemplate,
StdMutex,
TerminalAsync,
ARTIFICIAL_UI_DELAY,
DELAY_MS,
Expand Down
20 changes: 8 additions & 12 deletions terminal_async/examples/terminal_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,25 @@
* limitations under the License.
*/

use std::{io::{stderr, Write},
use std::{fs,
io::{stderr, Write},
ops::ControlFlow,
path::{self, PathBuf},
str::FromStr as _,
sync::Arc,
time::Duration};

use crossterm::style::Stylize as _;
use miette::IntoDiagnostic as _;
use miette::{miette, IntoDiagnostic as _};
use r3bl_core::{tracing_logging::tracing_config::TracingConfig,
DisplayPreference,
SharedWriter};
SendRawTerminal,
SharedWriter,
StdMutex};
use r3bl_terminal_async::{Readline,
ReadlineEvent,
Spinner,
SpinnerStyle,
StdMutex,
TerminalAsync};
use strum::IntoEnumIterator;
use strum_macros::{Display, EnumIter, EnumString};
Expand Down Expand Up @@ -252,8 +256,6 @@ mod task_2 {
}

mod process_input_event {
use std::str::FromStr;

use super::*;

pub fn process(
Expand Down Expand Up @@ -426,12 +428,6 @@ mod long_running_task {
}

pub mod file_walker {
use std::{fs,
path::{self, PathBuf}};

use miette::miette;
use r3bl_terminal_async::SendRawTerminal;

use super::*;

pub const FOLDER_DELIM: &str = std::path::MAIN_SEPARATOR_STR;
Expand Down
16 changes: 4 additions & 12 deletions terminal_async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,17 +465,12 @@ pub use readline_impl::*;
pub use spinner_impl::*;

// External crates.
use std::{collections::VecDeque, io::Error, pin::Pin, sync::Arc};
use crossterm::event::Event;
use futures_core::Stream;
use std::{collections::VecDeque, sync::Arc};

// Type aliases.

pub type StdMutex<T> = std::sync::Mutex<T>;

pub type SendRawTerminal = dyn std::io::Write + Send;
pub type SafeRawTerminal = Arc<StdMutex<SendRawTerminal>>;
// r3bl-open-core crates.
use r3bl_core::{StdMutex, SendRawTerminal, SafeRawTerminal};

// Type aliases.
pub type SafeLineState = Arc<StdMutex<LineState>>;
pub type SafeHistory = Arc<StdMutex<History>>;

Expand All @@ -484,9 +479,6 @@ pub type SafeBool = Arc<StdMutex<bool>>;
pub type PauseBuffer = VecDeque<r3bl_core::Text>;
pub type SafePauseBuffer = Arc<StdMutex<PauseBuffer>>;

pub type CrosstermEventResult = Result<Event, Error>;
pub type PinnedInputStream<T> = Pin<Box<dyn Stream<Item = T>>>;

// Constants.
pub const CHANNEL_CAPACITY: usize = 1_000;
pub const HISTORY_SIZE_MAX: usize = 1_000;
7 changes: 5 additions & 2 deletions terminal_async/src/public_api/terminal_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ use crossterm::{cursor::MoveToColumn,
terminal::{Clear, ClearType}};
use futures_util::FutureExt as _;
use miette::IntoDiagnostic as _;
use r3bl_core::{LineStateControlSignal, SharedWriter};
use r3bl_core::{CrosstermEventResult,
LineStateControlSignal,
PinnedInputStream,
SharedWriter};
use r3bl_tuify::{is_fully_uninteractive_terminal,
is_stdin_piped,
is_stdout_piped,
StdinIsPipedResult,
StdoutIsPipedResult,
TTYResult};

use crate::{CrosstermEventResult, PinnedInputStream, Readline, ReadlineEvent, StdMutex};
use crate::{Readline, ReadlineEvent, StdMutex};

pub struct TerminalAsync {
pub readline: Readline,
Expand Down
9 changes: 5 additions & 4 deletions terminal_async/src/readline_impl/readline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ use std::{io::{self, Write},
use crossterm::{terminal::{self, disable_raw_mode, Clear},
QueueableCommand};
use futures_util::StreamExt;
use r3bl_core::{LineStateControlSignal, SharedWriter};
use r3bl_core::{CrosstermEventResult,
LineStateControlSignal,
PinnedInputStream,
SharedWriter};
use thiserror::Error;
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};

use crate::{CrosstermEventResult,
History,
use crate::{History,
LineState,
LineStateLiveness,
PauseBuffer,
PinnedInputStream,
SafeHistory,
SafeLineState,
SafePauseBuffer,
Expand Down

0 comments on commit 3654803

Please sign in to comment.