mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-26 12:53:13 +00:00
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:
parent
41a0fe2b1d
commit
92eee61fb5
1 changed files with 21 additions and 28 deletions
49
src/color.rs
49
src/color.rs
|
@ -1,3 +1,4 @@
|
|||
use bitflags::bitflags;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
use crate::wchar::prelude::*;
|
||||
|
@ -31,24 +32,16 @@ pub enum Type {
|
|||
Reset,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
|
||||
pub struct Flags {
|
||||
pub bold: bool,
|
||||
pub underline: bool,
|
||||
pub italics: bool,
|
||||
pub dim: bool,
|
||||
pub reverse: bool,
|
||||
}
|
||||
|
||||
impl Flags {
|
||||
// const eval workaround
|
||||
const DEFAULT: Self = Flags {
|
||||
bold: false,
|
||||
underline: false,
|
||||
italics: false,
|
||||
dim: false,
|
||||
reverse: false,
|
||||
};
|
||||
bitflags! {
|
||||
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
|
||||
pub struct Flags: u8 {
|
||||
const DEFAULT = 0;
|
||||
const BOLD = 1<<0;
|
||||
const UNDERLINE = 1<<1;
|
||||
const ITALICS = 1<<2;
|
||||
const DIM = 1<<3;
|
||||
const REVERSE = 1<<4;
|
||||
}
|
||||
}
|
||||
|
||||
/// A type that represents a color.
|
||||
|
@ -136,52 +129,52 @@ impl RgbColor {
|
|||
|
||||
/// Returns whether the color is bold.
|
||||
pub const fn is_bold(self) -> bool {
|
||||
self.flags.bold
|
||||
self.flags.contains(Flags::BOLD)
|
||||
}
|
||||
|
||||
/// Set whether the color is bold.
|
||||
pub fn set_bold(&mut self, bold: bool) {
|
||||
self.flags.bold = bold;
|
||||
self.flags.set(Flags::BOLD, bold)
|
||||
}
|
||||
|
||||
/// Returns whether the color is underlined.
|
||||
pub const fn is_underline(self) -> bool {
|
||||
self.flags.underline
|
||||
self.flags.contains(Flags::UNDERLINE)
|
||||
}
|
||||
|
||||
/// Set whether the color is underline.
|
||||
pub fn set_underline(&mut self, underline: bool) {
|
||||
self.flags.underline = underline;
|
||||
self.flags.set(Flags::UNDERLINE, underline)
|
||||
}
|
||||
|
||||
/// Returns whether the color is italics.
|
||||
pub const fn is_italics(self) -> bool {
|
||||
self.flags.italics
|
||||
self.flags.contains(Flags::ITALICS)
|
||||
}
|
||||
|
||||
/// Set whether the color is italics.
|
||||
pub fn set_italics(&mut self, italics: bool) {
|
||||
self.flags.italics = italics;
|
||||
self.flags.set(Flags::ITALICS, italics)
|
||||
}
|
||||
|
||||
/// Returns whether the color is dim.
|
||||
pub const fn is_dim(self) -> bool {
|
||||
self.flags.dim
|
||||
self.flags.contains(Flags::DIM)
|
||||
}
|
||||
|
||||
/// Set whether the color is dim.
|
||||
pub fn set_dim(&mut self, dim: bool) {
|
||||
self.flags.dim = dim;
|
||||
self.flags.set(Flags::DIM, dim)
|
||||
}
|
||||
|
||||
/// Returns whether the color is reverse.
|
||||
pub const fn is_reverse(self) -> bool {
|
||||
self.flags.reverse
|
||||
self.flags.contains(Flags::REVERSE)
|
||||
}
|
||||
|
||||
/// Set whether the color is reverse.
|
||||
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.
|
||||
|
|
Loading…
Reference in a new issue