Guest User

display.rs

a guest
Feb 24th, 2023
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. // display.rs
  2.  
  3. extern crate termion;
  4.  
  5. use termion::color;
  6.  
  7. use std::io::stdout;
  8. use std::io::Write;
  9.  
  10. use crate::DisplayableFile;
  11.  
  12. impl DisplayableFile {
  13. pub fn display(&mut self) {
  14. write!(stdout(), "{}", termion::clear::All).unwrap();
  15.  
  16. let li = self.cursor_pos.y as i32;
  17. let ts = self.terminal_size.y as i32;
  18. let mut start_line = li - ts;
  19. if start_line < 0 { start_line = 0; }
  20. if start_line > (self.contents.len() as i32) { start_line = self.contents.len() as i32; }
  21.  
  22. // Display contents
  23. let max_length = self.contents.len() + self.terminal_size.y;
  24.  
  25. let mut count_line = 0;
  26.  
  27. for line_index in 0..max_length {
  28. let mut display = false;
  29.  
  30.  
  31. if line_index >= start_line as usize {
  32. display = true;
  33. }
  34. if count_line > ts-2 {
  35. display = false;
  36. }
  37.  
  38. if display {
  39. count_line += 1;
  40. writeln!(
  41. stdout(),
  42. "{}{}{}{:05}{}{}{}{}{}",
  43. termion::cursor::Goto(1, (&count_line + 1).try_into().unwrap()),
  44. color::Fg(color::LightBlue),
  45. color::Bg(color::White),
  46. line_index+1,
  47. color::Fg(color::Reset),
  48. color::Bg(color::Reset),
  49. color::Fg(color::Yellow),
  50. self.contents.get(line_index).unwrap_or(&String::from("")),
  51. color::Fg(color::Reset)
  52. ).unwrap();
  53. if line_index == self.cursor_pos.y {
  54. write!(
  55. stdout(),
  56. "{}{}|{}{}",
  57. termion::cursor::Goto((self.cursor_pos.x + 6) as u16, (count_line+1) as u16),
  58. color::Fg(color::Blue),
  59. termion::cursor::Goto((self.cursor_pos.x + 7) as u16, (count_line+1) as u16),
  60. color::Fg(color::Yellow),
  61. ).unwrap();
  62. }
  63. }
  64. }
  65.  
  66. // Display footer
  67. self.update_footer(String::from(self.mode.as_str()));
  68.  
  69. write!(
  70. stdout(),
  71. "{}",
  72. termion::cursor::Goto(1, (self.terminal_size.y - 1).try_into().unwrap())
  73. )
  74. .unwrap();
  75.  
  76. writeln!(
  77. stdout(),
  78. "{}{}{}{}{}{}",
  79. color::Fg(color::Black),
  80. color::Bg(color::White),
  81. self.footer,
  82. color::Fg(color::Reset),
  83. color::Bg(color::Reset),
  84. termion::cursor::Goto(1, 1)
  85. )
  86. .unwrap();
  87.  
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment