Skip to content

Commit 1dcabad

Browse files
committed
Finish bookmarks and snippets
1 parent 146a4a3 commit 1dcabad

File tree

7 files changed

+132
-6
lines changed

7 files changed

+132
-6
lines changed

bin/noteplan.dart

+13-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ void main (List<String> arguments) {
7171
final int last_update = db.get_last_update();
7272
if (_last_update_more_than(last_update)) { refresh(db); }
7373

74+
// Bookmarks
75+
if (command == 'bookmarks') {
76+
print(alf_to_results(db.search_bookmarks(query)));
77+
exit(0);
78+
}
79+
80+
// Snippets
81+
if (command == 'snippets') {
82+
print(alf_to_results(db.search_snippets(query)));
83+
exit(0);
84+
}
85+
7486
// Date parsing
7587
if (command == 'date') {
7688
try {
@@ -100,7 +112,7 @@ void main (List<String> arguments) {
100112
}
101113

102114
// Finally: Full-text search
103-
List<NoteMatch> found = db.search(query);
115+
List<NoteMatch> found = db.search_notes(query);
104116
print(alf_to_results(
105117
found
106118
.map((e) => e.to_alfred_result())

lib/bookmark.dart

+34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
import 'package:alfred_noteplan/alfred.dart';
12
import 'package:alfred_noteplan/note.dart';
3+
import 'package:alfred_noteplan/note_type.dart';
4+
import 'package:alfred_noteplan/noteplan.dart';
5+
import 'package:alfred_noteplan/strings.dart';
6+
import 'package:path/path.dart';
7+
import 'package:sqlite3/sqlite3.dart';
8+
29
class Bookmark {
310
final Note note;
411
final String url;
@@ -11,4 +18,31 @@ class Bookmark {
1118
this.url,
1219
{this.description}
1320
);
21+
22+
/// alfred result formatter working on raw SQL data
23+
static Map<String, dynamic> to_alfred_result(Row result) => alf_item(
24+
result['title'],
25+
'${basenameWithoutExtension(result['filename'])} ✱ ${result['url']}',
26+
arg: result['url'],
27+
variables: {'action': 'open'},
28+
mods: {
29+
'cmd': {
30+
'valid': true,
31+
'arg': NoteType.create_from_string(result['note_type']) == NoteType.note
32+
? Noteplan.openNoteUrl(result['filename'])
33+
: Noteplan.openCalendarUrl(basenameWithoutExtension(result['filename'])),
34+
'subtitle': str_bookmark_open_note
35+
},
36+
'shift': {
37+
'valid': true,
38+
'subtitle': str_bookmark_copy,
39+
'variables': {'action': 'copy-to-clipboard'}
40+
},
41+
'cmd+shift': {
42+
'valid': true,
43+
'subtitle': str_bookmark_copy_paste,
44+
'variables': {'action': 'copy-paste'}
45+
},
46+
}
47+
);
1448
}

lib/dbs.dart

+42-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:alfred_noteplan/folder.dart';
44
import 'package:alfred_noteplan/note.dart';
55
import 'package:alfred_noteplan/note_match.dart';
66
import 'package:alfred_noteplan/snippet.dart';
7+
import 'package:alfred_noteplan/strings.dart';
78
import 'package:path/path.dart';
89
import 'package:sqlite3/sqlite3.dart';
910

@@ -148,10 +149,8 @@ class Dbs {
148149
''').execute([timestamp ?? DateTime.now().millisecondsSinceEpoch]);
149150
}
150151

151-
}
152-
153-
List<NoteMatch> search(String query) {
154-
final String preparedQuery = _query_to_fts_query(query);
152+
List<NoteMatch> search_notes(String query) {
153+
final String preparedQuery = query.toFtsQuery();
155154
final ResultSet results = _db.select('''
156155
SELECT
157156
filename,
@@ -169,6 +168,45 @@ class Dbs {
169168
return results.map((Row row) => NoteMatch(row)).toList(growable: false);
170169
}
171170

171+
List<Map<String, dynamic>> search_bookmarks(String query) {
172+
final String preparedQuery = query.toFtsQuery();
173+
final ResultSet results = _db.select('''
174+
SELECT
175+
filename,
176+
note_type,
177+
title,
178+
url
179+
FROM
180+
main.bookmarks('${preparedQuery}')
181+
ORDER BY
182+
rank
183+
LIMIT
184+
18
185+
''');
186+
187+
return results.map((Row result) => Bookmark.to_alfred_result(result)).toList(growable: false);
188+
}
189+
190+
List<Map<String, dynamic>> search_snippets(String query) {
191+
final String preparedQuery = query.toFtsQuery();
192+
final ResultSet results = _db.select('''
193+
SELECT
194+
filename,
195+
note_type,
196+
language,
197+
title,
198+
content
199+
FROM
200+
main.snippets('${preparedQuery}')
201+
ORDER BY
202+
rank
203+
LIMIT
204+
18
205+
''');
206+
207+
return results.map((Row result) => Snippet.to_alfred_result(result)).toList(growable: false);
208+
}
209+
172210
ResultSet cache_get_updated({int since = 0}) {
173211
final String ignore_condition = Config.ignore.isEmpty
174212
? ''

lib/snippet.dart

+30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
import 'package:alfred_noteplan/alfred.dart';
12
import 'package:alfred_noteplan/note.dart';
3+
import 'package:alfred_noteplan/note_type.dart';
4+
import 'package:alfred_noteplan/noteplan.dart';
5+
import 'package:alfred_noteplan/strings.dart';
6+
import 'package:path/path.dart';
7+
import 'package:sqlite3/sqlite3.dart';
28

39
class Snippet {
410
final Note note;
@@ -12,4 +18,28 @@ class Snippet {
1218
this.title,
1319
this.content,
1420
);
21+
22+
/// alfred result formatter working on raw SQL data
23+
static Map<String, dynamic> to_alfred_result(Row result) => alf_item(
24+
result['title'],
25+
'${result['language']} ✱ ${basenameWithoutExtension(result['filename'])}',
26+
arg: result['content'],
27+
variables: {'action': 'copy-paste'},
28+
mods: {
29+
'cmd': {
30+
'valid': true,
31+
'arg': NoteType.create_from_string(result['note_type']) == NoteType.note
32+
? Noteplan.openNoteUrl(result['filename'])
33+
: Noteplan.openCalendarUrl(basenameWithoutExtension(result['filename'])),
34+
'subtitle': str_snippet_open_note,
35+
'variables': {'action': 'open'}
36+
},
37+
'shift': {
38+
'valid': true,
39+
'subtitle': str_bookmark_copy,
40+
'variables': {'action': 'copy-to-clipboard'}
41+
},
42+
}
43+
);
44+
1545
}

lib/strings.dart

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ const str_fts_result_arg_cmd_subtitle = 'Open the note in a new Noteplan window'
2222
const str_create_result_subtitle = 'Create a new note ✱ You\'ll be asked for location in the next step';
2323
str_create_folder_result_subtitle(String f, String t) => 'Create note "${t}" in folder "${f}"';
2424

25+
const str_bookmark_open_note = 'Open the note the bookmark was found in';
26+
const str_bookmark_copy = 'Copy the URL to clipboard';
27+
const str_bookmark_copy_paste = 'Copy the URL to clipboard and paste to frontmost app';
28+
29+
const str_snippet_open_note = 'Open the note the snippet was found in';
30+
const str_snippet_copy = 'Copy the snippet to clipboard without pasting';
31+
2532
extension StringExtensions on String {
2633
// source: https://stackoverflow.com/a/29629114/240239
2734
String toCapitalized() => length > 0 ? '${this[0].toUpperCase()}${substring(1).toLowerCase()}':'';

test/fake-run.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
cd workflow || exit 1 # imitate CWD to workflow
55

66
# shellcheck disable=SC1091
7-
source ../test/workflow-search.sh
7+
source ../test/workflow-search-notes.sh

test/test.sh

+5
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ echo
3737
echo "SEARCH - adam kiss (outputs: 19 [18 + create])"
3838
user_np_root=$user_np_root user_new_note_template=$user_new_note_template \
3939
./workflow/noteplan-arm64 search 'adam kiss' | jq '.items | map(.title + " - " + .subtitle)'
40+
echo
41+
42+
echo "Bookmark search - kirby"
43+
user_np_root=$user_np_root user_new_note_template=$user_new_note_template \
44+
./workflow/noteplan-arm64 bookmarks 'kirby' | jq '.items | map(.title + " - " + .subtitle)'
4045
echo

0 commit comments

Comments
 (0)