Last active
October 20, 2016 14:47
-
-
Save gerito1/0df1cb3c3d92ab4b518b5f7b510fa029 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// modules: Gtk | |
// compile: valac --pkg gtk+-3.0 test.vala | |
using Gtk; | |
public class TextFileViewer : Window { | |
private string input1 = "line 1 column 2 - Error: lalala\n"+ | |
"line 96 column 81 - Warning: inserting implicit <p>\n"+ | |
"line 134 column 1 - Warning: inserting implicit <p>\n"+ | |
"line 148 column 69 - Warning: replacing unexpected font with </font>\n"+ | |
"line 151 column 67 - Warning: replacing unexpected em with </em>\n"+ | |
"line 162 column 7 - Warning: inserting implicit <p>\n"+ | |
"line 162 column 11 - Warning: inserting implicit <p>\n"+ | |
"line 170 column 1 - Warning: inserting implicit <p>\n"; | |
private string input2 =" \n"+ //specifically it needs 11 characters before the first "line" to be correct | |
"line 1 column 2 - Error: lalala\n"+ | |
"line 96 column 81 - Warning: inserting implicit <p>\n"+ | |
"line 134 column 1 - Warning: inserting implicit <p>\n"+ | |
"line 148 column 69 - Warning: replacing unexpected font with </font>\n"+ | |
"line 151 column 67 - Warning: replacing unexpected em with </em>\n"+ | |
"line 162 column 7 - Warning: inserting implicit <p>\n"+ | |
"line 162 column 11 - Warning: inserting implicit <p>\n"+ | |
"line 170 column 1 - Warning: inserting implicit <p>\n"; | |
private TextView text_view; | |
//private Entry entry; | |
private Entry regex_entry; | |
private TreeView message_output; | |
private Gtk.ListStore list_store; | |
private Regex re; | |
public TextFileViewer () { | |
this.title = "Text File Viewer"; | |
this.window_position = WindowPosition.CENTER; | |
set_default_size (800, 300); | |
var toolbar = new Toolbar (); | |
toolbar.get_style_context ().add_class (STYLE_CLASS_PRIMARY_TOOLBAR); | |
var run_button = new Button.with_label ("Run"); | |
run_button.clicked.connect (on_run_clicked); | |
var button_item = new ToolItem (); | |
button_item.add (run_button); | |
toolbar.add (button_item); | |
var menu_button = new MenuButton (); | |
menu_button.set_label ("Select input"); | |
var menu = new Gtk.Menu (); | |
menu_button.set_popup (menu); | |
var menuitem1 = new Gtk.MenuItem.with_label ("input1"); | |
menuitem1.activate.connect (on_menuitem_activated); | |
menu.add (menuitem1); | |
var menuitem2 = new Gtk.MenuItem.with_label ("input2"); | |
menuitem2.activate.connect (on_menuitem_activated); | |
menu.add (menuitem2); | |
menu.show_all (); | |
var select_input_button_item = new ToolItem(); | |
select_input_button_item.add (menu_button); | |
toolbar.add (select_input_button_item); | |
this.regex_entry = new Entry(); | |
var regex_item = new ToolItem(); | |
regex_item.add (regex_entry); | |
toolbar.add(regex_item); | |
regex_entry.text = "line (?<line>\\d+) column (?<col>\\d+) -"+ | |
" ((?<err>Error)|(?<war>Warning)): (?<message>.*)"; | |
regex_item.set_expand(true); | |
this.text_view = new TextView (); | |
this.text_view.editable = true; | |
this.text_view.cursor_visible = true; | |
this.text_view.buffer.text = input1; | |
var scroll = new ScrolledWindow (null, null); | |
scroll.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC); | |
scroll.add (this.text_view); | |
var status_window = new ScrolledWindow (null, null); | |
status_window.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC); | |
this.list_store = new Gtk.ListStore (4,typeof (string), typeof (int), typeof (int),typeof (string)); | |
Gtk.TreeIter iter; | |
list_store.append (out iter); | |
list_store.set (iter, 0, "Error",1, 12, | |
2, 2, 3, "error unkown"); | |
this.message_output = new Gtk.TreeView.with_model (list_store); | |
status_window.add (message_output); | |
Gtk.CellRendererText cell = new Gtk.CellRendererText (); | |
message_output.insert_column_with_attributes (-1, "Type", cell, "text", 0); | |
message_output.insert_column_with_attributes (-1, "Line", cell, "text", 1); | |
message_output.insert_column_with_attributes (-1, "Column", cell, "text", 2); | |
message_output.insert_column_with_attributes (-1, "Message", cell, "text", 3); | |
var container = new Grid(); | |
container.attach (toolbar,0,0,1,1); | |
container.attach (scroll,0,1,1,1); | |
container.attach (status_window,0,2,1,1); | |
toolbar.hexpand = true; | |
scroll.vexpand = true; | |
status_window.set_min_content_height(120); | |
add (container); | |
show_all (); | |
} | |
private void on_menuitem_activated (Gtk.MenuItem menuitem) { | |
if (menuitem.get_label() == "input1") { | |
this.text_view.buffer.text = input1; | |
} else { | |
this.text_view.buffer.text = input2; | |
} | |
} | |
private void on_run_clicked () { | |
if (regex_entry.text.length > 0) { | |
try { | |
re = new Regex(regex_entry.text); | |
MatchInfo match; | |
this.list_store.clear (); | |
Gtk.TreeIter iter; | |
for (bool match_exist = re.match(this.text_view.buffer.text, 0, | |
out match); match_exist == true; match_exist = match.next()) { | |
warning ("String searched :" + match.get_string () ); | |
string? type; | |
if (match.fetch_named ("err")!= null && match.fetch_named ("err").length > 0 ) { | |
type = match.fetch_named ("err"); | |
} else if (match.fetch_named ("war")!= null && match.fetch_named ("war").length > 0 ) { | |
type = match.fetch_named ("war"); | |
}else if (match.fetch_named ("inf")!= null && match.fetch_named ("inf").length > 0 ) { | |
type = match.fetch_named ("inf"); | |
}else { | |
type = ""; | |
} | |
this.list_store.append (out iter); | |
warning ("line %s, column %s\n", match.fetch_named("line") ?? "-99", match.fetch_named("col") ?? "-99"); | |
list_store.set (iter, 0, type.dup (),1, int.parse(match.fetch_named("line")), | |
2, int.parse(match.fetch_named("col")), 3, match.fetch_named("message")); | |
} | |
} catch (RegexError er) { | |
if (er is RegexError.COMPILE) | |
warning ("failed to compile"); | |
if (er is RegexError.INTERNAL) | |
warning ("internal error play cool"); | |
if (er is RegexError.UNMATCHED_PARENTHESIS) | |
warning ("Bad day"); | |
warning ( er.message); | |
} | |
} | |
} | |
public static int main (string [] args) { | |
Gtk.init (ref args); | |
var window = new TextFileViewer(); | |
window.destroy.connect(Gtk.main_quit); | |
window.show_all(); | |
Gtk.main(); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment