Guest User

main.rs

a guest
Feb 24th, 2023
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. // main.rs
  2.  
  3. // Standard Library Imports
  4. use std::env::args;
  5. use std::fs;
  6.  
  7. // Local Imports
  8. mod display;
  9. mod run;
  10. mod footer;
  11.  
  12. struct Coordinates {
  13. x: usize,
  14. y: usize,
  15. }
  16.  
  17. struct DisplayableFile {
  18. // Coordinates
  19. terminal_size: Coordinates,
  20. cursor_pos: Coordinates,
  21.  
  22. // Debug
  23. error: String,
  24.  
  25. // Document Stat
  26. file_name: String,
  27. footer: String,
  28. contents: Vec<String>,
  29. saved: bool,
  30. command: String,
  31. mode: Mode
  32. }
  33. #[derive(Debug)]
  34.  
  35. enum Mode {
  36. Normal,
  37. Insert,
  38. Command
  39. }
  40.  
  41. impl Mode {
  42. fn as_str(&self) -> &str {
  43. match self {
  44. Mode::Normal => "Normal",
  45. Mode::Insert => "Insert",
  46. Mode::Command => "Command"
  47. }
  48. }
  49. }
  50. fn main() {
  51. let args: Vec<String> = args().collect();
  52. let mut buffer: Vec<String> = vec![];
  53.  
  54. // If file is specified, load the file into the buffer.
  55. // Otherwise, leave buffer empty.
  56. let mut file_name = "Buffer";
  57. if args.len() > 2 {
  58. if !std::path::Path::new(&args[1]).exists() {
  59. println!("File does not exist");
  60. std::process::exit(0);
  61. }
  62. file_name = &args[1];
  63. let file_string = fs::read_to_string(&args[1]);
  64. for line in file_string.unwrap().split('\n') {
  65. buffer.push(String::from(line));
  66. }
  67. } else {
  68. buffer.push(String::from(""));
  69. }
  70.  
  71. let size = termion::terminal_size().unwrap();
  72. let terminal_size = Coordinates {
  73. x: size.0.into(),
  74. y: size.1.into(),
  75. };
  76.  
  77. let cursor_pos = Coordinates { x: 0, y: 0 };
  78.  
  79. let mut file = DisplayableFile {
  80. file_name: String::from(file_name),
  81. terminal_size: terminal_size,
  82. cursor_pos: cursor_pos,
  83. footer: String::from("File Footer"),
  84. contents: buffer,
  85. saved: true,
  86.  
  87. error: String::from(""),
  88. command: String::new(),
  89. mode: Mode::Insert,
  90. };
  91.  
  92. file.display();
  93. loop {
  94. file.run_off_mode();
  95. }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment