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
|
/// impl blocks are split similarly to the original C implementation
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Editor {
|
pub struct Editor {
|
||||||
screen_cols: u16,
|
screen_cols: usize,
|
||||||
screen_rows: u16,
|
screen_rows: usize,
|
||||||
|
output_buffer: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// init
|
// init
|
||||||
@ -19,8 +20,8 @@ impl Editor {
|
|||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let mut instance = Self::default();
|
let mut instance = Self::default();
|
||||||
let size = instance.get_window_size();
|
let size = instance.get_window_size();
|
||||||
instance.screen_cols = size.cols;
|
instance.screen_cols = size.cols as usize;
|
||||||
instance.screen_rows = size.rows;
|
instance.screen_rows = size.rows as usize;
|
||||||
|
|
||||||
instance
|
instance
|
||||||
}
|
}
|
||||||
@ -107,30 +108,60 @@ impl Editor {
|
|||||||
|
|
||||||
// Output
|
// Output
|
||||||
impl Editor {
|
impl Editor {
|
||||||
fn draw_rows(&mut self) {
|
/// Equivalent of the abAppend function
|
||||||
let stdout = io::stdout();
|
/// in the original tutorial, just appends
|
||||||
let mut handle = stdout.lock();
|
/// to the `output_buffer` String in the
|
||||||
let buffer = String::from("~\r\n").into_bytes();
|
/// editor struct.
|
||||||
|
fn append_out(&mut self, str: &str) {
|
||||||
|
self.output_buffer.push_str(str);
|
||||||
|
}
|
||||||
|
|
||||||
for _ in 0..self.screen_rows as usize {
|
fn draw_rows(&mut self) {
|
||||||
handle.write(&buffer).unwrap();
|
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) {
|
pub fn refresh_screen(&mut self) {
|
||||||
let stdout = io::stdout();
|
self.output_buffer.clear();
|
||||||
let mut handle = stdout.lock();
|
|
||||||
|
|
||||||
// Clear screen
|
// Hide cursor, reposition cursor
|
||||||
let mut buffer = String::from("\x1b[2J").into_bytes();
|
self.append_out("\x1b[?25l");
|
||||||
handle.write_all(&mut buffer).unwrap();
|
self.append_out("\x1b[H");
|
||||||
|
|
||||||
// Reposition cursor
|
|
||||||
let mut buffer = String::from("\x1b[H").into_bytes();
|
|
||||||
handle.write_all(&mut buffer).unwrap();
|
|
||||||
|
|
||||||
self.draw_rows();
|
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