Convert colors::Flags to a bitflags enum

We use accessors and setters for all operations, so there's no benefit to
storing the modifiers as separate boolean fields.
This commit is contained in:
Mahmoud Al-Qudsi 2024-05-07 13:57:49 -05:00
parent 41a0fe2b1d
commit 92eee61fb5

View file

@ -1,3 +1,4 @@
use bitflags::bitflags;
use std::cmp::Ordering; use std::cmp::Ordering;
use crate::wchar::prelude::*; use crate::wchar::prelude::*;
@ -31,24 +32,16 @@ pub enum Type {
Reset, Reset,
} }
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)] bitflags! {
pub struct Flags { #[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
pub bold: bool, pub struct Flags: u8 {
pub underline: bool, const DEFAULT = 0;
pub italics: bool, const BOLD = 1<<0;
pub dim: bool, const UNDERLINE = 1<<1;
pub reverse: bool, const ITALICS = 1<<2;
} const DIM = 1<<3;
const REVERSE = 1<<4;
impl Flags { }
// const eval workaround
const DEFAULT: Self = Flags {
bold: false,
underline: false,
italics: false,
dim: false,
reverse: false,
};
} }
/// A type that represents a color. /// A type that represents a color.
@ -136,52 +129,52 @@ impl RgbColor {
/// Returns whether the color is bold. /// Returns whether the color is bold.
pub const fn is_bold(self) -> bool { pub const fn is_bold(self) -> bool {
self.flags.bold self.flags.contains(Flags::BOLD)
} }
/// Set whether the color is bold. /// Set whether the color is bold.
pub fn set_bold(&mut self, bold: bool) { pub fn set_bold(&mut self, bold: bool) {
self.flags.bold = bold; self.flags.set(Flags::BOLD, bold)
} }
/// Returns whether the color is underlined. /// Returns whether the color is underlined.
pub const fn is_underline(self) -> bool { pub const fn is_underline(self) -> bool {
self.flags.underline self.flags.contains(Flags::UNDERLINE)
} }
/// Set whether the color is underline. /// Set whether the color is underline.
pub fn set_underline(&mut self, underline: bool) { pub fn set_underline(&mut self, underline: bool) {
self.flags.underline = underline; self.flags.set(Flags::UNDERLINE, underline)
} }
/// Returns whether the color is italics. /// Returns whether the color is italics.
pub const fn is_italics(self) -> bool { pub const fn is_italics(self) -> bool {
self.flags.italics self.flags.contains(Flags::ITALICS)
} }
/// Set whether the color is italics. /// Set whether the color is italics.
pub fn set_italics(&mut self, italics: bool) { pub fn set_italics(&mut self, italics: bool) {
self.flags.italics = italics; self.flags.set(Flags::ITALICS, italics)
} }
/// Returns whether the color is dim. /// Returns whether the color is dim.
pub const fn is_dim(self) -> bool { pub const fn is_dim(self) -> bool {
self.flags.dim self.flags.contains(Flags::DIM)
} }
/// Set whether the color is dim. /// Set whether the color is dim.
pub fn set_dim(&mut self, dim: bool) { pub fn set_dim(&mut self, dim: bool) {
self.flags.dim = dim; self.flags.set(Flags::DIM, dim)
} }
/// Returns whether the color is reverse. /// Returns whether the color is reverse.
pub const fn is_reverse(self) -> bool { pub const fn is_reverse(self) -> bool {
self.flags.reverse self.flags.contains(Flags::REVERSE)
} }
/// Set whether the color is reverse. /// Set whether the color is reverse.
pub fn set_reverse(&mut self, reverse: bool) { pub fn set_reverse(&mut self, reverse: bool) {
self.flags.reverse = reverse; self.flags.set(Flags::REVERSE, reverse)
} }
/// Returns the name index for the given color. Requires that the color be named or RGB. /// Returns the name index for the given color. Requires that the color be named or RGB.