aboutsummaryrefslogtreecommitdiff
path: root/todo.cpp
blob: 069b04b9bc6d3245bfc98badc712ff68922a7a4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include<iostream>
#include<vector>
#include<vector>
#include<string>
#include<fstream>
#include<string.h>
#include <sstream>

using namespace std;

struct todo {
  int priorite;
  string str;
  bool etat;
};

bool color;


todo todoFromCmd(string line) {
 todo tmp;
 tmp.etat = 0;
 istringstream streamLine(line);
 streamLine >> tmp.priorite;
 if( streamLine.good() == false) {
   tmp.priorite = 4;
   streamLine.clear();
   getline(streamLine, tmp.str);
   tmp.str = string(" ") + tmp.str;
 } else 
   getline(streamLine, tmp.str);
 return tmp;
}
todo todoFromLine(string line) {
 todo tmp;
 istringstream streamLine(line);
 streamLine >> tmp.priorite;
 streamLine >> tmp.etat;
 getline(streamLine, tmp.str);
 
 return tmp;
}
ostream& operator<<( ostream& os, const todo& todoTask)
{
  os << "[" << (todoTask.etat ? (color ? "\e[32m✓\e[0m" : "x") : " ") << "] ";
  if( todoTask.priorite == 1  && color)
    os << "\e[31m";
  else if( todoTask.priorite == 2 && color)
    os << "\e[36m";
  else if( todoTask.priorite == 3 && color)
    os << "\e[32m";

  os << todoTask.str;
  
  color && os << "\e[0m";
  os << endl;
  return os;
}

int main(int argc, char *argv[]) {
  color = true;
  vector<todo> listTodo;
  int action = -1;
  if( argc > 1 ) 
    if( !strcmp( argv[1], "show") ) {
      action = 0;
    }else if ( !strcmp( argv[1], "add") ) {
      action = 1;
    }else if ( !strcmp( argv[1], "remove") ) {
      action = 2;
    }else if ( !strcmp( argv[1], "check") ) {
      action = 3;
    }else if ( !strcmp( argv[1], "set") ) {
      if( argc > 2 && argv[2][0] >= '0' && argv[2][0] <= '9' )
        action = 4;
      else
        cout << "Erreur : Priorité non interprétable" << endl;
    }else if ( !strcmp( argv[1], "s") ) {
      if( argc > 2 && argv[2][0] >= '0' && argv[2][0] <= '9' )
        action = 5,color = false;
      else
        cout << "Erreur : Priorité non interprétable" << endl;
    }else  {}
  else
    action = 0;
  if( action == -1 ) {
    cout << "Usage : todo [show,add,remove,check,set]" << endl;
  } else {
    ifstream file(".todo.txt", ios::in);

    if( file ) {
      string line;
      while( getline(file, line) ) {
        listTodo.push_back(todoFromLine(line));
      }
      file.close();
    } else if(action != 1 ){
      return 1;
    }
    switch( action ) {
      case 0:
        for (auto it = begin(listTodo); it != end(listTodo); ++it)
          cout << *it;

        return 0;
      case 1:
      {
        string sargv("");
        for( int i = 2 ; i < argc ; i++) {
          sargv += " ";
          sargv += argv[i];
        }
        if( sargv == "" ) {
          cout << "Chaine vide" << endl;
          break;
        } else
        listTodo.push_back(todoFromCmd(sargv));
        
        break;
      }
      case 2: 
      {
        for ( int i =  listTodo.size()-1; i >= 0 ; --i) {
          bool active = true;
          for( int j = 2 ; j < argc && active ; j++ )
            if( listTodo.at(i).str.find(argv[j]) == string::npos)
              active = false;
          if( active )
            listTodo.erase(begin(listTodo)+i);
        }
        break;
      }
      case 3:
        for (auto it = begin(listTodo); it != end(listTodo); ++it) {
          bool active = true;
          for( int i = 2 ; i < argc && active ; i++ )
            if( it->str.find(argv[i]) == string::npos)
              active = false;
          if( active )
            it->etat = 1;
        }
                                                                                     
        break;
      case 4:
        for (auto it = begin(listTodo); it != end(listTodo); ++it) {
          bool active = true;
          for( int i = 3 ; i < argc && active ; i++ )
            if( it->str.find(argv[i]) == string::npos)
              active = false;
          if( active ) {
            it->priorite = atoi(argv[2]);
          }
        }
      break;
      case 5:
        for (auto it = begin(listTodo); it != end(listTodo); ++it)
          if( it->priorite == atoi(argv[2]) )
            cout << *it;
        return 0;

      default:
        cout << "Erreur : Action incomprise" << endl;
    }
      ofstream fileO(".todo.txt", ios::out);
      if( fileO ) {
        for (auto it = begin(listTodo); it != end(listTodo); ++it)
          fileO << it->priorite << " " << it->etat << it->str << endl;
        fileO.close();
      } else {
        cout << "Erreur : Aucune modification effectuée" << endl;
      }
    
  }
  
  return 0;
}