Browse Source

Minor fixes and cleanup of terminal.*.

merge-requests/365/head
eidheim 10 years ago
parent
commit
925a318fc4
  1. 2
      src/cmake.cc
  2. 52
      src/terminal.cc
  3. 15
      src/terminal.h
  4. 21
      src/window.cc
  5. 1
      src/window.h

2
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;
}

52
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<void(bool success)> callback) {
void Terminal::async_execute(const std::string &command, const std::string &path, std::function<void(int exit_code)> 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;c<n;c++)
async_execute_print_string+=buffer[c];
async_execute_print_string_mutex.unlock();
async_execute_print();
message+=buffer[c];
async_print(message);
}
async_pid_mutex.lock();
int exit_code=async_pid_status.at(pid);
@ -202,19 +197,19 @@ void Terminal::async_execute(const std::string &command, const std::string &path
close(input_descriptor);
close(output_descriptor);
if(callback)
callback(exit_code==0);
callback(exit_code);
}
});
async_execute_thread.detach();
}
int Terminal::print(std::string message){
int Terminal::print(const std::string &message){
INFO("Terminal: PrintMessage");
text_view.get_buffer()->insert(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::InProgress> Terminal::print_in_progress(std::string st
std::shared_ptr<Terminal::InProgress> in_progress=std::shared_ptr<Terminal::InProgress>(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();
}

15
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<void(bool success)> 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<void(int exit_code)> callback=nullptr);
std::unordered_map<pid_t, std::pair<int, int> > async_pid_descriptors;
std::unordered_map<pid_t, int> 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<InProgress> 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_

21
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();
});
}

1
src/window.h

@ -27,7 +27,6 @@ private:
Menu menu;
std::atomic<bool> compiling;
Glib::Dispatcher compile_success;
Glib::Dispatcher run_success, run_error;
void create_menu();
void hide();

Loading…
Cancel
Save