pancurses/colorpair.rs
1use std::ops::BitOr;
2use super::{chtype, COLOR_PAIR};
3use crate::attributes::{Attribute, Attributes};
4
5#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
6pub struct ColorPair(pub u8);
7
8impl From<ColorPair> for chtype {
9 fn from(color_pair: ColorPair) -> chtype {
10 COLOR_PAIR(chtype::from(color_pair.0))
11 }
12}
13
14/// Implement the | operator for setting a color pair on an `Attributes` object
15///
16/// # Example
17///
18/// ```
19/// use pancurses::{Attribute, Attributes};
20/// use pancurses::colorpair::ColorPair;
21///
22/// let mut attributes = Attributes::new();
23/// assert!(attributes.color_pair().0 == 0);
24/// attributes = attributes | ColorPair(1);
25/// assert!(attributes.color_pair().0 == 1);
26/// ```
27impl BitOr<ColorPair> for Attributes {
28 type Output = Attributes;
29
30 fn bitor(mut self, rhs: ColorPair) -> Attributes {
31 self.set_color_pair(rhs);
32 self
33 }
34}
35
36/// Implement the | operator for combining a `ColorPair` and an `Attribute` to produce `Attributes`
37///
38/// # Example
39///
40/// ```
41/// use pancurses::Attribute;
42/// use pancurses::colorpair::ColorPair;
43///
44/// let attributes = ColorPair(5) | Attribute::Blink;
45/// assert!(attributes.color_pair().0 == 5);
46/// assert!(!attributes.is_bold());
47/// assert!(attributes.is_blink());
48/// ```
49impl BitOr<Attribute> for ColorPair {
50 type Output = Attributes;
51
52 fn bitor(self, rhs: Attribute) -> Attributes {
53 Attributes::new() | self | rhs
54 }
55}
56
57/// Implement the | operator for combining an `Attribute` and a `ColorPair` to produce `Attributes`
58///
59/// # Example
60///
61/// ```
62/// use pancurses::Attribute;
63/// use pancurses::colorpair::ColorPair;
64///
65/// let attributes = Attribute::Blink | ColorPair(2);
66/// assert!(attributes.color_pair().0 == 2);
67/// assert!(!attributes.is_bold());
68/// assert!(attributes.is_blink());
69/// ```
70impl BitOr<ColorPair> for Attribute {
71 type Output = Attributes;
72
73 fn bitor(self, rhs: ColorPair) -> Attributes {
74 Attributes::new() | self | rhs
75 }
76}