Browse Source

Cleanup of terminal.*. A little work left here.

merge-requests/365/head
eidheim 11 years ago
parent
commit
cee2448676
  1. 6
      juci/singletons.cc
  2. 4
      juci/singletons.h
  3. 82
      juci/terminal.cc
  4. 55
      juci/terminal.h
  5. 26
      juci/window.cc

6
juci/singletons.cc

@ -4,11 +4,11 @@ std::unique_ptr<Source::Config> Singleton::Config::source_=std::unique_ptr<Sourc
std::unique_ptr<Terminal::Config> Singleton::Config::terminal_=std::unique_ptr<Terminal::Config>(new Terminal::Config()); std::unique_ptr<Terminal::Config> Singleton::Config::terminal_=std::unique_ptr<Terminal::Config>(new Terminal::Config());
std::unique_ptr<Directories::Config> Singleton::Config::directories_=std::unique_ptr<Directories::Config>(new Directories::Config()); std::unique_ptr<Directories::Config> Singleton::Config::directories_=std::unique_ptr<Directories::Config>(new Directories::Config());
std::unique_ptr<Terminal::Controller> Singleton::terminal_=std::unique_ptr<Terminal::Controller>(); std::unique_ptr<Terminal> Singleton::terminal_=std::unique_ptr<Terminal>();
std::unique_ptr<Menu> Singleton::menu_=std::unique_ptr<Menu>(); std::unique_ptr<Menu> Singleton::menu_=std::unique_ptr<Menu>();
Terminal::Controller *Singleton::terminal() { Terminal *Singleton::terminal() {
if(!terminal_) if(!terminal_)
terminal_=std::unique_ptr<Terminal::Controller>(new Terminal::Controller()); terminal_=std::unique_ptr<Terminal>(new Terminal());
return terminal_.get(); return terminal_.get();
} }
Menu *Singleton::menu() { Menu *Singleton::menu() {

4
juci/singletons.h

@ -20,10 +20,10 @@ public:
static std::unique_ptr<Directories::Config> directories_; static std::unique_ptr<Directories::Config> directories_;
}; };
static Terminal::Controller *terminal(); static Terminal *terminal();
static Menu *menu(); static Menu *menu();
private: private:
static std::unique_ptr<Terminal::Controller> terminal_; static std::unique_ptr<Terminal> terminal_;
static std::unique_ptr<Menu> menu_; static std::unique_ptr<Menu> menu_;
}; };

82
juci/terminal.cc

