tldr alfred workflow

tldr

Too Long; Didn’t Read

偶然间,看到一个比较好用的工具.可以看做是简化版本的man.这个是官网的简介:

A collection of simplified and community-driven man pages.

举个例子,看看tar这个命令的对比

这个是man tar的截图

再来看看tldr的版本,显然要比man简单明了的多,这么好的功能当然要和Alfred这个神器结合一下.

Alfred Workflow

基本的思路就是把tldr的git仓库clone到本地,然后写个接口来嫁接Alfred的数据格式.直接上代码:

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package name.chengchao.springrun.service.alfredplugin;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import name.chengchao.springrun.model.AlfredLine;

public class TLDRService {

private final static String PATH = "/root/tldr/pages/common";
private static List<String> commandList = new ArrayList<String>();
private static ConcurrentHashMap<String, List<AlfredLine>> cache = new ConcurrentHashMap<String, List<AlfredLine>>();

static {
init();
}

@SuppressWarnings("unchecked")
public static void init() {
commandList.clear();
List<File> files = (List<File>) FileUtils.listFiles(new File(PATH), new String[] { "md" }, false);
for (File file : files) {
commandList.add(StringUtils.substringBefore(file.getName(), "."));
}
}

@SuppressWarnings("unchecked")
public static List<AlfredLine> search(String action, String keyword) throws Exception {
keyword = StringUtils.trim(keyword);
List<AlfredLine> list = new ArrayList<>();
int count = 0;
boolean bingo = false;
for (String command : commandList) {
if (command.equalsIgnoreCase(keyword)) {
bingo = true;
break;
}
if (command.startsWith(keyword)) {
count++;
AlfredLine alfredLine = new AlfredLine();
alfredLine.setTitle(command);
alfredLine.setSubtitle("");
alfredLine.setArg("");
alfredLine.setAutocomplete(command);
list.add(alfredLine);
if (count >= 10) {
break;
}
}
}

// 命中一个具体命令的逻辑
if (bingo) {
if (cache.get(keyword) != null) {
return cache.get(keyword);
}

list.clear();
File file = new File(PATH + "/" + keyword + ".md");
if (file.exists()) {
List<String> lines = FileUtils.readLines(file);
AlfredLine alfredLine = null;
for (String line : lines) {
if (line.startsWith("-")) {
if (alfredLine != null) {
list.add(alfredLine);
}
alfredLine = new AlfredLine();
alfredLine.setSubtitle(line.substring(1, line.length() - 1));
} else if (line.startsWith("`") && alfredLine != null) {
alfredLine.setTitle(line.substring(1, line.length() - 1));
alfredLine.setArg(line.substring(1, line.length() - 1));
}

}
if (alfredLine != null) {
list.add(alfredLine);
}

cache.put(keyword, list);
}
}
return list;
}
}

看看效果