Show welcome message
This commit is contained in:
parent
04369ca796
commit
208b92b713
@ -10,8 +10,9 @@ use std::io::BufReader;
|
||||
/// impl blocks are split similarly to the original C implementation
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Editor {
|
||||
screen_cols: u16,
|
||||
screen_rows: u16,
|
||||
screen_cols: usize,
|
||||
screen_rows: usize,
|
||||
output_buffer: String,
|
||||
}
|
||||
|
||||
// init
|
||||
@ -19,8 +20,8 @@ impl Editor {
|
||||
pub fn new() -> Self {
|
||||
let mut instance = Self::default();
|
||||
let size = instance.get_window_size();
|
||||
instance.screen_cols = size.cols;
|
||||
instance.screen_rows = size.rows;
|
||||
instance.screen_cols = size.cols as usize;
|
||||
instance.screen_rows = size.rows as usize;
|
||||
|
||||
instance
|
||||
}
|
||||
@ -107,30 +108,60 @@ impl Editor {
|
||||
|
||||
// Output
|
||||
impl Editor {
|
||||
fn draw_rows(&mut self) {
|
||||
let stdout = io::stdout();
|
||||
let mut handle = stdout.lock();
|
||||
let buffer = String::from("~\r\n").into_bytes();
|
||||
/// Equivalent of the abAppend function
|
||||
/// in the original tutorial, just appends
|
||||
/// to the `output_buffer` String in the
|
||||
/// editor struct.
|
||||
fn append_out(&mut self, str: &str) {
|
||||
self.output_buffer.push_str(str);
|
||||
}
|
||||
|
||||
for _ in 0..self.screen_rows as usize {
|
||||
handle.write(&buffer).unwrap();
|
||||
fn draw_rows(&mut self) {
|
||||
for y in 0..self.screen_rows {
|
||||
if y == (self.screen_rows / 3) {
|
||||
let mut welcome = format!("Kilo editor -- version {}", env!("CARGO_PKG_VERSION"));
|
||||
if welcome.len() > self.screen_cols {
|
||||
welcome.truncate(self.screen_cols)
|
||||
}
|
||||
|
||||
// Center welcome message
|
||||
let mut padding = (self.screen_cols - welcome.len()) / 2;
|
||||
if padding > 0 {
|
||||
self.append_out("~");
|
||||
padding -= 1;
|
||||
}
|
||||
while padding > 0 {
|
||||
self.append_out(" ");
|
||||
padding -=1;
|
||||
}
|
||||
|
||||
self.append_out(&welcome);
|
||||
} else {
|
||||
self.append_out("~");
|
||||
}
|
||||
|
||||
|
||||
self.append_out("\x1b[K");
|
||||
if y < (self.screen_rows as usize - 1) {
|
||||
self.append_out("\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn refresh_screen(&mut self) {
|
||||
let stdout = io::stdout();
|
||||
let mut handle = stdout.lock();
|
||||
self.output_buffer.clear();
|
||||
|
||||
// Clear screen
|
||||
let mut buffer = String::from("\x1b[2J").into_bytes();
|
||||
handle.write_all(&mut buffer).unwrap();
|
||||
|
||||
// Reposition cursor
|
||||
let mut buffer = String::from("\x1b[H").into_bytes();
|
||||
handle.write_all(&mut buffer).unwrap();
|
||||
// Hide cursor, reposition cursor
|
||||
self.append_out("\x1b[?25l");
|
||||
self.append_out("\x1b[H");
|
||||
|
||||
self.draw_rows();
|
||||
|
||||
handle.write_all(&mut buffer).unwrap();
|
||||
self.append_out("\x1b[H");
|
||||
self.append_out("\x1b[?25h");
|
||||
|
||||
let stdout = io::stdout();
|
||||
let mut handle = stdout.lock();
|
||||
handle.write_all(&self.output_buffer.as_bytes()).unwrap();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user