From 59925ea19c2b9b0f3f2b53d6650ac9df39f500a5 Mon Sep 17 00:00:00 2001 From: oyvang Date: Fri, 17 Apr 2015 15:17:09 +0200 Subject: [PATCH] Working console for linux, missing merge with forgie --- juci/config.json | 4 +- juci/menu.cc | 2 + juci/menu.xml | 6 ++- juci/notebook.cc | 63 +++++++++++++------------ juci/terminal.cc | 120 ++++++++++++++++++++++++++++------------------- juci/terminal.h | 19 +++++--- juci/window.cc | 25 +++++++++- juci/window.h | 3 +- 8 files changed, 153 insertions(+), 89 deletions(-) diff --git a/juci/config.json b/juci/config.json index e4ab5e7..03cbad8 100644 --- a/juci/config.json +++ b/juci/config.json @@ -24,7 +24,9 @@ "new_cc_file": "c", "close_tab": "w", "open_folder": "o", - "save_as": "s" + "save_as": "s", + "compile_and_run": "r>", + "compile": "r" }, "directoryfilter": { "ignore": [ diff --git a/juci/menu.cc b/juci/menu.cc index 880d7df..4fba192 100644 --- a/juci/menu.cc +++ b/juci/menu.cc @@ -27,6 +27,8 @@ Menu::Controller::Controller(Keybindings::Controller& keybindings) : [this]() { OnWindowSplitWindow(); }); + keybindings_.action_group_menu()->add(Gtk::Action::create("ProjectMenu", + "P_roject")); keybindings_.action_group_menu()->add(Gtk::Action::create("PluginMenu", "_Plugins")); keybindings_.action_group_menu()->add(Gtk::Action::create("HelpMenu", diff --git a/juci/menu.xml b/juci/menu.xml index e5ad384..7007147 100644 --- a/juci/menu.xml +++ b/juci/menu.xml @@ -18,7 +18,11 @@ - + + + + + diff --git a/juci/notebook.cc b/juci/notebook.cc index b5f437b..a2d0d93 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -151,39 +151,42 @@ bool Notebook::Controller::GeneratePopup(){ std::vector items; Gtk::TextIter start = CurrentTextView().get_buffer()->get_insert()->get_iter(); text_vec_.at(CurrentPage())->GetAutoCompleteSuggestions(start.get_line()+1, - start.get_line_offset()+2, - &items); + start.get_line_offset()+2, + &items); std::cout << items.size()<< std::endl; + if(items.size()>0){ - // Replace over with get suggestions from zalox! OVER IS JUST FOR TESTING + // Replace over with get suggestions from zalox! OVER IS JUST FOR TESTING - Gtk::ScrolledWindow popup_scroll_; - Gtk::ListViewText listview_(1,false,Gtk::SelectionMode::SELECTION_SINGLE); - - popup_scroll_.set_policy(Gtk::PolicyType::POLICY_NEVER, - Gtk::PolicyType::POLICY_NEVER); - listview_.set_enable_search(false); - listview_.set_headers_visible(false); - listview_.set_hscroll_policy(Gtk::ScrollablePolicy::SCROLL_NATURAL); - listview_.set_activate_on_single_click(true); - for (auto &i : items) listview_.append(i); - popup_scroll_.add(listview_); - popup_.get_vbox()->pack_start(popup_scroll_); - popup_.set_transient_for(*window_); - popup_.show_all(); - - int popup_x = popup_.get_width(); - int popup_y = items.size()*20; - PopupSetSize(popup_scroll_,popup_x,popup_y); - int x,y; - FindPopupPosition(CurrentTextView(),popup_x,popup_y,x,y); - popup_.move(x, y+15); - PopupSelectHandler(popup_, listview_); - ispopup = true; - popup_.run(); - popup_.hide(); - ispopup = false; - return true; + Gtk::ScrolledWindow popup_scroll_; + Gtk::ListViewText listview_(1,false,Gtk::SelectionMode::SELECTION_SINGLE); + + popup_scroll_.set_policy(Gtk::PolicyType::POLICY_NEVER, + Gtk::PolicyType::POLICY_NEVER); + listview_.set_enable_search(false); + listview_.set_headers_visible(false); + listview_.set_hscroll_policy(Gtk::ScrollablePolicy::SCROLL_NATURAL); + listview_.set_activate_on_single_click(true); + for (auto &i : items) listview_.append(i); + popup_scroll_.add(listview_); + popup_.get_vbox()->pack_start(popup_scroll_); + popup_.set_transient_for(*window_); + popup_.show_all(); + + int popup_x = popup_.get_width(); + int popup_y = items.size()*20; + PopupSetSize(popup_scroll_,popup_x,popup_y); + int x,y; + FindPopupPosition(CurrentTextView(),popup_x,popup_y,x,y); + popup_.move(x, y+15); + PopupSelectHandler(popup_, listview_); + ispopup = true; + popup_.run(); + popup_.hide(); + ispopup = false; + return true; + } + return false; } bool Notebook::Controller::ScrollEventCallback(GdkEventScroll* scroll_event) { diff --git a/juci/terminal.cc b/juci/terminal.cc index d81ff11..e512af0 100644 --- a/juci/terminal.cc +++ b/juci/terminal.cc @@ -1,70 +1,94 @@ #include "terminal.h" #include +#include Terminal::View::View(){ scrolledwindow_.add(textview_); scrolledwindow_.set_size_request(-1,150); view_.add(scrolledwindow_); + textview_.set_editable(false); + //Pango::TabArray tabsize; + //tabsize.set_tab(200,Pango::TAB_LEFT, 200); + //textview_.set_tabs(tabsize); } Terminal::Controller::Controller() { - root = ""; - Terminal().signal_key_release_event(). - connect(sigc::mem_fun(*this,&Terminal::Controller::OnButtonRealeaseEvenet),false); + folder_command_ = ""; } -bool Terminal::Controller::OnButtonRealeaseEvenet(GdkEventKey *key) { - if(key->keyval == 65421 || key->keyval == 65293){ - ExecuteCommand(); - } - return false; +void Terminal::Controller::SetFolderCommand(std::string path) { + int pos = path.find_last_of("/\\"); + path.erase(path.begin()+pos,path.end()); + folder_command_ = "cd "+ path + "; "; } -void Terminal::Controller::ExecuteCommand() { - - std::cout << "EXECUTE COMMAND ALGORITHM "<< std::endl; - std::string temp = getCommand(); - if(temp != ""){ - std::cout << "EXECUTE COMMAND: "<insert(Terminal().get_buffer()->end(),"Command Failed\n"); - } else{ - char buffer[1028]; - - while (fgets(buffer, 1028, p) != NULL) - { - Terminal().get_buffer()->insert(Terminal().get_buffer()->end(),buffer); - } - pclose(p); +void Terminal::Controller::CompileAndRun(std::string project_name) { + if (folder_command_=="") { + PrintMessage("juCi++ ERROR: Can not find project's CMakeList.txt\n"); + } else { + if (running.try_lock()) { + std::thread execute([=]() { + Terminal().get_buffer()->set_text(""); + ExecuteCommand("cmake ."); + if (ExistInConsole(cmake_sucsess)){ + ExecuteCommand("make"); + if (ExistInConsole(make_built)){ + if (FindExecutable(project_name)) { + ExecuteCommand("./"+project_name); + } else { + PrintMessage("juCi++ ERROR: Can not find Executable\n"); + } + } + } + }); + execute.detach(); + running.unlock(); + } + } +} - - } - Terminal().get_buffer()->insert(Terminal().get_buffer()->end(),"Command Executed\n"); - }else{ - std::cout << "NO COMMAND TO RUN"<< std::endl; - } - +void Terminal::Controller::PrintMessage(std::string message){ + Terminal().get_buffer()-> + insert(Terminal().get_buffer()-> end(),"> "+message); } -std::string Terminal::Controller::getCommand(){ - std::string command = ""; - Gtk::TextIter start,end; - int a = Terminal().get_buffer()->get_insert()->get_iter().get_line()-1; - if(a==-1)a=0; - start = Terminal().get_buffer()->get_iter_at_line(a); - end =Terminal().get_buffer()->get_iter_at_line(a); - while(!end.ends_line()) { - end++; +bool Terminal::Controller::FindExecutable(std::string executable) { + std::string build = Terminal().get_buffer()->get_text(); + double pos = build.find(make_built); + Gtk::TextIter start = Terminal().get_buffer()->get_iter_at_offset(pos); + Gtk::TextIter end = Terminal().get_buffer()->get_iter_at_offset(pos); + while (!end.ends_line()) { + end.forward_char(); } - while(!start.starts_line()) { - start--; - } - - command = Terminal().get_buffer()->get_text(start,end); - return command; + build = Terminal().get_buffer()->get_text(start, end); + pos = build.find_last_of(" "); + std::cout << "FINNER NY POS" << std::endl; + build = build.substr(pos+1); + std::cout <<"BUILD TARGET = "<< build << std::endl; + std::cout << "EXECUTABLE FILE = "<< executable << std::endl; + if(build != executable) return false; + return true; } +bool Terminal::Controller::ExistInConsole(std::string string) { + double pos = Terminal().get_buffer()-> + get_text().find(string); + if (pos == std::string::npos) return false; + return true; +} +void Terminal::Controller::ExecuteCommand(std::string command) { + command = folder_command_+command; + std::cout << "EXECUTE COMMAND: "<< command << std::endl; + FILE* p = popen(command.c_str(), "r"); + if (p == NULL) { + PrintMessage("juCi++ ERROR: Failed to run command" + command + "\n"); + }else { + char buffer[1028]; + while (fgets(buffer, 1028, p) != NULL) { + PrintMessage(buffer); + } + pclose(p); + } +} diff --git a/juci/terminal.h b/juci/terminal.h index d91876c..fe890fc 100644 --- a/juci/terminal.h +++ b/juci/terminal.h @@ -1,6 +1,7 @@ #ifndef JUCI_TERMINAL_H_ #define JUCI_TERMINAL_H_ +#include #include "gtkmm.h" namespace Terminal { @@ -21,14 +22,20 @@ namespace Terminal { Controller(); Gtk::HBox& view() {return view_.view();} Gtk::TextView& Terminal(){return view_.textview();} + void SetFolderCommand(std::string path); + void CompileAndRun(std::string project_name); private: - void ExecuteCommand(); - std::string getCommand(); - bool OnButtonRealeaseEvenet(GdkEventKey* key); + void ExecuteCommand(std::string command); + bool OnButtonRealeaseEvent(GdkEventKey* key); + bool ExistInConsole(std::string string); + bool FindExecutable(std::string executable); + void PrintMessage(std::string message); Terminal::View view_; - std::string root; - - + std::string folder_command_; + std::mutex running; + 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 diff --git a/juci/window.cc b/juci/window.cc index a922c67..61033ae 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -5,7 +5,7 @@ Window::Window() : main_config_(), keybindings_(main_config_.keybindings_cfg()), notebook_(*this,keybindings(), main_config_.source_cfg(), main_config_.dir_cfg()), - menu_(keybindings()) { + menu_(keybindings()) { set_title("juCi++"); set_default_size(600, 400); add(window_box_); @@ -34,6 +34,28 @@ Window::Window() : [this]() { OnSaveFileAs(); }); + + keybindings_.action_group_menu()->add(Gtk::Action::create("ProjectCompileAndRun", + "Compile And Run"), + Gtk::AccelKey(keybindings_.config_ + .key_map()["compile_and_run"]), + [this]() { + terminal_. + SetFolderCommand("/home/gm/ClionProjects/testi/CM.txt"); + std::string p = notebook_.directories().get_project_name("/home/gm/ClionProjects/testi"); + terminal_.CompileAndRun(p); + }); + + keybindings_.action_group_menu()->add(Gtk::Action::create("ProjectCompile", + "Compile"), + Gtk::AccelKey(keybindings_.config_ + .key_map()["compile"]), + [this]() { + terminal_. + SetFolderCommand("/home/gm/ClionProjects/testi/CM.txt"); + std::string p = notebook_.directories().get_project_name("/home/gm/ClionProjects/testi"); + terminal_.CompileAndRun(p); + }); this->signal_button_release_event(). connect(sigc::mem_fun(*this,&Window::OnMouseRelease),false); @@ -77,6 +99,7 @@ void Window::OnFileOpenFolder() { std::cout << "Folder selected: " << dialog.get_filename() << std::endl; notebook_.directories().open_folder(dialog.get_filename()); + std::cout << dialog.get_filename()<< std::endl; break; } case(Gtk::RESPONSE_CANCEL): diff --git a/juci/window.h b/juci/window.h index ca28c3b..954072e 100644 --- a/juci/window.h +++ b/juci/window.h @@ -29,8 +29,7 @@ public: void OnOpenFile(); void OnFileOpenFolder(); void OnSaveFileAs(); - bool OnMouseRelease(GdkEventButton* button); - + bool OnMouseRelease(GdkEventButton* button); }; #endif // JUCI_WINDOW_H