-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalfred_workflow_caching_example.dart
65 lines (55 loc) · 1.94 KB
/
alfred_workflow_caching_example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import 'dart:io' show exitCode;
import 'package:alfred_workflow/alfred_workflow.dart';
import 'package:args/args.dart';
import 'package:cli_script/cli_script.dart';
void main(List<String> arguments) {
wrapMain(() async {
final workflow = AlfredWorkflow();
try {
exitCode = 0;
final ArgParser parser = ArgParser()
..addOption('query', abbr: 'q', defaultsTo: '');
final ArgResults args = parser.parse(arguments);
final String query = args['query'].replaceAll(RegExp(r'\s+'), ' ').trim();
if (query.isEmpty) {
workflow.addItem(
const AlfredItem(
title: 'Search for some particular stuff ...',
icon: AlfredItemIcon(path: 'icon.png'),
),
);
} else {
/// Define a cacheKey. In this case you can just use the query.
workflow.cacheKey = query;
/// Check if anything using that cacheKey is already in the cache.
/// This will automatically add the cached items to the Alfred feedback.
final AlfredItems? cachedItems = await workflow.getItems();
/// If noting was cached simply do as before
if (cachedItems == null) {
final Uri url = Uri.https('www.google.com', '/search', {'q': query});
/// This will now automatically add the AlfredItem to the cache.
workflow.addItem(
AlfredItem(
title: 'Sorry I can\'t help you with that query.',
subtitle: 'Shall I try and search Google?',
arg: url.toString(),
text: AlfredItemText(
copy: url.toString(),
),
quickLookUrl: url.toString(),
icon: AlfredItemIcon(path: 'google.png'),
valid: true,
),
);
}
}
} catch (err) {
exitCode = 1;
workflow.addItem(
AlfredItem(title: err.toString()),
);
} finally {
workflow.run();
}
});
}