#![allow(clippy::from_over_into)]
use std::ops::{BitOr, BitXor};
use ascii::AsciiChar;
use crate::{
gen::*,
normal::{Attribute, Attributes},
shims::{
constants::A_ATTRIBUTES,
ncurses::{chtype, attr_t},
}
};
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash)]
pub struct ChtypeChar {
inner: chtype
}
impl ChtypeChar {
pub fn new(ch: AsciiChar) -> Self {
Self { inner: chtype::from(ch.as_byte()) }
}
pub fn from_chtype(raw: chtype) -> Self {
Self { inner: raw }
}
pub fn as_byte(self) -> u8 {
self.inner.to_be_bytes()[3]
}
pub fn as_char(self) -> char {
char::from(self.as_byte())
}
pub fn as_ascii_char(self) -> AsciiChar {
AsciiChar::new(self.as_char())
}
pub fn is_alphabetic(self) -> bool {
self.as_ascii_char().is_alphabetic()
}
pub fn is_digit(self) -> bool {
self.as_ascii_char().is_ascii_digit()
}
pub fn is_alphanumeric(self) -> bool {
self.as_ascii_char().is_alphanumeric()
}
pub fn is_blank(self) -> bool {
self.as_ascii_char().is_ascii_blank()
}
pub fn is_whitespace(self) -> bool {
self.as_ascii_char().is_whitespace()
}
pub fn is_control(self) -> bool {
self.as_ascii_char().is_ascii_control()
}
pub fn get_attributes(self) -> Attributes {
Attributes::_from(None, self.inner & A_ATTRIBUTES)
}
}
impl BitOr<Attributes> for ChtypeChar {
type Output = Self;
fn bitor(self, rhs: Attributes) -> Self::Output {
Self { inner: self.inner | rhs.as_attr_t() }
}
}
impl BitXor<Attributes> for ChtypeChar {
type Output = Self;
fn bitxor(self, rhs: Attributes) -> Self::Output {
Self { inner: self.inner ^ rhs.as_attr_t() }
}
}
impl BitOr<Attribute> for ChtypeChar {
type Output = Self;
fn bitor(self, rhs: Attribute) -> Self::Output {
let attr: attr_t = rhs.into();
Self { inner: self.inner | attr }
}
}
impl BitXor<Attribute> for ChtypeChar {
type Output = Self;
fn bitxor(self, rhs: Attribute) -> Self::Output {
let attr: attr_t = rhs.into();
Self { inner: self.inner ^ attr }
}
}
impl From<chtype> for ChtypeChar {
fn from(raw: chtype) -> Self {
Self { inner: raw }
}
}
impl Into<chtype> for ChtypeChar {
fn into(self) -> chtype {
self.inner.to_owned()
}
}
#[test]
fn chtype_char_test() {
assert_eq!(ChtypeChar::new(AsciiChar::new('s')).as_ascii_char(), AsciiChar::new('s'));
}
#[test]
fn chtype_char_get_attributes() {
let ch = ChtypeChar::new(AsciiChar::new('s'));
let attrs = Attributes::default().set_bold(true).set_dim(true);
let c = ch | attrs;
assert_eq!(c.as_char(), 's');
assert_eq!(c.get_attributes(), attrs);
assert_eq!(c.get_attributes().is_normal(), false);
assert_eq!(c.get_attributes().is_bold(), true);
assert_eq!(c.get_attributes().is_dim(), true);
assert_eq!(c.get_attributes().is_underline(), false);
}