From 365480362c180f6a3c0784911020a1a7d52753e2 Mon Sep 17 00:00:00 2001 From: Nazmul Idris Date: Mon, 30 Sep 2024 14:04:19 -0500 Subject: [PATCH] [all] Move common types for input event and terminal output from terminal_async into core --- Cargo.lock | 2 ++ core/Cargo.toml | 4 +++ core/src/common/mod.rs | 2 ++ core/src/common/type_aliases.rs | 35 +++++++++++++++++++ terminal_async/examples/spinner.rs | 2 +- terminal_async/examples/terminal_async.rs | 20 +++++------ terminal_async/src/lib.rs | 16 +++------ .../src/public_api/terminal_async.rs | 7 ++-- terminal_async/src/readline_impl/readline.rs | 9 ++--- 9 files changed, 66 insertions(+), 31 deletions(-) create mode 100644 core/src/common/type_aliases.rs diff --git a/Cargo.lock b/Cargo.lock index 473d17d8e..01aea734b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1743,10 +1743,12 @@ name = "r3bl_core" version = "0.9.16" dependencies = [ "assert_cmd", + "async-stream", "bincode", "chrono", "colorgrad", "crossterm 0.28.1", + "futures-core", "futures-util", "kv", "log", diff --git a/core/Cargo.toml b/core/Cargo.toml index 7125755f7..98b6e7b3f 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -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" diff --git a/core/src/common/mod.rs b/core/src/common/mod.rs index cd73afe8e..fd4939f3b 100644 --- a/core/src/common/mod.rs +++ b/core/src/common/mod.rs @@ -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::*; diff --git a/core/src/common/type_aliases.rs b/core/src/common/type_aliases.rs new file mode 100644 index 000000000..2478b53a9 --- /dev/null +++ b/core/src/common/type_aliases.rs @@ -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 = std::sync::Mutex; + +/// 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`. +pub type SafeRawTerminal = Arc>; + +/// Type alias for crossterm streaming (input) event result. +pub type CrosstermEventResult = Result; +/// Type alias for a pinned stream that is async safe. `T` is usually +/// [CrosstermEventResult]. +pub type PinnedInputStream = Pin>>; diff --git a/terminal_async/examples/spinner.rs b/terminal_async/examples/spinner.rs index 2e172bd7c..56b3c9f28 100644 --- a/terminal_async/examples/spinner.rs +++ b/terminal_async/examples/spinner.rs @@ -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, diff --git a/terminal_async/examples/terminal_async.rs b/terminal_async/examples/terminal_async.rs index 5e3fa901f..112e8dbb0 100644 --- a/terminal_async/examples/terminal_async.rs +++ b/terminal_async/examples/terminal_async.rs @@ -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}; @@ -252,8 +256,6 @@ mod task_2 { } mod process_input_event { - use std::str::FromStr; - use super::*; pub fn process( @@ -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; diff --git a/terminal_async/src/lib.rs b/terminal_async/src/lib.rs index b4fd39bf0..79e18d5c8 100644 --- a/terminal_async/src/lib.rs +++ b/terminal_async/src/lib.rs @@ -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 = std::sync::Mutex; - -pub type SendRawTerminal = dyn std::io::Write + Send; -pub type SafeRawTerminal = Arc>; +// r3bl-open-core crates. +use r3bl_core::{StdMutex, SendRawTerminal, SafeRawTerminal}; +// Type aliases. pub type SafeLineState = Arc>; pub type SafeHistory = Arc>; @@ -484,9 +479,6 @@ pub type SafeBool = Arc>; pub type PauseBuffer = VecDeque; pub type SafePauseBuffer = Arc>; -pub type CrosstermEventResult = Result; -pub type PinnedInputStream = Pin>>; - // Constants. pub const CHANNEL_CAPACITY: usize = 1_000; pub const HISTORY_SIZE_MAX: usize = 1_000; diff --git a/terminal_async/src/public_api/terminal_async.rs b/terminal_async/src/public_api/terminal_async.rs index a76996f35..5133a9de5 100644 --- a/terminal_async/src/public_api/terminal_async.rs +++ b/terminal_async/src/public_api/terminal_async.rs @@ -24,7 +24,10 @@ 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, @@ -32,7 +35,7 @@ use r3bl_tuify::{is_fully_uninteractive_terminal, StdoutIsPipedResult, TTYResult}; -use crate::{CrosstermEventResult, PinnedInputStream, Readline, ReadlineEvent, StdMutex}; +use crate::{Readline, ReadlineEvent, StdMutex}; pub struct TerminalAsync { pub readline: Readline, diff --git a/terminal_async/src/readline_impl/readline.rs b/terminal_async/src/readline_impl/readline.rs index fc75a90bb..c86c67319 100644 --- a/terminal_async/src/readline_impl/readline.rs +++ b/terminal_async/src/readline_impl/readline.rs @@ -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,