/
ResultStore.scala
143 lines (121 loc) · 4.2 KB
/
ResultStore.scala
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package bibimbap
package modules
import akka.actor._
import data._
class ResultStore(val repl: ActorRef, val console: ActorRef, val settings: Settings) extends Module {
val name = "results"
private var results = List[SearchResult]()
override def receive: Receive = {
case Command1("list") | Command1("show") =>
displayResults()
sender ! CommandSuccess
case Command2("import", ind) =>
getResults(ind) match {
case Some(rs) =>
for (r <- rs) {
doImport(r)
}
case None =>
console ! Error("Invalid search result")
}
sender ! CommandSuccess
case Command2("show", ind) =>
getResults(ind) match {
case Some(rs) =>
for (r <- rs) {
doShow(r)
}
case None =>
console ! Error("Invalid search result")
}
sender ! CommandSuccess
case SearchResults(newResults) =>
results = newResults
displayResults()
sender ! CommandSuccess
case ReplaceResults(ind, newResults) =>
getResults(ind) match {
case Some(rs) =>
results = results.map((rs zip newResults).toMap.orElse{ case x => x })
case None =>
console ! Error("Invalid search result")
}
sender ! CommandSuccess
case GetResults(index) =>
sender ! SearchResults(getResults(index).getOrElse(Nil))
case ShowResults =>
displayResults()
sender ! CommandSuccess
case x =>
super.receive(x)
}
private val Range = """(\d+)-(\d+)""".r
private val Single = """(\d+)""".r
private def getResults(index: String): Option[List[SearchResult]] = {
index match {
case "*" =>
Some(results)
case Range(lower, upper) =>
val l = lower.toInt
val u = upper.toInt
if (l <= u && l >= 0 && u < results.size) {
Some(results.slice(l, u + 1))
} else {
None
}
case Single(index) =>
val i = index.toInt
if (i < results.size && i >= 0) {
Some(List(results(i)))
} else {
None
}
}
}
private def doImport(res: SearchResult) {
import java.io.{FileWriter,File}
val fn = settings("general", "bib.filename")
val fw = new FileWriter(new File(fn), true)
fw.write(res.entry.toString)
fw.write("\n\n")
fw.close
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection
try {
val clipboard = Toolkit.getDefaultToolkit.getSystemClipboard
val stringSel = new StringSelection(res.entry.getKey)
clipboard.setContents(stringSel, stringSel)
} catch {
case e: java.awt.HeadlessException =>
console ! Warning("Could not store in clipboard: "+e.getMessage.trim)
}
// Inform search module that we imported this
modules("search") ! ImportedResult(res)
console ! Success("Imported: \\cite{"+res.entry.getKey+"}")
}
private def doShow(res: SearchResult) {
console ! Out(res.entry.toString)
}
private def displayResults() {
var i = 0
for (res <- results) {
val spc = if (i < 10) "" else " "
val colSourceCache = if (res.sources.contains("cache")) Console.YELLOW+"c"+Console.RESET else " "
val colSourceDBLP = if (res.sources.contains("dblp")) Console.YELLOW+"w"+Console.RESET else " "
val colSourceImported = if (res.sources.contains("managed")) Console.GREEN+"I"+Console.RESET else " "
val colSourceLoad = if (res.sources.contains("loaded")) Console.GREEN+"L"+Console.RESET else " "
val colInvalid = if (!res.entry.isValid) Console.RED+"!"+Console.RESET else " "
val extraCols = colSourceCache+colSourceDBLP+colSourceImported+colSourceLoad+colInvalid
console ! Info("["+i+spc+" "+extraCols+"] "+res.entry.inlineString)
i += 1
}
if (results.isEmpty) {
console ! Info("No match")
}
}
val helpItems = Map(
"list" -> HelpEntry("list", "Displays the current list of results."),
"import" -> HelpEntry("import <result>", "Imports the <result>th item from the last search results into managed.bib"),
"show" -> HelpEntry("show <result>", "Displays the bib entry for the <results>th search result.")
)
}