| 1 | =pod | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | App::MarkFiles - some utility functions for marking and operating on files | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | # This module: | ||
| 10 | use App::MarkFiles qw(get_dbh each_path add remove); | ||
| 11 | |||
| 12 | my $dbh = get_dbh(); # db handle for marks.db | ||
| 13 | |||
| 14 | add('/foo/bar', '/foo/baz'); | ||
| 15 | |||
| 16 | remove('/foo/baz'); | ||
| 17 | |||
| 18 | each_path(sub { | ||
| 19 | my ($path) = @_; | ||
| 20 | print "$path\n"; | ||
| 21 | }); | ||
| 22 | |||
| 23 | # mark commands: | ||
| 24 | $ mark add foo.txt | ||
| 25 | $ cd ~/somedir | ||
| 26 | $ mark mv | ||
| 27 | |||
| 28 | =head1 INSTALLING | ||
| 29 | |||
| 30 | $ perl Build.PL | ||
| 31 | $ ./Build | ||
| 32 | $ ./Build install | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | The mark utilities store a list of marked file paths in marks.db in the user's | ||
| 37 | home directory. Once marked, files can be copied, moved, listed, or passed as | ||
| 38 | parameters to arbitrary shell commands. | ||
| 39 | |||
| 40 | This originated as a simple tool for collecting files from one or more | ||
| 41 | directories and moving or copying them to another. A basic usage pattern | ||
| 42 | looks something like this: | ||
| 43 | |||
| 44 | $ cd ~/screenshots | ||
| 45 | $ mark add foo.png | ||
| 46 | $ cd ~/blog/files/screenshots | ||
| 47 | $ mark mv | ||
| 48 | Moved: /home/brennen/screenshots/foo.png | ||
| 49 | |||
| 50 | This is more steps than a simple invocation of mv(1), but its utility becomes | ||
| 51 | more apparent when it's combined with aliases for quickly navigating | ||
| 52 | directories or invoked from other programs like editors and file managers. | ||
| 53 | |||
| 54 | See C<bin/mark> in this distribution (or, when installed, the mark(1) man page) | ||
| 55 | for details on the commands. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | package App::MarkFiles; | ||
| 60 | |||
| 61 | use strict; | ||
| 62 | use warnings; | ||
| 63 | |||
| 64 | use base qw(Exporter); | ||
| 65 | our @EXPORT_OK = qw(get_dbh each_path add remove); | ||
| 66 | |||
| 67 | our ($VERSION) = '0.0.1'; | ||
| 68 | |||
| 69 | use DBI; | ||
| 70 | use File::HomeDir; | ||
| 71 | use File::Spec; | ||
| 72 | |||
| 73 | =head1 SUBROUTINES | ||
| 74 | |||
| 75 | =over | ||
| 76 | |||
| 77 | =item get_dbh() | ||
| 78 | |||
| 79 | Get database handle for default marks database, stored in F<~/marks.db>. | ||
| 80 | |||
| 81 | Creates a new marks.db with the correct schema if one doesn't already exist. | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub get_dbh { | ||
| 86 | my $dbfile = File::Spec->catfile(File::HomeDir->my_home, 'marks.db'); | ||
| 87 | |||
| 88 | my $init_new = 0; | ||
| 89 | $init_new = 1 unless -f $dbfile; | ||
| 90 | |||
| 91 | my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", ""); | ||
| 92 | |||
| 93 | create_mark_db($dbh) if $init_new; | ||
| 94 | |||
| 95 | return $dbh; | ||
| 96 | } | ||
| 97 | |||
| 98 | =item create_mark_db($dbh) | ||
| 99 | |||
| 100 | Create a new marks table. | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub create_mark_db { | ||
| 105 | my ($dbh) = @_; | ||
| 106 | |||
| 107 | $dbh->do(<<'SQL' | ||
| 108 | CREATE TABLE marks ( | ||
| 109 | id integer primary key, | ||
| 110 | path text, | ||
| 111 | datetime text | ||
| 112 | ); | ||
| 113 | SQL | ||
| 114 | ); | ||
| 115 | } | ||
| 116 | |||
| 117 | =item add(@paths) | ||
| 118 | |||
| 119 | Add a mark to one or more paths. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub add { | ||
| 124 | my (@paths) = @_; | ||
| 125 | |||
| 126 | # Filter out any paths that have already been marked: | ||
| 127 | my %pathmap = map { $_ => 1 } @paths; | ||
| 128 | each_path(sub { | ||
| 129 | my ($existing_path) = @_; | ||
| 130 | if ($pathmap{ $existing_path }) { | ||
| 131 | delete $pathmap{ $existing_path }; | ||
| 132 | } | ||
| 133 | }); | ||
| 134 | |||
| 135 | my $sth = get_dbh()->prepare(q{ | ||
| 136 | INSERT INTO marks (path, datetime) VALUES (?, datetime('now')) | ||
| 137 | }); | ||
| 138 | |||
| 139 | foreach my $path (keys %pathmap) { | ||
| 140 | $sth->execute($path); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | =item remove(@paths) | ||
| 145 | |||
| 146 | Remove all given paths from the mark list. | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub remove { | ||
| 151 | my (@paths) = @_; | ||
| 152 | |||
| 153 | my $sth = get_dbh()->prepare(q{ | ||
| 154 | DELETE FROM marks WHERE PATH = ?; | ||
| 155 | }); | ||
| 156 | |||
| 157 | foreach my $path (@paths) { | ||
| 158 | $sth->execute($path); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | =item each_path($func) | ||
| 163 | |||
| 164 | Run an anonymous function against each item in the mark list. | ||
| 165 | |||
| 166 | Expects a sub which takes a path string. | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | sub each_path { | ||
| 171 | my ($func) = @_; | ||
| 172 | |||
| 173 | my $sth = get_dbh()->prepare(q{ | ||
| 174 | SELECT DISTINCT(path) as path FROM marks ORDER BY datetime; | ||
| 175 | }); | ||
| 176 | |||
| 177 | $sth->execute(); | ||
| 178 | |||
| 179 | while (my $data = $sth->fetchrow_hashref()) { | ||
| 180 | $func->($data->{path}); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | =back | ||
| 185 | |||
| 186 | =head1 AUTHOR | ||
| 187 | |||
| 188 | Copyright 2018 Brennen Bearnes | ||
| 189 | |||
| 190 | mark is free software; you can redistribute it and/or modify | ||
| 191 | it under the terms of the GNU General Public License as published by | ||
| 192 | the Free Software Foundation; either version 2 of the License, or | ||
| 193 | (at your option) any later version. | ||
| 194 | |||
| 195 | This program is distributed in the hope that it will be useful, | ||
| 196 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 197 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 198 | GNU General Public License for more details. | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | 1; |