main.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <unistd.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <cmath>
  6. #include "appContext.hpp"
  7. #include "history.hpp"
  8. #include "exception.hpp"
  9. using namespace knacki::cd;
  10. void Usage(std::ostream &out, const std::string &progName)
  11. {
  12. // --history
  13. // --histDir
  14. // --histFile
  15. /* "--gethistfile"
  16. "--history"
  17. "--flush" || "-F"
  18. "--verbose" || "-v"
  19. "--help"
  20. */
  21. out << progName << std::endl;
  22. }
  23. AppContext *GetAppContext(char **av)
  24. {
  25. AppContext *app;
  26. try
  27. {
  28. app = new AppContext(av);
  29. }
  30. catch (ArgumentException &e)
  31. {
  32. std::cerr << av[0] << ": Invalid argument " << e.GetWrongArg() << std::endl;
  33. Usage(std::cerr, av[0]);
  34. return nullptr;
  35. }
  36. return app;
  37. }
  38. void PrintHistory(std::ostream &out, const std::string &progName, const History &hist)
  39. {
  40. if (hist.GetEntries().empty())
  41. {
  42. out << progName << ": No entry" << std::endl;
  43. }
  44. else
  45. {
  46. const unsigned int maxSize = (unsigned int) std::log10(hist.GetEntries().size()) +1;
  47. for (HistoryEntry i : hist.GetEntries())
  48. {
  49. if (hist.IsPrevious(i))
  50. out << "-";
  51. else if (hist.IsCurrent(i))
  52. out << ">";
  53. else
  54. out << " ";
  55. out << std::setfill(' ') << std::setw(maxSize) << ((size_t)(i.GetIndex())) << ": " << i.GetPath() << std::endl;
  56. }
  57. }
  58. }
  59. int main(int, char **av)
  60. {
  61. AppContext *app = GetAppContext(av);
  62. bool written = false;
  63. if (!app)
  64. return 0;
  65. if (app->GetArgs().usageOnly)
  66. {
  67. Usage(std::cout, av[0]);
  68. return 0;
  69. }
  70. History *hist;
  71. try
  72. {
  73. if (app->IsReadOnly())
  74. {
  75. hist = new History(app->GetArgs().histAbsolutePath);
  76. }
  77. else
  78. {
  79. hist = new HistoryRW(app->GetArgs().histAbsolutePath);
  80. }
  81. }
  82. catch (IOError &e)
  83. {
  84. std::cerr << av[0] << ": " << app->GetArgs().histAbsolutePath << ": " << e.what << std::endl;
  85. return 0;
  86. }
  87. if (app->GetArgs().historyOnly)
  88. PrintHistory(std::cerr, av[0], *hist);
  89. if (app->GetArgs().getHistoryFile)
  90. std::cout << app->GetArgs().histAbsolutePath;
  91. if (app->GetArgs().flush)
  92. written |= ((HistoryRW*)hist)->Flush();
  93. const HistoryEntry *nextEnt = app->IsReadOnly() ? nullptr : hist->GetNextWorkingDirectory(*app);
  94. if (nextEnt)
  95. {
  96. written |= ((HistoryRW*)hist)->Append(*nextEnt);
  97. if (app->GetArgs().verbose)
  98. std::cerr << nextEnt->GetPath() << std::endl;
  99. std::cout << nextEnt->GetPath() << std::endl;
  100. delete nextEnt;
  101. }
  102. if (written)
  103. ((HistoryRW*)hist)->Write();
  104. delete hist;
  105. delete app;
  106. }