Browse Source

Mainly cleanup of terminal.*. Added first time clang-parsing message in terminal.

merge-requests/365/head
eidheim 11 years ago
parent
commit
6d7c4ffa09
  1. 16
      juci/config.cc
  2. 9
      juci/config.h
  3. 8
      juci/juci.cc
  4. 29
      juci/notebook.cc
  5. 17
      juci/notebook.h
  6. 15
      juci/source.cc
  7. 7
      juci/source.h
  8. 79
      juci/terminal.cc
  9. 33
      juci/terminal.h
  10. 54
      juci/window.cc
  11. 6
      juci/window.h

16
juci/config.cc

@ -2,7 +2,7 @@
#include "logging.h"
MainConfig::MainConfig() :
keybindings_cfg_(), source_cfg() {
keybindings_cfg(), source_cfg() {
INFO("Reading config file");
boost::property_tree::json_parser::read_json("config.json", cfg_);
INFO("Config file read");
@ -22,7 +22,7 @@ void MainConfig::GenerateSource() {
auto visual_json = source_json.get_child("visual");
for (auto &i : visual_json) {
if (i.first == "background") {
source_cfg.background = i.second.get_value<std::string>();
source_cfg.background = i.second.get_value<std::string>();
}
if (i.first == "show_line_numbers") {
source_cfg.show_line_numbers = i.second.get_value<std::string>() == "1" ? true : false;
@ -55,10 +55,10 @@ void MainConfig::GenerateTerminalCommands() {
boost::property_tree::ptree compile_commands_json = source_json.get_child("compile_commands");
boost::property_tree::ptree run_commands_json = source_json.get_child("run_commands");
for (auto &i : compile_commands_json) {
terminal_cfg_.InsertCompileCommand(i.second.get_value<std::string>());
terminal_cfg.compile_commands.emplace_back(i.second.get_value<std::string>());
}
for (auto &i : run_commands_json) {
terminal_cfg_.SetRunCommand(i.second.get_value<std::string>());
terminal_cfg.run_command=(i.second.get_value<std::string>()); //TODO: run_commands array->one run_command?
}
}
@ -68,11 +68,11 @@ void MainConfig::GenerateKeybindings() {
std::ifstream menu_xml("menu.xml");
if (menu_xml.is_open()) {
while (getline(menu_xml, line))
keybindings_cfg_.AppendXml(line);
keybindings_cfg.AppendXml(line);
}
boost::property_tree::ptree keys_json = cfg_.get_child("keybindings");
for (auto &i : keys_json)
keybindings_cfg_.key_map()[i.first] = i.second.get_value<std::string>();
keybindings_cfg.key_map()[i.first] = i.second.get_value<std::string>();
DEBUG("Keybindings fetched");
}
@ -82,8 +82,8 @@ void MainConfig::GenerateDirectoryFilter() {
boost::property_tree::ptree ignore_json = dir_json.get_child("ignore");
boost::property_tree::ptree except_json = dir_json.get_child("exceptions");
for ( auto &i : except_json )
dir_cfg_.AddException(i.second.get_value<std::string>());
dir_cfg.AddException(i.second.get_value<std::string>());
for ( auto &i : ignore_json )
dir_cfg_.AddIgnore(i.second.get_value<std::string>());
dir_cfg.AddIgnore(i.second.get_value<std::string>());
DEBUG("Directory filter fetched");
}

9
juci/config.h

@ -12,10 +12,10 @@
class MainConfig {
public:
Source::Config source_cfg;
Terminal::Config terminal_cfg;
Keybindings::Config keybindings_cfg;
Directories::Config dir_cfg;
MainConfig();
Keybindings::Config& keybindings_cfg() { return keybindings_cfg_; }
Directories::Config& dir_cfg() { return dir_cfg_; }
Terminal::Config& terminal_cfg() { return terminal_cfg_; }
void PrintMenu();
void GenerateSource();
void GenerateKeybindings();
@ -24,8 +24,5 @@ public:
private:
boost::property_tree::ptree cfg_;
boost::property_tree::ptree key_tree_;
Keybindings::Config keybindings_cfg_;
Directories::Config dir_cfg_;
Terminal::Config terminal_cfg_;
};
#endif

8
juci/juci.cc

@ -35,12 +35,12 @@ void Juci::on_activate() {
add_window(*window);
window->show();
if(directory!="") {
//TODO: use the following instead, window->notebook_.open_directory(directory);
window->notebook_.project_path=directory;
window->notebook_.directories.open_folder(directory);
//TODO: use the following instead, window->notebook.open_directory(directory);
window->notebook.project_path=directory;
window->notebook.directories.open_folder(directory);
}
for(auto &f: files)
window->notebook_.OnOpenFile(f);
window->notebook.OnOpenFile(f);
}
int main(int argc, char *argv[]) {

29
juci/notebook.cc

@ -2,21 +2,21 @@
#include "notebook.h"
#include "logging.h"
Notebook::View::View() : notebook_() {
view_.pack2(notebook_);
view_.set_position(120);
Notebook::View::View() {
pack2(notebook);
set_position(120);
}
Notebook::Controller::Controller(Gtk::Window* window,
Keybindings::Controller& keybindings,
Notebook::Controller::Controller(Keybindings::Controller& keybindings,
Terminal::Controller& terminal,
Source::Config& source_cfg,
Directories::Config& dir_cfg) :
terminal(terminal),
directories(dir_cfg),
source_config(source_cfg) {
INFO("Create notebook");
window_ = window;
refClipboard_ = Gtk::Clipboard::get();
view().pack1(directories.widget(), true, true);
view.pack1(directories.widget(), true, true);
CreateKeybindings(keybindings);
INFO("Notebook Controller Success");
} // Constructor
@ -163,14 +163,10 @@ Notebook::Controller::~Controller() {
for (auto &i : scrolledtext_vec_) delete i;
}
Gtk::Paned& Notebook::Controller::view() {
return view_.view();
}
void Notebook::Controller::OnOpenFile(std::string path) {
INFO("Notebook open file");
INFO("Notebook create page");
text_vec_.emplace_back(new Source::Controller(source_config, path, project_path));
text_vec_.emplace_back(new Source::Controller(source_config, path, project_path, terminal));
scrolledtext_vec_.push_back(new Gtk::ScrolledWindow());
editor_vec_.push_back(new Gtk::HBox());
scrolledtext_vec_.back()->add(*text_vec_.back()->view);
@ -233,7 +229,6 @@ void Notebook::Controller::OnEditCut() {
std::string Notebook::Controller::GetCursorWord() {
INFO("Notebook get cursor word");
int page = CurrentPage();
std::string word;
Gtk::TextIter start, end;
start = CurrentTextView().get_buffer()->get_insert()->get_iter();
@ -261,7 +256,6 @@ void Notebook::Controller::OnEditSearch() {
void Notebook::Controller::Search(bool forward) {
INFO("Notebook search");
int page = CurrentPage();
std::string search_word;
search_word = entry();
Gtk::TextIter test;
@ -325,7 +319,7 @@ int Notebook::Controller::Pages() {
return Notebook().get_n_pages();
}
Gtk::Notebook& Notebook::Controller::Notebook() {
return view_.notebook();
return view.notebook;
}
bool Notebook::Controller:: OnSaveFile() {
@ -354,10 +348,9 @@ bool Notebook::Controller:: OnSaveFile(std::string path) {
std::string Notebook::Controller::OnSaveFileAs(){
INFO("Notebook save as");
Gtk::FileChooserDialog dialog("Please choose a file",
Gtk::FileChooserDialog dialog((Gtk::Window&)(*view.get_toplevel()), "Please choose a file",
Gtk::FILE_CHOOSER_ACTION_SAVE);
DEBUG("SET TRANSISTEN FPR");
dialog.set_transient_for(*window_);
dialog.set_position(Gtk::WindowPosition::WIN_POS_CENTER_ALWAYS);
dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
dialog.add_button("_Save", Gtk::RESPONSE_OK);
@ -385,7 +378,7 @@ std::string Notebook::Controller::OnSaveFileAs(){
void Notebook::Controller::AskToSaveDialog() {
INFO("AskToSaveDialog");
DEBUG("AskToSaveDialog: Finding file path");
Gtk::MessageDialog dialog(*window_, "Save file!",
Gtk::MessageDialog dialog((Gtk::Window&)(*view.get_toplevel()), "Save file!",
false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);
dialog.set_secondary_text(
"Do you want to save: " +

17
juci/notebook.h

@ -12,20 +12,18 @@
#include <sigc++/sigc++.h>
#include "clangmm.h"
#include "keybindings.h"
#include "terminal.h"
namespace Notebook {
class View {
class View : public Gtk::Paned {
public:
View();
Gtk::Paned& view() {return view_;}
Gtk::Notebook& notebook() {return notebook_; }
protected:
Gtk::Paned view_;
Gtk::Notebook notebook_;
Gtk::Notebook notebook;
};
class Controller {
public:
Controller(Gtk::Window* window, Keybindings::Controller& keybindings,
Controller(Keybindings::Controller& keybindings,
Terminal::Controller& terminal,
Source::Config& config,
Directories::Config& dir_cfg);
~Controller();
@ -45,8 +43,8 @@ namespace Notebook {
Gtk::TreeViewColumn* column);
void OnOpenFile(std::string filename);
int Pages();
Gtk::Paned& view();
void Search(bool forward);
View view;
std::string OnSaveFileAs();
std::string project_path;
Directories::Controller directories; //Todo: make private after creating open_directory()
@ -56,8 +54,8 @@ namespace Notebook {
void AskToSaveDialog();
Glib::RefPtr<Gtk::Builder> m_refBuilder;
Glib::RefPtr<Gio::SimpleActionGroup> refActionGroup;
Terminal::Controller& terminal;
Source::Config& source_config;
View view_;
std::vector<std::unique_ptr<Source::Controller> > text_vec_;
std::vector<Gtk::ScrolledWindow*> scrolledtext_vec_;
@ -66,7 +64,6 @@ namespace Notebook {
Gtk::TextIter search_match_end_;
Gtk::TextIter search_match_start_;
Glib::RefPtr<Gtk::Clipboard> refClipboard_;
Gtk::Window* window_;
}; // class controller
} // namespace Notebook
#endif // JUCI_NOTEBOOK_H_

15
juci/source.cc

@ -134,8 +134,8 @@ bool Source::View::on_key_press(GdkEventKey* key) {
//////////////////
clang::Index Source::ClangView::clang_index(0, 1);
Source::ClangView::ClangView(const Source::Config& config, const std::string& file_path, const std::string& project_path):
Source::View(config, file_path, project_path),
Source::ClangView::ClangView(const Source::Config& config, const std::string& file_path, const std::string& project_path, Terminal::Controller& terminal):
Source::View(config, file_path, project_path), terminal(terminal),
parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) {
override_font(Pango::FontDescription(config.font));
override_background_color(Gdk::RGBA(config.background));
@ -163,10 +163,15 @@ parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) {
parse_thread_go=true;
});
parse_done.connect([this](){
auto first_time_parse_done=this->terminal.PrintMessage("Parsing "+file_path, "finished");
parse_done.connect([this, first_time_parse_done](){
if(parse_thread_mapped) {
INFO("Updating syntax");
update_syntax(extract_tokens(0, get_source_buffer()->get_text().size()));
if(first_time_parse) {
first_time_parse_done();
first_time_parse=false;
}
INFO("Syntax updated");
}
else {
@ -521,12 +526,12 @@ bool Source::ClangView::on_key_press(GdkEventKey* key) {
// Source::Controller::Controller()
// Constructor for Controller
Source::Controller::Controller(const Source::Config &config,
const std::string& file_path, std::string project_path) {
const std::string& file_path, std::string project_path, Terminal::Controller& terminal) {
if(project_path=="") {
project_path=boost::filesystem::path(file_path).parent_path().string();
}
if (config.legal_extension(file_path.substr(file_path.find_last_of(".") + 1)))
view=std::unique_ptr<View>(new ClangView(config, file_path, project_path));
view=std::unique_ptr<View>(new ClangView(config, file_path, project_path, terminal));
else
view=std::unique_ptr<View>(new GenericView(config, file_path, project_path));
INFO("Source Controller with childs constructed");

7
juci/source.h

@ -10,6 +10,7 @@
#include <string>
#include <atomic>
#include "gtksourceviewmm.h"
#include "terminal.h"
namespace Source {
class Config {
@ -81,7 +82,7 @@ namespace Source {
class ClangView : public View {
public:
ClangView(const Source::Config& config, const std::string& file_path, const std::string& project_path);
ClangView(const Source::Config& config, const std::string& file_path, const std::string& project_path, Terminal::Controller& terminal);
~ClangView();
// inits the syntax highligthing on file open
void init_syntax_highlighting(const std::map<std::string, std::string>
@ -107,6 +108,8 @@ namespace Source {
std::vector<std::string> get_compilation_commands();
bool on_key_press(GdkEventKey* key);
bool on_key_release(GdkEventKey* key);
Terminal::Controller& terminal;
bool first_time_parse=true;
Glib::Dispatcher parse_done;
Glib::Dispatcher parse_start;
@ -121,7 +124,7 @@ namespace Source {
class Controller {
public:
Controller(const Source::Config &config,
const std::string& file_path, std::string project_path);
const std::string& file_path, std::string project_path, Terminal::Controller& terminal);
Glib::RefPtr<Gsv::Buffer> buffer();
bool is_saved = true;

79
juci/terminal.cc

@ -1,36 +1,18 @@
#include "terminal.h"
#include <iostream>
#include <thread>
#include <atomic>
#include "logging.h"
Terminal::Config::Config() {
}
Terminal::Config::Config(Terminal::Config& original) :
run_command_(original.run_command_){
for (size_t it = 0; it<original.compile_commands().size(); ++it) {
InsertCompileCommand(original.compile_commands().at(it));
}
}
void Terminal::Config::InsertCompileCommand(std::string command){
compile_commands_.push_back(command);
}
void Terminal::Config::SetRunCommand(std::string command){
run_command_ = command;
}
Terminal::View::View(){
scrolledwindow_.add(textview_);
scrolledwindow_.set_size_request(-1,150);
view_.add(scrolledwindow_);
textview_.set_editable(false);
text_view.set_editable(false);
scrolled_window.add(text_view);
add(scrolled_window);
}
Terminal::Controller::Controller(Terminal::Config& cfg) :
config_(cfg) {
config(cfg) {
folder_command_ = "";
}
@ -44,9 +26,9 @@ void Terminal::Controller::SetFolderCommand( boost::filesystem::path
void Terminal::Controller::Compile(){
INFO("Terminal: Compile");
Terminal().get_buffer()->set_text("");
view.text_view.get_buffer()->set_text("");
DEBUG("Terminal: Compile: running cmake command");
std::vector<std::string> commands = config().compile_commands();
std::vector<std::string> commands = config.compile_commands;
for (size_t it = 0; it < commands.size(); ++it) {
ExecuteCommand(commands.at(it), "r");
@ -60,21 +42,58 @@ void Terminal::Controller::Run(std::string executable) {
PrintMessage("juCi++ execute: " + executable + "\n");
DEBUG("Terminal: Compile: running run command: ");
DEBUG_VAR(executable);
ExecuteCommand("cd "+config().run_command() + "; ./"+executable, "r");
ExecuteCommand("cd "+config.run_command + "; ./"+executable, "r");
PrintMessage("\n");
}
void Terminal::Controller::PrintMessage(std::string message){
int Terminal::Controller::PrintMessage(std::string message){
INFO("Terminal: PrintMessage");
Terminal().get_buffer()->
insert(Terminal().get_buffer()-> end(),"> "+message);
view.text_view.get_buffer()->insert(view.text_view.get_buffer()->end(), "> "+message);
auto mark_end=view.text_view.get_buffer()->create_mark(view.text_view.get_buffer()->end());
view.text_view.scroll_to(mark_end);
return mark_end->get_iter().get_line();
}
void Terminal::Controller::PrintMessage(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);
while(!iter.ends_line())
iter++;
view.text_view.get_buffer()->insert(iter, message);
}
std::function<void()> Terminal::Controller::PrintMessage(std::string start_msg, std::string done_msg) {
int line_nr=PrintMessage(start_msg+"...\n");
std::shared_ptr<std::atomic<bool> > stop(new std::atomic<bool>(false));
std::shared_ptr<Glib::Dispatcher> waiting_print(new Glib::Dispatcher());
waiting_print->connect([this, line_nr](){
PrintMessage(line_nr-1, ".");
});
std::shared_ptr<std::thread> wait_thread(new std::thread([this, stop, waiting_print](){
size_t c=0;
while(!*stop) {
if(c%100==0)
(*waiting_print)();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
c++;
}
}));
std::function<void()> done=[this, line_nr, stop, done_msg, waiting_print, wait_thread](){
*stop=true;
PrintMessage(line_nr-1, done_msg);
if(wait_thread->joinable())
wait_thread->join(); //waiting_print has to be destroyed in main thread
};
return done;
}
bool Terminal::Controller::ExistInConsole(std::string string) {
INFO("Terminal: ExistInConsole");
DEBUG("Terminal: PrintMessage: finding string in buffer");
double pos = Terminal().get_buffer()->
double pos = view.text_view.get_buffer()->
get_text().find(string);
if (pos == std::string::npos) return false;
return true;

33
juci/terminal.h

@ -2,6 +2,7 @@
#define JUCI_TERMINAL_H_
#include <mutex>
#include <functional>
#include "gtkmm.h"
#include <boost/filesystem.hpp>
@ -9,44 +10,32 @@ namespace Terminal {
class Config {
public:
Config ();
Config(Terminal::Config& original);
std::vector<std::string>& compile_commands() { return compile_commands_; }
void InsertCompileCommand(std::string command);
std::string& run_command() { return run_command_; }
void SetRunCommand(std::string command);
private:
std::vector<std::string> compile_commands_;
std::string run_command_;
std::vector<std::string> compile_commands;
std::string run_command;
};
class View {
class View : public Gtk::HBox {
public:
View();
Gtk::HBox& view() {return view_;}
Gtk::TextView& textview() {return textview_;}
private:
Gtk::HBox view_;
Gtk::TextView textview_;
Gtk::ScrolledWindow scrolledwindow_;
Gtk::TextView text_view;
Gtk::ScrolledWindow scrolled_window;
}; // class view
class Controller {
public:
Controller(Terminal::Config& cfg);
Gtk::HBox& view() {return view_.view();}
Gtk::TextView& Terminal(){return view_.textview();}
void SetFolderCommand(boost::filesystem::path CMake_path);
void Run(std::string executable);
void Compile();
Terminal::Config& config() { return config_; }
void PrintMessage(std::string message);
int PrintMessage(std::string message);
void PrintMessage(int line_nr, std::string message);
std::function<void()> PrintMessage(std::string start_msg, std::string stop_msg);
Terminal::View view;
private:
Terminal::Config config_;
Terminal::Config& config;
void ExecuteCommand(std::string command, std::string mode);
bool OnButtonRealeaseEvent(GdkEventKey* key);
bool ExistInConsole(std::string string);
Terminal::View view_;
std::string folder_command_;
std::string path_;
const std::string cmake_sucsess = "Build files have been written to:";

54
juci/window.cc

@ -4,13 +4,13 @@
Window::Window() :
window_box_(Gtk::ORIENTATION_VERTICAL),
main_config_(),
keybindings_(main_config_.keybindings_cfg()),
terminal_(main_config_.terminal_cfg()),
notebook_(this,keybindings(),
keybindings_(main_config_.keybindings_cfg),
terminal(main_config_.terminal_cfg),
notebook(keybindings(), terminal,
main_config_.source_cfg,
main_config_.dir_cfg()),
main_config_.dir_cfg),
menu_(keybindings()),
api_(menu_, notebook_) {
api_(menu_, notebook) {
INFO("Create Window");
set_title("juCi++");
set_default_size(600, 400);
@ -66,16 +66,16 @@ Window::Window() :
SaveFile();
if (running.try_lock()) {
std::thread execute([=]() {
std::string path = notebook_.CurrentTextView().file_path;
std::string path = notebook.CurrentTextView().file_path;
size_t pos = path.find_last_of("/\\");
if(pos != std::string::npos) {
path.erase(path.begin()+pos,path.end());
terminal_.SetFolderCommand(path);
terminal.SetFolderCommand(path);
}
terminal_.Compile();
std::string executable = notebook_.directories.
terminal.Compile();
std::string executable = notebook.directories.
GetCmakeVarValue(path,"add_executable");
terminal_.Run(executable);
terminal.Run(executable);
running.unlock();
});
execute.detach();
@ -92,13 +92,13 @@ Window::Window() :
SaveFile();
if (running.try_lock()) {
std::thread execute([=]() {
std::string path = notebook_.CurrentTextView().file_path;
std::string path = notebook.CurrentTextView().file_path;
size_t pos = path.find_last_of("/\\");
if(pos != std::string::npos){
path.erase(path.begin()+pos,path.end());
terminal_.SetFolderCommand(path);
terminal.SetFolderCommand(path);
}
terminal_.Compile();
terminal.Compile();
running.unlock();
});
execute.detach();
@ -111,10 +111,10 @@ Window::Window() :
window_box_.pack_start(menu_.view(), Gtk::PACK_SHRINK);
window_box_.pack_start(notebook_.entry, Gtk::PACK_SHRINK);
window_box_.pack_start(notebook.entry, Gtk::PACK_SHRINK);
paned_.set_position(300);
paned_.pack1(notebook_.view(), true, false);
paned_.pack2(terminal_.view(), true, true);
paned_.pack1(notebook.view, true, false);
paned_.pack2(terminal.view, true, true);
window_box_.pack_end(paned_);
show_all_children();
INFO("Window created");
@ -139,8 +139,8 @@ void Window::OnFileOpenFolder() {
case(Gtk::RESPONSE_OK):
{
std::string project_path=dialog.get_filename();
notebook_.project_path=project_path;
notebook_.directories.open_folder(project_path);
notebook.project_path=project_path;
notebook.directories.open_folder(project_path);
break;
}
case(Gtk::RESPONSE_CANCEL):
@ -188,7 +188,7 @@ void Window::OnOpenFile() {
switch (result) {
case(Gtk::RESPONSE_OK): {
std::string path = dialog.get_filename();
notebook_.OnOpenFile(path);
notebook.OnOpenFile(path);
break;
}
case(Gtk::RESPONSE_CANCEL): {
@ -201,20 +201,20 @@ void Window::OnOpenFile() {
}
bool Window::SaveFile() {
if(notebook_.OnSaveFile()) {
terminal_.PrintMessage("File saved to: " +
notebook_.CurrentTextView().file_path+"\n");
if(notebook.OnSaveFile()) {
terminal.PrintMessage("File saved to: " +
notebook.CurrentTextView().file_path+"\n");
return true;
}
terminal_.PrintMessage("File not saved");
terminal.PrintMessage("File not saved");
return false;
}
bool Window::SaveFileAs() {
if(notebook_.OnSaveFile(notebook_.OnSaveFileAs())){
terminal_.PrintMessage("File saved to: " +
notebook_.CurrentTextView().file_path+"\n");
if(notebook.OnSaveFile(notebook.OnSaveFileAs())){
terminal.PrintMessage("File saved to: " +
notebook.CurrentTextView().file_path+"\n");
return true;
}
terminal_.PrintMessage("File not saved");
terminal.PrintMessage("File not saved");
return false;
}

6
juci/window.h

@ -15,13 +15,11 @@ public:
Gtk::Box window_box_;
virtual ~Window() { }
//private:
MainConfig main_config_;
Keybindings::Controller keybindings_;
Menu::Controller menu_;
Notebook::Controller notebook_;
Terminal::Controller terminal_;
Notebook::Controller notebook;
Terminal::Controller terminal;
PluginApi api_;
Keybindings::Controller& keybindings() { return keybindings_; }

Loading…
Cancel
Save