Guest User

run.rs

a guest
Feb 24th, 2023
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. // run.rs
  2.  
  3. use std::io::Write;
  4.  
  5. use termion::event::Key;
  6. use termion::input::TermRead;
  7. use termion::raw::IntoRawMode;
  8. use crate::DisplayableFile;
  9. use crate::Mode;
  10.  
  11. impl DisplayableFile {
  12. pub fn run_off_mode(&mut self) {
  13. self.display();
  14. match self.mode {
  15. Mode::Command => self.command_run(),
  16. Mode::Insert => self.insert_run(),
  17. Mode::Normal => self.normal_run()
  18. }
  19. self.display();
  20. }
  21. pub fn normal_run(&mut self) {
  22. self.display();
  23. let _stdout = std::io::stdout().into_raw_mode().unwrap();
  24. let stdin = std::io::stdin();
  25. for c in stdin.keys() {
  26. match c.unwrap() {
  27. Key::Char('c') => { self.mode = Mode::Command; self.display(); },
  28. Key::Char('i') => { self.mode = Mode::Insert; self.display(); },
  29. Key::Ctrl(ch) => {
  30. if ch == 'c' {
  31. std::process::exit(0);
  32. }
  33. if ch == 's' {
  34. self.saved = true;
  35. }
  36. self.display();
  37. }
  38. _ => {
  39. self.display();
  40. }
  41. }
  42. }
  43. }
  44. pub fn command_run(&mut self) {
  45. self.display();
  46. let _stdout = std::io::stdout().into_raw_mode().unwrap();
  47. let stdin = std::io::stdin();
  48. for c in stdin.keys() {
  49. match c.unwrap() {
  50. Key::Esc => { self.mode = Mode::Normal; self.display(); },
  51. Key::Char(ch) => { self.command.push(ch); self.display(); },
  52. Key::Backspace => { drop(self.command.pop()); self.display(); },
  53. Key::Ctrl(ch) => {
  54. if ch == 'c' {
  55. std::process::exit(0);
  56. }
  57. if ch == 's' {
  58. self.saved = true;
  59. }
  60. self.display();
  61. }
  62. _ => {
  63. self.display();
  64. }
  65. }
  66. }
  67. }
  68. pub fn insert_run(&mut self) {
  69. self.display();
  70. let mut stdout = std::io::stdout().into_raw_mode().unwrap();
  71. let stdin = std::io::stdin();
  72. for c in stdin.keys() {
  73. match c.unwrap() {
  74. Key::Esc => { self.mode = Mode::Normal; self.display(); },
  75. Key::Ctrl(ch) => {
  76. if ch == 'c' {
  77. std::process::exit(0);
  78. }
  79. if ch == 's' {
  80. self.saved = !self.saved;
  81. }
  82.  
  83. self.display();
  84. }
  85. Key::Char(ch) => {
  86. self.saved = false;
  87. // Some guards to prevent blatant errors
  88. if self.contents.get(self.cursor_pos.y) == Option::None
  89. || self.cursor_pos.x > self.contents.get(self.cursor_pos.y).unwrap_or(&String::from(" ")).len() {
  90. } else if ch != '\n' {
  91. // Regular key press
  92. let mut new_str = String::from(self.contents.get(self.cursor_pos.y).unwrap());
  93. new_str.insert(self.cursor_pos.x, ch);
  94. self.contents[self.cursor_pos.y]
  95. .insert(self.cursor_pos.x, ch);
  96. self.cursor_pos.x += 1;
  97. self.display();
  98. } else {
  99. // Enter Key
  100. self.contents.insert(self.cursor_pos.y+1, String::new());
  101. self.cursor_pos.y += 1;
  102. self.cursor_pos.x = 0;
  103. self.display();
  104. }
  105. }
  106. Key::Backspace => {
  107. self.saved = false;
  108. if self.contents.get(self.cursor_pos.y) == Option::None
  109. || self.cursor_pos.x > self.contents.get(self.cursor_pos.y).unwrap_or(&String::from(" ")).len()
  110. || self.cursor_pos.x < 1 {
  111.  
  112. } else {
  113. self.contents[self.cursor_pos.y]
  114. .replace_range(self.cursor_pos.x-1..self.cursor_pos.x, "");
  115. self.cursor_pos.x -= 1;
  116. self.display();
  117. }
  118. }
  119. Key::Up => {
  120. if self.cursor_pos.y > 0 {
  121. self.cursor_pos.y -= 1;
  122. self.cursor_pos.x = self.contents[self.cursor_pos.y].len();
  123. }
  124. self.display();
  125. }
  126. Key::Down => {
  127. if self.cursor_pos.y < self.contents.len()-1 {
  128. self.cursor_pos.y += 1;
  129. self.cursor_pos.x = self.contents[self.cursor_pos.y].len();
  130. self.display();
  131. }
  132. }
  133. Key::Left => {
  134. if self.cursor_pos.x > 0 {
  135. self.cursor_pos.x -= 1;
  136. }
  137. self.display();
  138. }
  139. Key::Right => {
  140. self.cursor_pos.x += 1;
  141. self.display();
  142. }
  143. _ => {
  144. self.display();
  145. }
  146. }
  147. }
  148. self.display();
  149. stdout.flush().unwrap();
  150. }
  151. }
Add Comment
Please, Sign In to add comment