@ -43,94 +43,80 @@ void Terminal::InProgress::cancel(const std::string& msg) {
} }
} }
Terminal::View::View(){ Terminal::Terminal() {
text_view.set_editable(false); text_view.set_editable(false);
scrolled_window.add(text_view); scrolled_window.add(text_view);
add(scrolled_window); add(scrolled_window);
}
change_folder_command = "";
Terminal::Controller::Controller() { text_view.signal_size_allocate().connect([this](Gtk::Allocation& allocation){
folder_command_ = ""; auto end=text_view.get_buffer()->create_mark(text_view.get_buffer()->end());
view.text_view.signal_size_allocate().connect([this](Gtk::Allocation& allocation){ text_view.scroll_to(end);
auto end=view.text_view.get_buffer()->create_mark(view.text_view.get_buffer()->end()); text_view.get_buffer()->delete_mark(end);
view.text_view.scroll_to(end);
view.text_view.get_buffer()->delete_mark(end);
}); });
} }
void Terminal::Controller::SetFolderCommand( boost::filesystem::path void Terminal::set_change_folder_command(boost::filesystem::path CMake_path) {
CMake_path) { INFO("Terminal: set_change_folder_command");
INFO("Terminal: SetFolderCommand"); path = CMake_path.string();
path_ = CMake_path.string(); change_folder_command = "cd "+ path + "; ";
folder_command_ = "cd "+ path_ + "; ";
} }
void Terminal::Controller::Compile(){ void Terminal::compile() {
INFO("Terminal: Compile"); INFO("Terminal: compile");
text_view.get_buffer()->set_text("");
view.text_view.get_buffer()->set_text(""); DEBUG("Terminal: compile: running cmake command");
DEBUG("Terminal: Compile: running cmake command");
std::vector<std::string> commands = Singleton::Config::terminal()->compile_commands; std::vector<std::string> commands = Singleton::Config::terminal()->compile_commands;
for (size_t it = 0; it < commands.size(); ++it) { for (size_t it = 0; it < commands.size(); ++it) {
ExecuteCommand(commands.at(it), "r"); execute_command(commands.at(it), "r");
} }
print("\n"); print("\n");
DEBUG("Terminal: Compile: compile done"); DEBUG("Terminal: compile: compile done");
} }
void Terminal::Controller::Run(std::string executable) { void Terminal::run(std::string executable) {
INFO("Terminal: Run"); INFO("Terminal: run");
print("juCi++ execute: " + executable + "\n"); print("juCi++ execute: " + executable + "\n");
DEBUG("Terminal: Compile: running run command: "); DEBUG("Terminal: compile: running run command: ");
DEBUG_VAR(executable); DEBUG_VAR(executable);
ExecuteCommand("cd "+Singleton::Config::terminal()->run_command + "; ./"+executable, "r"); execute_command("cd "+Singleton::Config::terminal()->run_command + "; ./"+executable, "r");
print("\n"); print("\n");
} }
int Terminal::Controller::print(std::string message){ int Terminal::print(std::string message){
INFO("Terminal: PrintMessage"); INFO("Terminal: PrintMessage");
view.text_view.get_buffer()->insert(view.text_view.get_buffer()->end(), "> "+message); text_view.get_buffer()->insert(text_view.get_buffer()->end(), "> "+message);
return view.text_view.get_buffer()->end().get_line(); return text_view.get_buffer()->end().get_line();
} }
void Terminal::Controller::print(int line_nr, std::string message){ void Terminal::print(int line_nr, std::string message){
INFO("Terminal: PrintMessage at line " << line_nr); INFO("Terminal: PrintMessage at line " << line_nr);
auto iter=view.text_view.get_buffer()->get_iter_at_line(line_nr); auto iter=text_view.get_buffer()->get_iter_at_line(line_nr);
while(!iter.ends_line()) while(!iter.ends_line())
iter++; iter++;
view.text_view.get_buffer()->insert(iter, message); text_view.get_buffer()->insert(iter, message);
} }
std::shared_ptr<Terminal::InProgress> Terminal::Controller::print_in_progress(std::string start_msg) { 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)); std::shared_ptr<Terminal::InProgress> in_progress=std::shared_ptr<Terminal::InProgress>(new Terminal::InProgress(start_msg));
return in_progress; return in_progress;
} }
bool Terminal::Controller::ExistInConsole(std::string string) { void Terminal::execute_command(std::string command, std::string mode) {
INFO("Terminal: ExistInConsole"); INFO("Terminal: execute_command");
DEBUG("Terminal: PrintMessage: finding string in buffer"); command = change_folder_command+command;
double pos = view.text_view.get_buffer()-> DEBUG("Terminal: PrintMessage: running command");
get_text().find(string);
if (pos == std::string::npos) return false;
return true;
}
void Terminal::Controller::ExecuteCommand(std::string command, std::string mode) {
INFO("Terminal: ExecuteCommand");
command = folder_command_+command;
DEBUG("Terminal: PrintMessage: Running command");
FILE* p = NULL; FILE* p = NULL;
std::cout << command << std::endl; std::cout << command << std::endl;
p = popen(command.c_str(), mode.c_str()); p = popen(command.c_str(), mode.c_str());
if (p == NULL) { if (p == NULL) {
print("juCi++ ERROR: Failed to run command" + command + "\n"); print("juCi++ ERROR: Failed to run command" + command + "\n");
}else { }
else {
char buffer[1028]; char buffer[1028];
while (fgets(buffer, 1028, p) != NULL) { while (fgets(buffer, 1028, p) != NULL) {
print(buffer); print(buffer);
} }
pclose(p); pclose(p);
} }
} }

55
juci/terminal.h

