use std::fmt;
#[cfg(any(feature = "blocking", feature = "tokio"))]
use std::sync::Arc;
use crate::backend::ConPtyBackend;
#[cfg(any(feature = "blocking", feature = "tokio"))]
use crate::core::session::Session as SessionCore;
use crate::error::Result;
use crate::size::Size;
use crate::status::ExitStatus;
#[derive(Debug, Clone, Default)]
pub struct SessionOptions {
size: Size,
backend: Option<ConPtyBackend>,
}
impl SessionOptions {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub const fn size(mut self, size: Size) -> Self {
self.size = size;
self
}
#[must_use]
pub fn backend(mut self, backend: ConPtyBackend) -> Self {
self.backend = Some(backend);
self
}
#[cfg(any(feature = "blocking", feature = "tokio"))]
#[must_use]
pub(super) fn into_parts(self) -> (Size, Option<ConPtyBackend>) {
(self.size, self.backend)
}
}
pub struct SessionOutput {
status: ExitStatus,
bytes: Vec<u8>,
}
impl SessionOutput {
#[cfg(any(feature = "blocking", feature = "tokio"))]
#[must_use]
pub(super) const fn new(status: ExitStatus, bytes: Vec<u8>) -> Self {
Self { status, bytes }
}
#[must_use]
pub const fn status(&self) -> ExitStatus {
self.status
}
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}
#[must_use]
pub fn into_bytes(self) -> Vec<u8> {
self.bytes
}
}
impl fmt::Debug for SessionOutput {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SessionOutput")
.field("status", &self.status)
.field("bytes", &format_args!("{} bytes", self.bytes.len()))
.finish()
}
}
#[derive(Clone)]
pub struct PtyController {
#[cfg(any(feature = "blocking", feature = "tokio"))]
pub(super) inner: Arc<SessionCore>,
#[cfg(not(any(feature = "blocking", feature = "tokio")))]
uninhabited: std::convert::Infallible,
}
impl PtyController {
#[cfg(any(feature = "blocking", feature = "tokio"))]
pub(super) const fn new(inner: Arc<SessionCore>) -> Self {
Self { inner }
}
}
#[cfg(any(feature = "blocking", feature = "tokio"))]
impl PtyController {
pub fn resize(&self, size: Size) -> Result<()> {
self.inner.resize(size)
}
#[must_use]
pub fn size(&self) -> Size {
self.inner.size()
}
pub fn clear(&self) -> Result<()> {
self.inner.clear()
}
#[must_use]
pub fn supports_clear(&self) -> bool {
self.inner.supports_clear()
}
#[must_use]
#[cfg(test)]
pub(crate) fn supports_release(&self) -> bool {
self.inner.supports_release()
}
#[must_use]
#[cfg(test)]
pub(crate) fn reader_finished(&self) -> bool {
self.inner.reader_finished()
}
#[must_use]
#[cfg(test)]
pub(crate) fn backend_kind(&self) -> &crate::backend::BackendKind {
self.inner.backend_kind()
}
}
#[cfg(any(feature = "blocking", feature = "tokio"))]
impl fmt::Debug for PtyController {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PtyController")
.field("size", &self.inner.size())
.field("supports_clear", &self.inner.supports_clear())
.finish_non_exhaustive()
}
}