Add a basic writer type to print single bytes

It prints always to the last line and shifts lines up on newline or line wrap. To easily shift lines up, we make the ColorCode and ScreenChar types Copy.
This commit is contained in:
Philipp Oppermann
2015-09-24 14:24:45 +02:00
parent 49fd8926a5
commit ab464b107d
2 changed files with 53 additions and 1 deletions

View File

@@ -13,7 +13,7 @@
// limitations under the License. // limitations under the License.
#![feature(no_std, lang_items)] #![feature(no_std, lang_items)]
#![feature(const_fn)] #![feature(const_fn, unique)]
#![no_std] #![no_std]
extern crate rlibc; extern crate rlibc;

View File

@@ -1,3 +1,5 @@
use core::ptr::Unique;
const BUFFER_HEIGHT: usize = 25; const BUFFER_HEIGHT: usize = 25;
const BUFFER_WIDTH: usize = 80; const BUFFER_WIDTH: usize = 80;
@@ -21,6 +23,55 @@ pub enum Color {
White = 15, White = 15,
} }
pub struct Writer {
column_position: usize,
color_code: ColorCode,
buffer: Unique<Buffer>,
}
impl Writer {
pub fn write_byte(&mut self, byte: u8) {
match byte {
b'\n' => self.new_line(),
byte => {
if self.column_position >= BUFFER_WIDTH {
self.new_line();
}
let row = BUFFER_HEIGHT - 1;
let col = self.column_position;
self.buffer().chars[row][col] = ScreenChar {
ascii_character: byte,
color_code: self.color_code,
};
self.column_position += 1;
}
}
}
fn buffer(&mut self) -> &mut Buffer {
unsafe{self.buffer.get_mut()}
}
fn new_line(&mut self) {
for row in 0..(BUFFER_HEIGHT-1) {
let buffer = self.buffer();
buffer.chars[row] = buffer.chars[row + 1]
}
self.clear_row(BUFFER_HEIGHT-1);
self.column_position = 0;
}
fn clear_row(&mut self, row: usize) {
let blank = ScreenChar {
ascii_character: ' ' as u8,
color_code: self.color_code,
};
self.buffer().chars[row] = [blank; BUFFER_WIDTH];
}
}
#[derive(Clone, Copy)]
struct ColorCode(u8); struct ColorCode(u8);
impl ColorCode { impl ColorCode {
@@ -29,6 +80,7 @@ impl ColorCode {
} }
} }
#[derive(Clone, Copy)]
struct ScreenChar { struct ScreenChar {
ascii_character: u8, ascii_character: u8,
color_code: ColorCode, color_code: ColorCode,