mirror of https://gitlab.com/cppit/jucipp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
273 lines
8.2 KiB
273 lines
8.2 KiB
#include "terminal.h" |
|
#include <iostream> |
|
#include "logging.h" |
|
#include "singletons.h" |
|
#include "process.h" |
|
|
|
#include <iostream> //TODO: remove |
|
using namespace std; //TODO: remove |
|
|
|
Terminal::InProgress::InProgress(const std::string& start_msg): stop(false) { |
|
waiting_print.connect([this](){ |
|
Singleton::terminal->async_print(line_nr-1, "."); |
|
}); |
|
start(start_msg); |
|
} |
|
|
|
Terminal::InProgress::~InProgress() { |
|
stop=true; |
|
if(wait_thread.joinable()) |
|
wait_thread.join(); |
|
} |
|
|
|
void Terminal::InProgress::start(const std::string& msg) { |
|
line_nr=Singleton::terminal->print(msg+"...\n"); |
|
wait_thread=std::thread([this](){ |
|
size_t c=0; |
|
while(!stop) { |
|
if(c%100==0) |
|
waiting_print(); |
|
std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
|
c++; |
|
} |
|
}); |
|
} |
|
|
|
void Terminal::InProgress::done(const std::string& msg) { |
|
if(!stop) { |
|
stop=true; |
|
Singleton::terminal->async_print(line_nr-1, msg); |
|
} |
|
} |
|
|
|
void Terminal::InProgress::cancel(const std::string& msg) { |
|
if(!stop) { |
|
stop=true; |
|
Singleton::terminal->async_print(line_nr-1, msg); |
|
} |
|
} |
|
|
|
Terminal::Terminal() { |
|
bold_tag=get_buffer()->create_tag(); |
|
bold_tag->property_weight()=PANGO_WEIGHT_BOLD; |
|
|
|
async_print_dispatcher.connect([this](){ |
|
async_print_strings_mutex.lock(); |
|
if(async_print_strings.size()>0) { |
|
for(auto &string_bold: async_print_strings) |
|
print(string_bold.first, string_bold.second); |
|
async_print_strings.clear(); |
|
} |
|
async_print_strings_mutex.unlock(); |
|
}); |
|
async_print_on_line_dispatcher.connect([this](){ |
|
async_print_on_line_strings_mutex.lock(); |
|
if(async_print_on_line_strings.size()>0) { |
|
for(auto &line_string: async_print_on_line_strings) |
|
print(line_string.first, line_string.second); |
|
async_print_on_line_strings.clear(); |
|
} |
|
async_print_on_line_strings_mutex.unlock(); |
|
}); |
|
} |
|
|
|
int Terminal::process(const std::string &command, const boost::filesystem::path &path, bool use_pipes) { |
|
std::unique_ptr<Process> process; |
|
if(use_pipes) |
|
process=std::unique_ptr<Process>(new Process(command, path.string(), [this](const char* bytes, size_t n) { |
|
async_print(std::string(bytes, n)); |
|
}, [this](const char* bytes, size_t n) { |
|
async_print(std::string(bytes, n), true); |
|
})); |
|
else |
|
process=std::unique_ptr<Process>(new Process(command, path.string())); |
|
|
|
if(process->get_id()<=0) { |
|
async_print("Error: Failed to run command: " + command + "\n", true); |
|
return -1; |
|
} |
|
|
|
return process->get_exit_code(); |
|
} |
|
|
|
int Terminal::process(std::istream &stdin_stream, std::ostream &stdout_stream, const std::string &command, const boost::filesystem::path &path) { |
|
Process process(command, path.string(), [this, &stdout_stream](const char* bytes, size_t n) { |
|
Glib::ustring umessage(bytes, n); |
|
Glib::ustring::iterator iter; |
|
while(!umessage.validate(iter)) { |
|
auto next_char_iter=iter; |
|
next_char_iter++; |
|
umessage.replace(iter, next_char_iter, "?"); |
|
} |
|
stdout_stream.write(umessage.data(), n); |
|
}, [this](const char* bytes, size_t n) { |
|
async_print(std::string(bytes, n), true); |
|
}); |
|
|
|
if(process.get_id()<=0) { |
|
async_print("Error: Failed to run command: " + command + "\n", true); |
|
return -1; |
|
} |
|
|
|
char buffer[131072]; |
|
for(;;) { |
|
stdin_stream.readsome(buffer, 131072); |
|
auto read_n=stdin_stream.gcount(); |
|
if(read_n==0) |
|
break; |
|
if(!process.write(buffer, read_n)) |
|
break; |
|
} |
|
|
|
return process.get_exit_code(); |
|
} |
|
|
|
void Terminal::async_process(const std::string &command, const boost::filesystem::path &path, std::function<void(int exit_code)> callback) { |
|
std::thread async_execute_thread([this, command, path, callback](){ |
|
processes_mutex.lock(); |
|
stdin_buffer.clear(); |
|
std::shared_ptr<Process> process(new Process(command, path.string(), [this](const char* bytes, size_t n) { |
|
async_print(std::string(bytes, n)); |
|
}, [this](const char* bytes, size_t n) { |
|
async_print(std::string(bytes, n), true); |
|
}, true)); |
|
auto pid=process->get_id(); |
|
if (pid<=0) { |
|
processes_mutex.unlock(); |
|
async_print("Error: Failed to run command: " + command + "\n", true); |
|
if(callback) |
|
callback(-1); |
|
return; |
|
} |
|
else { |
|
processes.emplace_back(process); |
|
processes_mutex.unlock(); |
|
} |
|
|
|
exit_code_type exit_code=process->get_exit_code(); |
|
|
|
processes_mutex.lock(); |
|
for(auto it=processes.begin();it!=processes.end();it++) { |
|
if((*it)->get_id()==pid) { |
|
processes.erase(it); |
|
break; |
|
} |
|
} |
|
stdin_buffer.clear(); |
|
processes_mutex.unlock(); |
|
|
|
if(callback) |
|
callback(exit_code); |
|
}); |
|
async_execute_thread.detach(); |
|
} |
|
|
|
void Terminal::kill_last_async_process(bool force) { |
|
processes_mutex.lock(); |
|
if(processes.size()>0) |
|
Process::kill(processes.back()->get_id(), force); |
|
processes_mutex.unlock(); |
|
} |
|
|
|
void Terminal::kill_async_processes(bool force) { |
|
processes_mutex.lock(); |
|
for(auto &process: processes) |
|
Process::kill(process->get_id(), force); |
|
processes_mutex.unlock(); |
|
} |
|
|
|
size_t Terminal::print(const std::string &message, bool bold){ |
|
Glib::ustring umessage=message; |
|
Glib::ustring::iterator iter; |
|
while(!umessage.validate(iter)) { |
|
auto next_char_iter=iter; |
|
next_char_iter++; |
|
umessage.replace(iter, next_char_iter, "?"); |
|
} |
|
|
|
if(bold) |
|
get_buffer()->insert_with_tag(get_buffer()->end(), umessage, bold_tag); |
|
else |
|
get_buffer()->insert(get_buffer()->end(), umessage); |
|
|
|
if(get_buffer()->get_line_count()>Singleton::config->terminal.history_size) { |
|
int lines=get_buffer()->get_line_count()-Singleton::config->terminal.history_size; |
|
get_buffer()->erase(get_buffer()->begin(), get_buffer()->get_iter_at_line(lines)); |
|
deleted_lines+=static_cast<size_t>(lines); |
|
} |
|
|
|
return static_cast<size_t>(get_buffer()->end().get_line())+deleted_lines; |
|
} |
|
|
|
void Terminal::print(size_t line_nr, const std::string &message){ |
|
if(line_nr<deleted_lines) |
|
return; |
|
|
|
Glib::ustring umessage=message; |
|
Glib::ustring::iterator iter; |
|
while(!umessage.validate(iter)) { |
|
auto next_char_iter=iter; |
|
next_char_iter++; |
|
umessage.replace(iter, next_char_iter, "?"); |
|
} |
|
|
|
auto end_line_iter=get_buffer()->get_iter_at_line(static_cast<int>(line_nr-deleted_lines)); |
|
while(!end_line_iter.ends_line() && end_line_iter.forward_char()) {} |
|
get_buffer()->insert(end_line_iter, umessage); |
|
} |
|
|
|
std::shared_ptr<Terminal::InProgress> Terminal::print_in_progress(std::string start_msg) { |
|
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, bool bold) { |
|
async_print_strings_mutex.lock(); |
|
bool dispatch=true; |
|
if(async_print_strings.size()>0) |
|
dispatch=false; |
|
async_print_strings.emplace_back(message, bold); |
|
async_print_strings_mutex.unlock(); |
|
if(dispatch) |
|
async_print_dispatcher(); |
|
} |
|
|
|
void Terminal::async_print(int line_nr, const std::string &message) { |
|
async_print_on_line_strings_mutex.lock(); |
|
bool dispatch=true; |
|
if(async_print_on_line_strings.size()>0) |
|
dispatch=false; |
|
async_print_on_line_strings.emplace_back(line_nr, message); |
|
async_print_on_line_strings_mutex.unlock(); |
|
if(dispatch) |
|
async_print_on_line_dispatcher(); |
|
} |
|
|
|
bool Terminal::on_key_press_event(GdkEventKey *event) { |
|
processes_mutex.lock(); |
|
if(processes.size()>0) { |
|
get_buffer()->place_cursor(get_buffer()->end()); |
|
auto unicode=gdk_keyval_to_unicode(event->keyval); |
|
char chr=(char)unicode; |
|
if(unicode>=32 && unicode<=126) { |
|
stdin_buffer+=chr; |
|
get_buffer()->insert_at_cursor(stdin_buffer.substr(stdin_buffer.size()-1)); |
|
} |
|
else if(event->keyval==GDK_KEY_BackSpace) { |
|
if(stdin_buffer.size()>0 && get_buffer()->get_char_count()>0) { |
|
auto iter=get_buffer()->end(); |
|
iter--; |
|
stdin_buffer.pop_back(); |
|
get_buffer()->erase(iter, get_buffer()->end()); |
|
} |
|
} |
|
else if(event->keyval==GDK_KEY_Return) { |
|
stdin_buffer+='\n'; |
|
processes.back()->write(stdin_buffer.c_str(), stdin_buffer.size()); |
|
get_buffer()->insert_at_cursor(stdin_buffer.substr(stdin_buffer.size()-1)); |
|
stdin_buffer.clear(); |
|
} |
|
} |
|
processes_mutex.unlock(); |
|
return true; |
|
}
|
|
|