Skip to content

Commit

Permalink
Open data file for edit
Browse files Browse the repository at this point in the history
Renamed data file to end with .txt so that it will open in a default text editor.  This is a short cut for being able to maintain the link information.

Refactor 'repo' to 'link' for clarification.
  • Loading branch information
skeletonkey committed Oct 24, 2022
1 parent d4753a6 commit 76642fc
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 20 deletions.
File renamed without changes.
24 changes: 12 additions & 12 deletions LinkOpener.pm
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package LinkOpener;

my $repo_data = '.linkopener.dat';
my $link_data = '.linkopener.txt';
my $sep = '|';

sub get_repos {
my %repos = ();
open(my $fh, '<', $repo_data) || die("Unable to open file ($repo_data) for read: $!\n");
sub get_links {
my %links = ();
open(my $fh, '<', $link_data) || die("Unable to open file ($link_data) for read: $!\n");
while (my $line = <$fh>) {
chomp($line);
my ($link, $alias, $count) = split(/\|/, $line);
$repos{$link} = {
$links{$link} = {
link => $link,
name => get_name($link, $alias),
count => $count || 0,
}
}
close($fh);

return %repos;
return %links;
}

sub save_data {
my %data = @_;
open(my $fh, '>', $repo_data) || die("Unable to open file ($repo_data) for write: $!\n");
open(my $fh, '>', $link_data) || die("Unable to open file ($link_data) for write: $!\n");
foreach my $name (keys %data) {
print $fh join($sep, $data{$name}{link}, $data{$name}{name}, $data{$name}{count}) . "\n";
}
Expand All @@ -32,7 +32,7 @@ sub save_data {
sub add_url {
my ($url, $name) = @_;
$name ||= '';
open(my $fh, '>>', $repo_data) || die("Unable to open file ($repo_data) for append: $!\n");
open(my $fh, '>>', $link_data) || die("Unable to open file ($link_data) for append: $!\n");
print $fh join($sep, $url, $name) . "\n";
close($fh);
}
Expand All @@ -50,12 +50,12 @@ sub update_link_count {
my $link = shift;

my $name = get_name($link);
my %repos = get_repos();
my %links = get_links();

return unless exists $repos{$link};
$repos{$link}{count}++;
return unless exists $links{$link};
$links{$link}{count}++;

save_data(%repos);
save_data(%links);
}

1;
63 changes: 63 additions & 0 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@
<false/>
</dict>
</array>
<key>5ADBA718-2EF4-459C-AE9F-3FF7E9C686E7</key>
<array>
<dict>
<key>destinationuid</key>
<string>7F516328-55B7-4744-AFB7-52B16646032F</string>
<key>modifiers</key>
<integer>0</integer>
<key>modifiersubtext</key>
<string></string>
<key>vitoclose</key>
<false/>
</dict>
</array>
<key>802A07C9-DC0F-4694-8880-60C52A81DC77</key>
<array>
<dict>
Expand Down Expand Up @@ -242,6 +255,42 @@ perl linkopener.pl "$query" 2&gt;&amp;1</string>
<key>version</key>
<integer>2</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>openwith</key>
<string></string>
<key>sourcefile</key>
<string>.linkopener.txt</string>
</dict>
<key>type</key>
<string>alfred.workflow.action.openfile</string>
<key>uid</key>
<string>7F516328-55B7-4744-AFB7-52B16646032F</string>
<key>version</key>
<integer>3</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>argumenttype</key>
<integer>2</integer>
<key>keyword</key>
<string>link edit</string>
<key>subtext</key>
<string></string>
<key>text</key>
<string>Open link data file</string>
<key>withspace</key>
<false/>
</dict>
<key>type</key>
<string>alfred.workflow.input.keyword</string>
<key>uid</key>
<string>5ADBA718-2EF4-459C-AE9F-3FF7E9C686E7</string>
<key>version</key>
<integer>1</integer>
</dict>
</array>
<key>readme</key>
<string></string>
Expand All @@ -263,6 +312,13 @@ perl linkopener.pl "$query" 2&gt;&amp;1</string>
<key>ypos</key>
<real>180</real>
</dict>
<key>5ADBA718-2EF4-459C-AE9F-3FF7E9C686E7</key>
<dict>
<key>xpos</key>
<real>230</real>
<key>ypos</key>
<real>485</real>
</dict>
<key>7A6DD6BD-5F0B-4131-8D9D-38AA47D83ECB</key>
<dict>
<key>xpos</key>
Expand All @@ -277,6 +333,13 @@ perl linkopener.pl "$query" 2&gt;&amp;1</string>
<key>ypos</key>
<real>175</real>
</dict>
<key>7F516328-55B7-4744-AFB7-52B16646032F</key>
<dict>
<key>xpos</key>
<real>455</real>
<key>ypos</key>
<real>485</real>
</dict>
<key>802A07C9-DC0F-4694-8880-60C52A81DC77</key>
<dict>
<key>xpos</key>
Expand Down
16 changes: 8 additions & 8 deletions linkopener.pl
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@
# https://www.alfredapp.com/help/workflows/inputs/script-filter/json/

my $find = $ARGV[0];
my %repos;
my %links;

find_link($find);

sub find_link {
my $tmpl = '"title":"%s","subtitle":"%s","arg":"%s"';

%repos = LinkOpener::get_repos();
%links = LinkOpener::get_links();

my @keys = sort( sort_repos grep {$repos{$_}{name} =~ /$find/i} keys %repos);
my @keys = sort( sort_links grep {$links{$_}{name} =~ /$find/i} keys %links);
if (@keys) {
print '{"items":[{' . join('},{', map({ sprintf($tmpl, $repos{$_}{name},$repos{$_}{link},$repos{$_}{link}) } @keys)) . '}]}';
print '{"items":[{' . join('},{', map({ sprintf($tmpl, $links{$_}{name},$links{$_}{link},$links{$_}{link}) } @keys)) . '}]}';
}
else {
print '{"items":[{"title":"No links found","subtitle":"Please try a different search"}]}';
}
}

sub sort_repos {
if ($repos{$a}{count} == $repos{$b}{count}) {
return $repos{$a}{name} cmp $repos{$b}{name};
sub sort_links {
if ($links{$a}{count} == $links{$b}{count}) {
return $links{$a}{name} cmp $links{$b}{name};
}
else {
return $repos{$b}{count} <=> $repos{$a}{count};
return $links{$b}{count} <=> $links{$a}{count};
}
}

0 comments on commit 76642fc

Please sign in to comment.