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.

114 lines
2.2 KiB

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