diff --git a/juci/singletons.cc b/juci/singletons.cc index b9c19bd..c43ba63 100644 --- a/juci/singletons.cc +++ b/juci/singletons.cc @@ -4,11 +4,11 @@ std::unique_ptr Singleton::Config::source_=std::unique_ptr Singleton::Config::terminal_=std::unique_ptr(new Terminal::Config()); std::unique_ptr Singleton::Config::directories_=std::unique_ptr(new Directories::Config()); -std::unique_ptr Singleton::terminal_=std::unique_ptr(); +std::unique_ptr Singleton::terminal_=std::unique_ptr(); std::unique_ptr Singleton::menu_=std::unique_ptr(); -Terminal::Controller *Singleton::terminal() { +Terminal *Singleton::terminal() { if(!terminal_) - terminal_=std::unique_ptr(new Terminal::Controller()); + terminal_=std::unique_ptr(new Terminal()); return terminal_.get(); } Menu *Singleton::menu() { diff --git a/juci/singletons.h b/juci/singletons.h index be8240a..126965c 100644 --- a/juci/singletons.h +++ b/juci/singletons.h @@ -20,10 +20,10 @@ public: static std::unique_ptr directories_; }; - static Terminal::Controller *terminal(); + static Terminal *terminal(); static Menu *menu(); private: - static std::unique_ptr terminal_; + static std::unique_ptr terminal_; static std::unique_ptr menu_; }; diff --git a/juci/terminal.cc b/juci/terminal.cc index 1e25e92..5f36da1 100644 --- a/juci/terminal.cc +++ b/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); scrolled_window.add(text_view); add(scrolled_window); -} - -Terminal::Controller::Controller() { - folder_command_ = ""; - view.text_view.signal_size_allocate().connect([this](Gtk::Allocation& allocation){ - auto end=view.text_view.get_buffer()->create_mark(view.text_view.get_buffer()->end()); - view.text_view.scroll_to(end); - view.text_view.get_buffer()->delete_mark(end); + + change_folder_command = ""; + text_view.signal_size_allocate().connect([this](Gtk::Allocation& allocation){ + auto end=text_view.get_buffer()->create_mark(text_view.get_buffer()->end()); + text_view.scroll_to(end); + text_view.get_buffer()->delete_mark(end); }); } -void Terminal::Controller::SetFolderCommand( boost::filesystem::path - CMake_path) { - INFO("Terminal: SetFolderCommand"); - path_ = CMake_path.string(); - folder_command_ = "cd "+ path_ + "; "; +void Terminal::set_change_folder_command(boost::filesystem::path CMake_path) { + INFO("Terminal: set_change_folder_command"); + path = CMake_path.string(); + change_folder_command = "cd "+ path + "; "; } -void Terminal::Controller::Compile(){ - INFO("Terminal: Compile"); - - view.text_view.get_buffer()->set_text(""); - DEBUG("Terminal: Compile: running cmake command"); +void Terminal::compile() { + INFO("Terminal: compile"); + text_view.get_buffer()->set_text(""); + DEBUG("Terminal: compile: running cmake command"); std::vector commands = Singleton::Config::terminal()->compile_commands; for (size_t it = 0; it < commands.size(); ++it) { - ExecuteCommand(commands.at(it), "r"); - + execute_command(commands.at(it), "r"); } print("\n"); - DEBUG("Terminal: Compile: compile done"); + DEBUG("Terminal: compile: compile done"); } -void Terminal::Controller::Run(std::string executable) { - INFO("Terminal: Run"); +void Terminal::run(std::string executable) { + INFO("Terminal: run"); print("juCi++ execute: " + executable + "\n"); - DEBUG("Terminal: Compile: running run command: "); + DEBUG("Terminal: compile: running run command: "); DEBUG_VAR(executable); - ExecuteCommand("cd "+Singleton::Config::terminal()->run_command + "; ./"+executable, "r"); + execute_command("cd "+Singleton::Config::terminal()->run_command + "; ./"+executable, "r"); print("\n"); } -int Terminal::Controller::print(std::string message){ +int Terminal::print(std::string message){ INFO("Terminal: PrintMessage"); - view.text_view.get_buffer()->insert(view.text_view.get_buffer()->end(), "> "+message); - return view.text_view.get_buffer()->end().get_line(); + text_view.get_buffer()->insert(text_view.get_buffer()->end(), "> "+message); + 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); - 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()) iter++; - view.text_view.get_buffer()->insert(iter, message); + text_view.get_buffer()->insert(iter, message); } -std::shared_ptr Terminal::Controller::print_in_progress(std::string start_msg) { +std::shared_ptr Terminal::print_in_progress(std::string start_msg) { std::shared_ptr in_progress=std::shared_ptr(new Terminal::InProgress(start_msg)); return in_progress; } -bool Terminal::Controller::ExistInConsole(std::string string) { - INFO("Terminal: ExistInConsole"); - DEBUG("Terminal: PrintMessage: finding string in buffer"); - double pos = view.text_view.get_buffer()-> - 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"); +void Terminal::execute_command(std::string command, std::string mode) { + INFO("Terminal: execute_command"); + command = change_folder_command+command; + DEBUG("Terminal: PrintMessage: running command"); FILE* p = NULL; std::cout << command << std::endl; p = popen(command.c_str(), mode.c_str()); if (p == NULL) { print("juCi++ ERROR: Failed to run command" + command + "\n"); - }else { + } + else { char buffer[1028]; while (fgets(buffer, 1028, p) != NULL) { print(buffer); } pclose(p); } - } diff --git a/juci/terminal.h b/juci/terminal.h index a5e779b..026884c 100644 --- a/juci/terminal.h +++ b/juci/terminal.h @@ -8,23 +8,14 @@ #include #include -namespace Terminal { +class Terminal : public Gtk::HBox { +public: class Config { public: std::vector compile_commands; 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 { public: InProgress(const std::string& start_msg); @@ -39,26 +30,24 @@ namespace Terminal { std::thread wait_thread; }; - class Controller { - public: - Controller(); - void SetFolderCommand(boost::filesystem::path CMake_path); - void Run(std::string executable); - void Compile(); - int print(std::string message); - void print(int line_nr, std::string message); - std::shared_ptr print_in_progress(std::string start_msg); - Terminal::View view; - private: - void ExecuteCommand(std::string command, std::string mode); - bool OnButtonRealeaseEvent(GdkEventKey* key); - bool ExistInConsole(std::string string); - std::string folder_command_; - std::string path_; - const std::string cmake_sucsess = "Build files have been written to:"; - const std::string make_built = "Built target"; - const std::string make_executable = "Linking CXX executable"; - }; // class controller -} // namespace Terminal + Terminal(); + void set_change_folder_command(boost::filesystem::path CMake_path); + void run(std::string executable); + void compile(); + int print(std::string message); + void print(int line_nr, std::string message); + std::shared_ptr print_in_progress(std::string start_msg); +private: + void execute_command(std::string command, std::string mode); + + Gtk::TextView text_view; + Gtk::ScrolledWindow scrolled_window; + + std::string change_folder_command; + std::string path; + const std::string cmake_sucsess = "Build files have been written to:"; + const std::string make_built = "Built target"; + const std::string make_executable = "Linking CXX executable"; +}; #endif // JUCI_TERMINAL_H_ diff --git a/juci/window.cc b/juci/window.cc index 9f1e931..a5365d8 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -21,13 +21,13 @@ Window::Window() : box(Gtk::ORIENTATION_VERTICAL) { 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.set_position(120); vpaned.set_position(300); 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); show_all_children(); @@ -169,15 +169,15 @@ void Window::add_menu() { notebook.save_current(); if (running.try_lock()) { std::thread execute([this]() { - std::string path = notebook.get_current_view()->file_path; - size_t pos = path.find_last_of("/\\"); - if(pos != std::string::npos) { - path.erase(path.begin()+pos,path.end()); - Singleton::terminal()->SetFolderCommand(path); - } - Singleton::terminal()->Compile(); - std::string executable = directories.get_cmakelists_variable(path,"add_executable"); - Singleton::terminal()->Run(executable); + std::string path = notebook.get_current_view()->file_path; + size_t pos = path.find_last_of("/\\"); + if(pos != std::string::npos) { + path.erase(path.begin()+pos,path.end()); + Singleton::terminal()->set_change_folder_command(path); + } + Singleton::terminal()->compile(); + std::string executable = directories.get_cmakelists_variable(path,"add_executable"); + Singleton::terminal()->run(executable); running.unlock(); }); execute.detach(); @@ -193,9 +193,9 @@ void Window::add_menu() { size_t pos = path.find_last_of("/\\"); if(pos != std::string::npos){ 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(); }); execute.detach();