Commit a2ca3214 authored by Alberto Fanjul's avatar Alberto Fanjul

Refactor config-file option

Read custom color from config-file too
parent e04396c5
......@@ -27,8 +27,30 @@ Application Options:
--text=TEXT Text to parse for urls
--link-color=COLOR Color name or #rgb for links
--custom-link=REGEX,REPLACEMENT,COLOR Pattern to transform custom links
--config-file=PATH Custom path for config file
--display=DISPLAY X display to use
```
## Running
./TextViewHyperLinks --custom-link '!([0-9]+),https://gitlab.gnome.org/GNOME/gitg/merge_requests/\1,orange' --custom-link '#([0-9]+),https://gitlab.gnome.org/GNOME/gitg/issues/\1,green' --text 'this is a text with an url https://gitlab.gnome.org/GNOME/gitg/issues/152 and a hidden link #152 and hidden MR !21 and documentation http://example.com/doc?param=1&chapter=4 for sure' --link-color "#0000ee"
You can add config to a ini file (`.git/config` would be the most obvious one) but `custom.ini` on current dir is the default
```
[custom-link "issues"]
regexp=!([0-9]+)
replacement=https://gitlab.gnome.org/GNOME/gitg/merge_requests/\\1
color = orange
[custom-link "merge.request"]
regexp=#([0-9]+)
replacement=https://gitlab.gnome.org/GNOME/gitg/merge_requests/\\1
color = green
[textviewhyperlinks]
color = #0000ee
```
and run same example as:
./TextViewHyperLinks --text 'this is a text with an url https://gitlab.gnome.org/GNOME/gitg/issues/152 and a hidden link #152 and hidden MR !21 and documentation http://example.com/doc?param=1&chapter=4 for sure'
......@@ -2,7 +2,7 @@ public class TextViewHyperLinksApp : Gtk.Application
{
private static string? text = null;
private static string? link_color = null;
private static string? config_file = null;
private static string config_file = "custom.ini";
[CCode (array_length = false, array_null_terminated = true)]
private static string[]? custom_links = null;
......@@ -11,7 +11,7 @@ public class TextViewHyperLinksApp : Gtk.Application
{ "text", 0, 0, OptionArg.STRING, ref text, "Text to parse for urls", "TEXT" },
{ "link-color", 0, 0, OptionArg.STRING, ref link_color, "Color name or #rgb for links", "COLOR" },
{ "custom-link", 0, 0, OptionArg.STRING_ARRAY, ref custom_links, "Pattern to transform custom links", "REGEX,REPLACEMENT,COLOR" },
{ "config-file", 0, 0, OptionArg.STRING, ref config_file, "Custom path for config file", "PATH" },
{ "config-file", 0, 0, OptionArg.STRING, ref config_file, "Custom path for config file. Default \"custom.ini\"", "PATH" },
{ null }
};
......@@ -27,10 +27,10 @@ public class TextViewHyperLinksApp : Gtk.Application
}
class TextViewHyperLinksWindow: Gtk.ApplicationWindow
{
Gdk.RGBA color_link;
bool is_custom_color_link = false;
Gdk.RGBA color_hovered_link;
bool hovering_over_link = false;
Gdk.Cursor hand_cursor;
......@@ -80,6 +80,7 @@ class TextViewHyperLinksWindow: Gtk.ApplicationWindow
if (link_color != null)
{
is_custom_color_link = true;
color_link.parse(link_color);
}
......@@ -91,7 +92,7 @@ class TextViewHyperLinksWindow: Gtk.ApplicationWindow
read_ini_file(buffer, config_file);
apply_link_tags(buffer, /https?:\/\/[a-zA-Z\/&#.0-9=?]+/, null, color_link, false);
apply_link_tags(buffer, /https?:\/\/[a-zA-Z\/&#.0-9=?]+/, null, color_link, is_custom_color_link);
foreach (string custom_link in custom_links)
{
string[] params = custom_link.split(",");
......@@ -119,18 +120,30 @@ class TextViewHyperLinksWindow: Gtk.ApplicationWindow
this.add (scrolled_window);
}
private void read_ini_file(Gtk.TextBuffer buffer, string? config_file)
private void read_ini_file(Gtk.TextBuffer buffer, string config_file)
{
GLib.KeyFile file = new GLib.KeyFile();
try
{
if(config_file == null)
{
config_file="custom.ini";
}
if (file.load_from_file(config_file , GLib.KeyFileFlags.NONE))
{
if (file.has_group ("textviewhyperlinks"))
{
if (!is_custom_color_link)
{
if (file.has_key ("textviewhyperlinks", "color"))
{
string link_color = file.get_string ("textviewhyperlinks", "color");
if (link_color != null)
{
is_custom_color_link = true;
color_link.parse(link_color);
}
}
}
}
foreach (string group in file.get_groups())
{
if (group.has_prefix("custom-link"))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment