// SPDX-FileCopyrightText: 2023 CELESTIFYX Team
// SPDX-License-Identifier: GPL-3.0-or-later

use std::io::Stdin;

use anyhow::{
    Context as _,
    Result
};

use rustix::termios::{
    LocalModes,
    OptionalActions,
    Termios,
    tcgetattr,
    tcsetattr
};

pub(super) fn enable_raw_mode(stdin: &Stdin) -> Result<Termios> {
    let old: Termios = tcgetattr(stdin).context("Failed to receive terminal attributes")?;

    let mut new: Termios = old.clone();
    new.local_modes &= !LocalModes::ICANON;
    new.local_modes &= !LocalModes::ECHO;

    tcsetattr(stdin, OptionalActions::Now, &new).context("Failed to update terminal attributes")?;
    Ok(old)
}
