You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.6 KiB

  1. // FileBrowser.cpp
  2. #include "FileBrowser.h"
  3. using namespace std;
  4. void FileBrowser::display()
  5. {
  6. const string long_separator(50, '-');
  7. const string short_separator(8, '-');
  8. //system("cls || clear");
  9. if (!error_message_.empty()) {
  10. cout << "ERROR: " + error_message_ << endl;
  11. error_message_.clear();
  12. }
  13. string file_name = buffer_.file_name();
  14. if (file_name.empty())
  15. cout << "<no file opened>\n";
  16. else
  17. cout << file_name << endl;
  18. cout << long_separator << endl;
  19. auto lines = buffer_.get_lines();
  20. delete[] lines;
  21. buffer_.display();
  22. cout << long_separator << endl;
  23. cout << " next previous open\n";
  24. cout << " go back quit\n";
  25. cout << short_separator << endl;
  26. }
  27. void FileBrowser::execute_command(char command, bool & done)
  28. {
  29. switch (command) {
  30. /*case 'b': {
  31. buffer_.back();
  32. break;
  33. }
  34. case 'g': {
  35. stringstream ss;
  36. string temp;
  37. int link;
  38. cout << "go to link: ";
  39. getline(cin, temp);
  40. ss.str(temp);
  41. ss >> link;
  42. if (!ss) {
  43. error_message_ = "Could not open " + temp;
  44. }
  45. if (link < 1 || link > buffer_.max_links()) {
  46. error_message_ = "Could not open " + temp + " because the file is out of range";
  47. }
  48. buffer_.go(link);
  49. break;
  50. }*/
  51. case 'n': {
  52. buffer_.move_to_next_page();
  53. break;
  54. }
  55. case 'o': {
  56. cout << "file name: ";
  57. string file_name;
  58. getline(cin, file_name);
  59. if (!buffer_.open(file_name))
  60. error_message_ = "Could not open " + file_name;
  61. break;
  62. }
  63. case 'p': {
  64. buffer_.move_to_previous_page();
  65. break;
  66. }
  67. case 'q': {
  68. done = true;
  69. break;
  70. }
  71. }
  72. }
  73. void FileBrowser::run()
  74. {
  75. cout << "Window height? ";
  76. cin >> window_height_;
  77. cin.get(); // '\n'
  78. cout << '\n';
  79. buffer_.set_window_height(window_height_);
  80. cout << "Maximum length? ";
  81. cin >> maximum_length_;
  82. cin.get(); // '\n'
  83. cout << '\n';
  84. bool done = false;
  85. while (!done) {
  86. display();
  87. cout << "command: ";
  88. char command;
  89. cin >> command;
  90. cin.get(); // '\n'
  91. execute_command(command, done);
  92. cout << endl;
  93. }
  94. }