Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // display.rs
- extern crate termion;
- use termion::color;
- use std::io::stdout;
- use std::io::Write;
- use crate::DisplayableFile;
- impl DisplayableFile {
- pub fn display(&mut self) {
- write!(stdout(), "{}", termion::clear::All).unwrap();
- let li = self.cursor_pos.y as i32;
- let ts = self.terminal_size.y as i32;
- let mut start_line = li - ts;
- if start_line < 0 { start_line = 0; }
- if start_line > (self.contents.len() as i32) { start_line = self.contents.len() as i32; }
- // Display contents
- let max_length = self.contents.len() + self.terminal_size.y;
- let mut count_line = 0;
- for line_index in 0..max_length {
- let mut display = false;
- if line_index >= start_line as usize {
- display = true;
- }
- if count_line > ts-2 {
- display = false;
- }
- if display {
- count_line += 1;
- writeln!(
- stdout(),
- "{}{}{}{:05}{}{}{}{}{}",
- termion::cursor::Goto(1, (&count_line + 1).try_into().unwrap()),
- color::Fg(color::LightBlue),
- color::Bg(color::White),
- line_index+1,
- color::Fg(color::Reset),
- color::Bg(color::Reset),
- color::Fg(color::Yellow),
- self.contents.get(line_index).unwrap_or(&String::from("")),
- color::Fg(color::Reset)
- ).unwrap();
- if line_index == self.cursor_pos.y {
- write!(
- stdout(),
- "{}{}|{}{}",
- termion::cursor::Goto((self.cursor_pos.x + 6) as u16, (count_line+1) as u16),
- color::Fg(color::Blue),
- termion::cursor::Goto((self.cursor_pos.x + 7) as u16, (count_line+1) as u16),
- color::Fg(color::Yellow),
- ).unwrap();
- }
- }
- }
- // Display footer
- self.update_footer(String::from(self.mode.as_str()));
- write!(
- stdout(),
- "{}",
- termion::cursor::Goto(1, (self.terminal_size.y - 1).try_into().unwrap())
- )
- .unwrap();
- writeln!(
- stdout(),
- "{}{}{}{}{}{}",
- color::Fg(color::Black),
- color::Bg(color::White),
- self.footer,
- color::Fg(color::Reset),
- color::Bg(color::Reset),
- termion::cursor::Goto(1, 1)
- )
- .unwrap();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment