From 04f70e5edb8de44a3df2ebb93308fe08f04e3e87 Mon Sep 17 00:00:00 2001 From: ache Date: Thu, 16 Mar 2017 08:15:25 +0100 Subject: New feature : Todo edit Now you can edit multiple message by using the mode "edit" or "e" or "mod". Edit it with the default editor ("vim" or "$EDITOR" or "$VISUAL"). I don't think i should put "ed" as the default editor. Because, seriously, it's ed ... I personaly use vim so i put it instead of emacs. --- todo.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'todo.cpp') diff --git a/todo.cpp b/todo.cpp index b0a8df6..c3fc1f7 100755 --- a/todo.cpp +++ b/todo.cpp @@ -6,6 +6,7 @@ #include #include #include +#include using namespace std; @@ -121,6 +122,9 @@ int main(int argc, char *argv[]) { action = 7; }else if ( !strcmp( argv[1], "unchecked") || !strcmp( argv[1], "todo") ) { action = 8; + }else if ( !strcmp( argv[1], "edit") || !strcmp( argv[1], "mod") + || !strcmp( argv[1], "e") ) { + action = 12; }else {} else action = 0; @@ -158,6 +162,7 @@ int main(int argc, char *argv[]) { for ( int i = listTodo.size()-1; i >= 0 ; --i) if( listTodo.at(i).str == sargv ) { cerr << "Erreur doublon" << endl; + exit(-1); break; } listTodo.push_back(todoFromCmd(sargv)); @@ -255,6 +260,67 @@ int main(int argc, char *argv[]) { it->etat = MISSED_STATE; } break; + case 12: // Edit + 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 ) { + const char* defaultEditor = "vim", + * const envEditor = getenv("EDITOR"), + * const visEditor = getenv("VISUAL"); + if( envEditor && envEditor[0] ) { + defaultEditor = envEditor; + } + if( visEditor && visEditor[0] ) { + defaultEditor = visEditor; + } + FILE* tmp = NULL; + char fileName[] = "todo.cpp_XXXXXX"; + int fd = mkstemp(fileName); + if( strlen( it->str.c_str() ) > 0 ) { + write(fd, it->str.c_str()+1, + strlen(it->str.c_str()) -1 ); + } + close(fd); + + char* cmd = new char[ 2 + strlen(defaultEditor) + + strlen(fileName) ]; + sprintf( cmd, "%s %s", defaultEditor, fileName); + + system(cmd); + + + ifstream file(fileName, ios::in); + + if( file ) { + string line; + bool first = true; + while( getline(file, line) ) { + if( ! first ) { + first = false; + cerr << "Erreur : Edition multi-lignes" << + "Seule la première ligne serra " + " prise en compte" << endl; + break; + } + it->str = string(" ") + line; + } + file.close(); + }else { + cerr << "Impossible d'ouvrir le fichier " + "temporaire" << fileName << endl; + } + + + unlink( fileName ); + delete[] cmd; + } + } + break; + + default: cout << "Erreur : Action incomprise" << endl; } -- cgit v1.2.3