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.
 
 
 
 

120 lines
3.4 KiB

  1. /*
  2. * This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <https://www.gnu.org/licenses/>.
  12. *
  13. * [email protected] 2019
  14. */
  15. #ifndef PROMPT_H
  16. #define PROMPT_H
  17. #include <QString>
  18. #include <QVector>
  19. #include <QDebug>
  20. #include <iostream>
  21. #include <string>
  22. #include "colors.h"
  23. #include "models.h"
  24. QString prompt(QString title){
  25. std::cout << QString(KHEADER + title + RST).toStdString().c_str() << std::endl;
  26. _PROMPT:
  27. std::cout << ">";
  28. QTextStream s(stdin);
  29. QString out = s.readLine();
  30. std::cout << std::endl;
  31. if(out.length() < 1){
  32. qWarning(FRED("Invalid input"));
  33. goto _PROMPT;
  34. }
  35. return out;
  36. }
  37. int promptInt(QString title,int lowerLimit,int upperLimit){
  38. std::cout << QString(KHEADER + title + RST).toStdString().c_str() << std::endl;
  39. _PROMPT_INT:
  40. std::cout << ">";
  41. QTextStream s(stdin);
  42. QString out = s.readLine();
  43. std::cout << std::endl;
  44. if(out.length() < 1){
  45. qWarning(FRED("Invalid input"));
  46. goto _PROMPT_INT;
  47. }
  48. int num = out.toInt();
  49. if(num < lowerLimit || num > upperLimit){
  50. qWarning(FRED("Input out of range"));
  51. goto _PROMPT_INT;
  52. }
  53. return num;
  54. }
  55. query_t promptQuery(QVector<query_t> q){
  56. std::cout << QString("%0Select a series [0-%1]%3").arg(KHEADER).arg(QString::number(q.length()-1)).arg(RST).toStdString().c_str() << std::endl;
  57. int i = 0;
  58. for(auto query:q){
  59. std::cout << QString("%0[%1]%2 %3").arg(KBOLD).arg(QString::number(i)).arg(RST).arg(query.name).toStdString().c_str() << std::endl;
  60. i++;
  61. }
  62. _PROMPT_QUERY:
  63. std::cout << ">";
  64. QTextStream s(stdin);
  65. QString out = s.readLine();
  66. std::cout << std::endl;
  67. if(out.length() < 1){
  68. qWarning(FRED("Invalid input"));
  69. goto _PROMPT_QUERY;
  70. }
  71. int num = out.toInt();
  72. if(num < 0 || num > (q.length() - 1)){
  73. qWarning(FRED("Input out of range"));
  74. goto _PROMPT_QUERY;
  75. }
  76. return q.at(num);
  77. }
  78. collection_t promptCollection(QVector<collection_t> q){
  79. std::cout << QString("%0Select a collection [0-%1] or 'A' to select all%3").arg(KHEADER).arg(q.length()-1).arg(RST).toStdString().c_str() << std::endl;
  80. int i = 0;
  81. for(auto col:q){
  82. std::cout << QString("%0[%1]%2 %3 (Season %4)").arg(KBOLD).arg(i).arg(RST).arg(col.name).arg(col.season).toStdString().c_str() << std::endl;
  83. i++;
  84. }
  85. _PROMPT_COLLECTION:
  86. std::cout << ">";
  87. QTextStream s(stdin);
  88. QString out = s.readLine();
  89. std::cout << std::endl;
  90. if(out.length() < 1){
  91. qWarning(FRED("Invalid input"));
  92. goto _PROMPT_COLLECTION;
  93. }
  94. if(out.toLower()=="a")
  95. return collection_t();
  96. int num = out.toInt();
  97. if(num < 0 || num > (q.length() - 1)){
  98. qWarning(FRED("Input out of range"));
  99. goto _PROMPT_COLLECTION;
  100. }
  101. return q.at(num);
  102. }
  103. #endif // PROMPT_H