-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move code to api * Add palette::from_csv * Refactor palette::from_str with tests * Add tests to api::font::from_bytes * Add TODO comments to fs code * Rename kernel and user dirs to sys and usr * Add missing files from migration * Move console::Style to api * Add more missing changes
- Loading branch information
Showing
75 changed files
with
834 additions
and
774 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use core::fmt; | ||
|
||
pub struct Style { | ||
foreground: Option<usize>, | ||
background: Option<usize>, | ||
} | ||
|
||
impl Style { | ||
pub fn reset() -> Self { | ||
Self { foreground: None, background: None } | ||
} | ||
|
||
pub fn foreground(name: &str) -> Self { | ||
Self { foreground: color_to_fg(name), background: None } | ||
} | ||
|
||
pub fn with_foreground(self, name: &str) -> Self { | ||
Self { foreground: color_to_fg(name), background: self.background } | ||
} | ||
|
||
pub fn background(name: &str) -> Self { | ||
Self { foreground: None, background: color_to_bg(name) } | ||
} | ||
|
||
pub fn with_background(self, name: &str) -> Self { | ||
Self { foreground: self.foreground, background: color_to_bg(name) } | ||
} | ||
|
||
pub fn color(name: &str) -> Self { | ||
Self::foreground(name) | ||
} | ||
|
||
pub fn with_color(self, name: &str) -> Self { | ||
self.with_foreground(name) | ||
} | ||
} | ||
|
||
impl fmt::Display for Style { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
if let Some(fg) = self.foreground { | ||
if let Some(bg) = self.background { | ||
write!(f, "\x1b[{};{}m", fg, bg) | ||
} else { | ||
write!(f, "\x1b[{}m", fg) | ||
} | ||
} else if let Some(bg) = self.background { | ||
write!(f, "\x1b[{}m", bg) | ||
} else { | ||
write!(f, "\x1b[0m") | ||
} | ||
} | ||
} | ||
|
||
fn color_to_fg(name: &str) -> Option<usize> { | ||
match name { | ||
"Black" => Some(30), | ||
"Red" => Some(31), | ||
"Green" => Some(32), | ||
"Brown" => Some(33), | ||
"Blue" => Some(34), | ||
"Magenta" => Some(35), | ||
"Cyan" => Some(36), | ||
"LightGray" => Some(37), | ||
"DarkGray" => Some(90), | ||
"LightRed" => Some(91), | ||
"LightGreen" => Some(92), | ||
"Yellow" => Some(93), | ||
"LightBlue" => Some(94), | ||
"Pink" => Some(95), | ||
"LightCyan" => Some(96), | ||
"White" => Some(97), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn color_to_bg(name: &str) -> Option<usize> { | ||
if let Some(fg) = color_to_fg(name) { | ||
Some(fg + 10) | ||
} else { | ||
None | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
pub mod console; | ||
pub mod font; | ||
pub mod syscall; | ||
pub mod vga; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/// The standard color palette in VGA text mode | ||
#[allow(dead_code)] | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
#[repr(u8)] | ||
pub enum Color { | ||
Black = 0, | ||
Blue = 1, | ||
Green = 2, | ||
Cyan = 3, | ||
Red = 4, | ||
Magenta = 5, | ||
Brown = 6, | ||
LightGray = 7, | ||
DarkGray = 8, | ||
LightBlue = 9, | ||
LightGreen = 10, | ||
LightCyan = 11, | ||
LightRed = 12, | ||
Pink = 13, | ||
Yellow = 14, | ||
White = 15, | ||
} | ||
|
||
const COLORS: [Color; 16] = [ | ||
Color::Black, | ||
Color::Blue, | ||
Color::Green, | ||
Color::Cyan, | ||
Color::Red, | ||
Color::Magenta, | ||
Color::Brown, | ||
Color::LightGray, | ||
Color::DarkGray, | ||
Color::LightBlue, | ||
Color::LightGreen, | ||
Color::LightCyan, | ||
Color::LightRed, | ||
Color::Pink, | ||
Color::Yellow, | ||
Color::White, | ||
]; | ||
|
||
pub fn colors() -> [Color; 16] { | ||
COLORS | ||
} | ||
|
||
pub fn from_index(index: usize) -> Color { | ||
COLORS[index] | ||
} | ||
|
||
pub fn from_ansi(code: u8) -> Color { | ||
match code { | ||
30 => Color::Black, | ||
31 => Color::Red, | ||
32 => Color::Green, | ||
33 => Color::Brown, | ||
34 => Color::Blue, | ||
35 => Color::Magenta, | ||
36 => Color::Cyan, | ||
37 => Color::LightGray, | ||
90 => Color::DarkGray, | ||
91 => Color::LightRed, | ||
92 => Color::LightGreen, | ||
93 => Color::Yellow, | ||
94 => Color::LightBlue, | ||
95 => Color::Pink, | ||
96 => Color::LightCyan, | ||
97 => Color::White, | ||
_ => Color::Black, // Error | ||
} | ||
} | ||
|
||
impl Color { | ||
pub fn to_palette_code(&self) -> u8 { | ||
match self { | ||
Color::Black => 0x00, | ||
Color::Blue => 0x01, | ||
Color::Green => 0x02, | ||
Color::Cyan => 0x03, | ||
Color::Red => 0x04, | ||
Color::Magenta => 0x05, | ||
Color::LightGray => 0x07, | ||
Color::Brown => 0x14, | ||
Color::DarkGray => 0x38, | ||
Color::LightBlue => 0x39, | ||
Color::LightGreen => 0x3A, | ||
Color::LightCyan => 0x3B, | ||
Color::LightRed => 0x3C, | ||
Color::Pink => 0x3D, | ||
Color::Yellow => 0x3E, | ||
Color::White => 0x3F, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub mod color; | ||
pub mod palette; | ||
|
||
pub use color::Color; | ||
pub use palette::Palette; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use alloc::vec::Vec; | ||
use core::convert::TryInto; | ||
|
||
pub struct Palette { | ||
pub colors: [(u8, u8, u8, u8); 16] | ||
} | ||
|
||
pub fn from_csv(s: &str) -> Result<Palette, ()> { | ||
let colors: Vec<_> = s.split("\n").filter_map(|line| { | ||
let line = line.split("#").next().unwrap(); // Remove comments | ||
let color: Vec<u8> = line.split(",").filter_map(|value| { | ||
let radix = if value.contains("0x") { 16 } else { 10 }; | ||
let value = value.trim().trim_start_matches("0x"); | ||
u8::from_str_radix(value, radix).ok() | ||
}).collect(); | ||
if color.len() == 4 { // Color index + rgb values | ||
Some((color[0], color[1], color[2], color[3])) | ||
} else { | ||
None | ||
} | ||
}).collect(); | ||
if let Ok(colors) = colors.try_into() { // Array of 16 colors | ||
Ok(Palette { colors }) | ||
} else { | ||
Err(()) | ||
} | ||
} | ||
|
||
#[test_case] | ||
fn parse_palette_csv() { | ||
assert!(from_csv("").is_err()); | ||
assert!(from_csv("0,0,0,0").is_err()); | ||
|
||
let s = include_str!("../../../dsk/ini/palette.csv"); | ||
let palette = from_csv(s).unwrap(); | ||
assert_eq!(palette.colors[0x03].0, 0x03); | ||
assert_eq!(palette.colors[0x0D].2, 0x86); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.