4
0

style.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. use std::io::{self, stdout, Write};
  2. use crossterm::{style::*, ExecutableCommand};
  3. pub struct StyleManager;
  4. #[allow(dead_code)]
  5. impl StyleManager {
  6. #[inline]
  7. pub fn set_foreground_color(color: Color) -> io::Result<()> {
  8. stdout().execute(SetForegroundColor(color)).unwrap().flush()
  9. }
  10. #[inline]
  11. pub fn set_background_color(color: Color) -> io::Result<()> {
  12. stdout().execute(SetBackgroundColor(color)).unwrap().flush()
  13. }
  14. #[inline]
  15. pub fn set_underline_color(color: Color) -> io::Result<()> {
  16. stdout().execute(SetUnderlineColor(color)).unwrap().flush()
  17. }
  18. #[inline]
  19. pub fn set_color(fg: Option<Color>, bg: Option<Color>) -> io::Result<()> {
  20. stdout()
  21. .execute(SetColors(Colors {
  22. foreground: fg,
  23. background: bg,
  24. }))
  25. .unwrap()
  26. .flush()
  27. }
  28. #[inline]
  29. pub fn set_attr(attr: Attribute) -> io::Result<()> {
  30. stdout().execute(SetAttribute(attr)).unwrap().flush()
  31. }
  32. #[inline]
  33. pub fn set_attrs(attr: Attributes) -> io::Result<()> {
  34. stdout().execute(SetAttributes(attr)).unwrap().flush()
  35. }
  36. #[inline]
  37. pub fn set_style(style: ContentStyle) -> io::Result<()> {
  38. stdout().execute(SetStyle(style)).unwrap().flush()
  39. }
  40. #[inline]
  41. pub fn reset_color() -> io::Result<()> {
  42. stdout().execute(ResetColor).unwrap().flush()
  43. }
  44. }