@ -8,23 +8,14 @@
#include <thread> #include <thread>
#include <atomic> #include <atomic>
namespace Terminal { class Terminal : public Gtk::HBox {
public:
class Config { class Config {
public: public:
std::vector<std::string> compile_commands; std::vector<std::string> compile_commands;
std::string run_command; std::string run_command;
}; };
class View : public Gtk::HBox {
public:
View();
Gtk::TextView text_view;
Gtk::ScrolledWindow scrolled_window;
}; // class view
class Controller;
//Temporary solution for displaying functions in progress, and when they are done.
class InProgress { class InProgress {
public: public:
InProgress(const std::string& start_msg); InProgress(const std::string& start_msg);
@ -39,26 +30,24 @@ namespace Terminal {
std::thread wait_thread; std::thread wait_thread;
}; };
class Controller { Terminal();
public: void set_change_folder_command(boost::filesystem::path CMake_path);
Controller(); void run(std::string executable);
void SetFolderCommand(boost::filesystem::path CMake_path); void compile();
void Run(std::string executable); int print(std::string message);
void Compile(); void print(int line_nr, std::string message);
int print(std::string message); std::shared_ptr<InProgress> print_in_progress(std::string start_msg);
void print(int line_nr, std::string message); private:
std::shared_ptr<InProgress> print_in_progress(std::string start_msg); void execute_command(std::string command, std::string mode);
Terminal::View view;
private: Gtk::TextView text_view;
void ExecuteCommand(std::string command, std::string mode); Gtk::ScrolledWindow scrolled_window;
bool OnButtonRealeaseEvent(GdkEventKey* key);
bool ExistInConsole(std::string string); std::string change_folder_command;
std::string folder_command_; std::string path;
std::string path_; const std::string cmake_sucsess = "Build files have been written to:";
const std::string cmake_sucsess = "Build files have been written to:"; const std::string make_built = "Built target";
const std::string make_built = "Built target"; const std::string make_executable = "Linking CXX executable";
const std::string make_executable = "Linking CXX executable"; };
}; // class controller
} // namespace Terminal
#endif // JUCI_TERMINAL_H_ #endif // JUCI_TERMINAL_H_

26
juci/window.cc

@ -21,13 +21,13 @@ Window::Window() : box(Gtk::ORIENTATION_VERTICAL) {
box.pack_start(entry_box, Gtk::PACK_SHRINK); box.pack_start(entry_box, Gtk::PACK_SHRINK);
directory_and_notebook_panes.pack1(directories, true, true); //TODO: should be pack1(directories, ...) Clean up directories.* directory_and_notebook_panes.pack1(directories, true, true);
directory_and_notebook_panes.pack2(notebook); directory_and_notebook_panes.pack2(notebook);
directory_and_notebook_panes.set_position(120); directory_and_notebook_panes.set_position(120);
vpaned.set_position(300); vpaned.set_position(300);
vpaned.pack1(directory_and_notebook_panes, true, false); vpaned.pack1(directory_and_notebook_panes, true, false);
vpaned.pack2(Singleton::terminal()->view, true, true); vpaned.pack2(*Singleton::terminal(), true, true);
box.pack_end(vpaned); box.pack_end(vpaned);
show_all_children(); show_all_children();
@ -169,15 +169,15 @@ void Window::add_menu() {
notebook.save_current(); notebook.save_current();
if (running.try_lock()) { if (running.try_lock()) {
std::thread execute([this]() { std::thread execute([this]() {
std::string path = notebook.get_current_view()->file_path; std::string path = notebook.get_current_view()->file_path;
size_t pos = path.find_last_of("/\\"); size_t pos = path.find_last_of("/\\");
if(pos != std::string::npos) { if(pos != std::string::npos) {
path.erase(path.begin()+pos,path.end()); path.erase(path.begin()+pos,path.end());
Singleton::terminal()->SetFolderCommand(path); Singleton::terminal()->set_change_folder_command(path);
} }
Singleton::terminal()->Compile(); Singleton::terminal()->compile();
std::string executable = directories.get_cmakelists_variable(path,"add_executable"); std::string executable = directories.get_cmakelists_variable(path,"add_executable");
Singleton::terminal()->Run(executable); Singleton::terminal()->run(executable);
running.unlock(); running.unlock();
}); });
execute.detach(); execute.detach();
@ -193,9 +193,9 @@ void Window::add_menu() {
size_t pos = path.find_last_of("/\\"); size_t pos = path.find_last_of("/\\");
if(pos != std::string::npos){ if(pos != std::string::npos){
path.erase(path.begin()+pos,path.end()); path.erase(path.begin()+pos,path.end());
Singleton::terminal()->SetFolderCommand(path); Singleton::terminal()->set_change_folder_command(path);
} }
Singleton::terminal()->Compile(); Singleton::terminal()->compile();
running.unlock(); running.unlock();
}); });
execute.detach(); execute.detach();

Loading…
Cancel
Save