diff --git a/src/cmake.cc b/src/cmake.cc index 06d9e07..0d9fefc 100644 --- a/src/cmake.cc +++ b/src/cmake.cc @@ -47,7 +47,7 @@ CMake::CMake(const boost::filesystem::path &path) { bool CMake::create_compile_commands(const std::string &path) { Singleton::terminal()->print("Creating "+boost::filesystem::path(path+"/compile_commands.json").string()+"\n"); //TODO: Windows... - if(Singleton::terminal()->execute("cmake . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON 2>&1", path)) + if(Singleton::terminal()->execute("cmake . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON 2>&1", path)==EXIT_SUCCESS) return true; return false; } diff --git a/src/terminal.cc b/src/terminal.cc index d3562a3..d558706 100644 --- a/src/terminal.cc +++ b/src/terminal.cc @@ -115,13 +115,13 @@ Terminal::Terminal() { text_view.get_buffer()->delete_mark(end); }); - async_execute_print.connect([this](){ - async_execute_print_string_mutex.lock(); - if(async_execute_print_string.size()>0) { - print(async_execute_print_string); - async_execute_print_string=""; + async_print_dispatcher.connect([this](){ + async_print_string_mutex.lock(); + if(async_print_string.size()>0) { + print(async_print_string); + async_print_string=""; } - async_execute_print_string_mutex.unlock(); + async_print_string_mutex.unlock(); }); //Coppied from http://www.linuxprogrammingblog.com/code-examples/sigaction @@ -132,7 +132,7 @@ Terminal::Terminal() { sigaction(SIGCHLD, &act, NULL); } -bool Terminal::execute(const std::string &command, const std::string &path) { +int Terminal::execute(const std::string &command, const std::string &path) { boost::filesystem::path boost_path; std::string cd_path_and_command; if(path!="") { @@ -148,7 +148,7 @@ bool Terminal::execute(const std::string &command, const std::string &path) { p = popen(cd_path_and_command.c_str(), "r"); if (p == NULL) { print("Error: Failed to run command" + command + "\n"); - return false; + return -1; } else { char buffer[1024]; @@ -157,11 +157,11 @@ bool Terminal::execute(const std::string &command, const std::string &path) { while(gtk_events_pending()) gtk_main_iteration(); } - return pclose(p)==0; + return pclose(p); } } -void Terminal::async_execute(const std::string &command, const std::string &path, std::function callback) { +void Terminal::async_execute(const std::string &command, const std::string &path, std::function callback) { std::thread async_execute_thread([this, command, path, callback](){ boost::filesystem::path boost_path; std::string cd_path_and_command; @@ -179,21 +179,16 @@ void Terminal::async_execute(const std::string &command, const std::string &path auto pid=popen2(cd_path_and_command.c_str(), &input_descriptor, &output_descriptor); async_pid_descriptors[pid]={input_descriptor, output_descriptor}; async_pid_mutex.unlock(); - if (pid<=0) { - async_execute_print_string_mutex.lock(); - async_execute_print_string+="Error: Failed to run command" + command + "\n"; - async_execute_print_string_mutex.unlock(); - async_execute_print(); - } + if (pid<=0) + async_print("Error: Failed to run command" + command + "\n"); else { char buffer[1024]; ssize_t n; while ((n=read(output_descriptor, buffer, 1024)) > 0) { - async_execute_print_string_mutex.lock(); + std::string message; for(int c=0;cinsert(text_view.get_buffer()->end(), message); return text_view.get_buffer()->end().get_line(); } -void Terminal::print(int line_nr, std::string message){ +void Terminal::print(int line_nr, const std::string &message){ INFO("Terminal: PrintMessage at line " << line_nr); auto iter=text_view.get_buffer()->get_iter_at_line(line_nr); while(!iter.ends_line()) @@ -226,3 +221,14 @@ std::shared_ptr Terminal::print_in_progress(std::string st std::shared_ptr in_progress=std::shared_ptr(new Terminal::InProgress(start_msg)); return in_progress; } + +void Terminal::async_print(const std::string &message) { + async_print_string_mutex.lock(); + bool dispatch=true; + if(async_print_string.size()>0) + dispatch=false; + async_print_string+=message; + async_print_string_mutex.unlock(); + if(dispatch) + async_print_dispatcher(); +} diff --git a/src/terminal.h b/src/terminal.h index 493ffbd..c7d477f 100644 --- a/src/terminal.h +++ b/src/terminal.h @@ -26,22 +26,23 @@ public: }; Terminal(); - bool execute(const std::string &command, const std::string &path=""); - void async_execute(const std::string &command, const std::string &path="", std::function callback=nullptr); + int execute(const std::string &command, const std::string &path=""); + void async_execute(const std::string &command, const std::string &path="", std::function callback=nullptr); std::unordered_map > async_pid_descriptors; std::unordered_map async_pid_status; std::mutex async_pid_mutex; - int print(std::string message); - void print(int line_nr, std::string message); + int print(const std::string &message); + void print(int line_nr, const std::string &message); std::shared_ptr print_in_progress(std::string start_msg); + void async_print(const std::string &message); private: Gtk::TextView text_view; Gtk::ScrolledWindow scrolled_window; - Glib::Dispatcher async_execute_print; - std::string async_execute_print_string; - std::mutex async_execute_print_string_mutex; + Glib::Dispatcher async_print_dispatcher; + std::string async_print_string; + std::mutex async_print_string_mutex; }; #endif // JUCI_TERMINAL_H_ diff --git a/src/window.cc b/src/window.cc index 9f8ecd2..e297239 100644 --- a/src/window.cc +++ b/src/window.cc @@ -92,12 +92,6 @@ Window::Window() : box(Gtk::ORIENTATION_VERTICAL), notebook(directories), compil compile_success.connect([this](){ directories.open_folder(); }); - run_success.connect([this](){ - Singleton::terminal()->print("Execution returned: success\n"); - }); - run_error.connect([this](){ - Singleton::terminal()->print("Execution returned: error\n"); - }); INFO("Window created"); } // Window constructor @@ -222,16 +216,13 @@ void Window::create_menu() { if(path!="") { Singleton::terminal()->print("Compiling and executing "+path.string()+"\n"); //TODO: Windows... - Singleton::terminal()->async_execute("make 2>&1", cmake.project_path.string(), [this, path](bool success){ + Singleton::terminal()->async_execute("make 2>&1", cmake.project_path.string(), [this, path](int exit_code){ compiling=false; - if(success) { + if(exit_code==EXIT_SUCCESS) { compile_success(); //TODO: Windows... - Singleton::terminal()->async_execute(path.string()+" 2>&1", path.parent_path().string(), [this](bool success){ - if(success) - run_success(); - else - run_error(); + Singleton::terminal()->async_execute(path.string()+" 2>&1", path.parent_path().string(), [this, path](int exit_code){ + Singleton::terminal()->async_print(path.string()+" returned: "+std::to_string(exit_code)+'\n'); }); } }); @@ -249,9 +240,9 @@ void Window::create_menu() { compiling=true; Singleton::terminal()->print("Compiling project "+cmake.project_path.string()+"\n"); //TODO: Windows... - Singleton::terminal()->async_execute("make 2>&1", cmake.project_path.string(), [this](bool success){ + Singleton::terminal()->async_execute("make 2>&1", cmake.project_path.string(), [this](int exit_code){ compiling=false; - if(success) + if(exit_code==EXIT_SUCCESS) compile_success(); }); } diff --git a/src/window.h b/src/window.h index ea53f6b..f14f731 100644 --- a/src/window.h +++ b/src/window.h @@ -27,7 +27,6 @@ private: Menu menu; std::atomic compiling; Glib::Dispatcher compile_success; - Glib::Dispatcher run_success, run_error; void create_menu(); void hide();