// This file was generated by gir (https://github.com/gtk-rs/gir)
// from
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use crate::{ffi, PtyFlags};
use glib::{prelude::*, translate::*};

glib::wrapper! {
    #[doc(alias = "VtePty")]
    pub struct Pty(Object<ffi::VtePty, ffi::VtePtyClass>) @implements gio::Initable;

    match fn {
        type_ => || ffi::vte_pty_get_type(),
    }
}

impl Pty {
    #[doc(alias = "vte_pty_new_sync")]
    pub fn new_sync(
        flags: PtyFlags,
        cancellable: Option<&impl IsA<gio::Cancellable>>,
    ) -> Result<Pty, glib::Error> {
        assert_initialized_main_thread!();
        unsafe {
            let mut error = std::ptr::null_mut();
            let ret = ffi::vte_pty_new_sync(
                flags.into_glib(),
                cancellable.map(|p| p.as_ref()).to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    // rustdoc-stripper-ignore-next
    /// Creates a new builder-pattern struct instance to construct [`Pty`] objects.
    ///
    /// This method returns an instance of [`PtyBuilder`](crate::builders::PtyBuilder) which can be used to create [`Pty`] objects.
    pub fn builder() -> PtyBuilder {
        PtyBuilder::new()
    }

    #[doc(alias = "vte_pty_child_setup")]
    pub fn child_setup(&self) {
        unsafe {
            ffi::vte_pty_child_setup(self.to_glib_none().0);
        }
    }

    #[doc(alias = "vte_pty_get_size")]
    #[doc(alias = "get_size")]
    pub fn size(&self) -> Result<(i32, i32), glib::Error> {
        unsafe {
            let mut rows = std::mem::MaybeUninit::uninit();
            let mut columns = std::mem::MaybeUninit::uninit();
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::vte_pty_get_size(
                self.to_glib_none().0,
                rows.as_mut_ptr(),
                columns.as_mut_ptr(),
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok((rows.assume_init(), columns.assume_init()))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[doc(alias = "vte_pty_set_size")]
    pub fn set_size(&self, rows: i32, columns: i32) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::vte_pty_set_size(self.to_glib_none().0, rows, columns, &mut error);
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[doc(alias = "vte_pty_set_utf8")]
    pub fn set_utf8(&self, utf8: bool) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::vte_pty_set_utf8(self.to_glib_none().0, utf8.into_glib(), &mut error);
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    pub fn flags(&self) -> PtyFlags {
        ObjectExt::property(self, "flags")
    }
}

// rustdoc-stripper-ignore-next
/// A [builder-pattern] type to construct [`Pty`] objects.
///
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
#[must_use = "The builder must be built to be used"]
pub struct PtyBuilder {
    builder: glib::object::ObjectBuilder<'static, Pty>,
}

impl PtyBuilder {
    fn new() -> Self {
        Self {
            builder: glib::object::Object::builder(),
        }
    }

    pub fn fd(self, fd: i32) -> Self {
        Self {
            builder: self.builder.property("fd", fd),
        }
    }

    pub fn flags(self, flags: PtyFlags) -> Self {
        Self {
            builder: self.builder.property("flags", flags),
        }
    }

    // rustdoc-stripper-ignore-next
    /// Build the [`Pty`].
    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
    pub fn build(self) -> Pty {
        self.builder.build()
    }
}
