mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Implement the fmt::Write trait and print something with the write! macro
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
use core::fmt;
|
||||||
use core::ptr::Unique;
|
use core::ptr::Unique;
|
||||||
use volatile::Volatile;
|
use volatile::Volatile;
|
||||||
|
|
||||||
@@ -87,12 +88,26 @@ impl Writer {
|
|||||||
fn new_line(&mut self) {/* TODO */}
|
fn new_line(&mut self) {/* TODO */}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Write for Writer {
|
||||||
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||||
|
for byte in s.bytes() {
|
||||||
|
self.write_byte(byte)
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn print_something() {
|
pub fn print_something() {
|
||||||
|
use core::fmt::Write;
|
||||||
|
|
||||||
let mut writer = Writer {
|
let mut writer = Writer {
|
||||||
column_position: 0,
|
column_position: 0,
|
||||||
color_code: ColorCode::new(Color::LightGreen, Color::Black),
|
color_code: ColorCode::new(Color::LightGreen, Color::Black),
|
||||||
buffer: unsafe { Unique::new(0xb8000 as *mut _) },
|
buffer: unsafe { Unique::new(0xb8000 as *mut _) },
|
||||||
};
|
};
|
||||||
|
|
||||||
writer.write_str("Hello!");
|
writer.write_byte(b'H');
|
||||||
|
writer.write_str("ello! ");
|
||||||
|
write!(writer, "The numbers are {} and {}", 42, 1.0/3.0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user