Scoop API

scoop:scoop.readline:0.1.0

src/history/file.scoop

Selected production source · src/history/file.scoop

1package scoop.readline.history
3import scoop.readline.readlineConfig
5import scoop.readline.Config
6import scoop.platform.fs.*
7import scoop.platform.io.*
9/** File history compatible with rustyline V1/V2 and coordinated by advisory file locks. */
10public class FileHistory public constructor(config: Config = readlineConfig()): PersistentHistory {
11 private val memory: MemoryHistory = MemoryHistory(config)
12 private var pending: Long = 0L
14 public override val size: Long get() = memory.size
15 public override fun get(index: Long, direction: SearchDirection): SearchResult? =
16 memory.get(index, direction)
17 public override fun add(line: String): Boolean {
18 if (!memory.add(line))
19 return false
20 if (pending < size)
21 pending += 1L
22 return true
23 }
24 public override fun clear() {
25 memory.clear();
26 pending = 0L
27 }
28 public override fun setMaxLength(length: Long) {
29 memory.setMaxLength(length)
30 if (pending > size)
31 pending = size
32 }
33 public override fun setIgnoreDuplicates(ignore: Boolean) {
34 memory.setIgnoreDuplicates(ignore)
35 }
36 public override fun setIgnoreSpace(ignore: Boolean) {
37 memory.setIgnoreSpace(ignore)
38 }
39 public override fun search(
40 term: String,
41 start: Long,
42 direction: SearchDirection
43 ): SearchResult? = memory.search(term, start, direction)
44 public override fun startsWith(
45 term: String,
46 start: Long,
47 direction: SearchDirection
48 ): SearchResult? = memory.startsWith(term, start, direction)
50 /** Appends loaded entries to memory; malformed UTF-8 leaves the previous history intact. */
51 public override fun load(path: Path) {
52 val entries =
53 File.open(path).use {
54 file -> file.withLock(shared = true) { decodeHistory(file.readText()) }
55 }
56 for (entry in entries)
57 memory.add(entry)
58 pending = 0L
59 }
61 /** Replaces the file with all current entries, including an empty history. */
62 public override fun save(path: Path) {
63 val text = encodeHistory(memory)
64 File.open(path, OpenOptions(write = true, create = true, mode = 0x180)).use {
65 file -> file.withLock { replaceHistoryFile(file, text) }
66 }
67 pending = 0L
68 }
70 /** Merges only unsaved entries with the current file under an exclusive lock. */
71 public override fun append(path: Path) {
72 if (pending == 0L)
73 return
74 File.open(path, OpenOptions(write = true, create = true, mode = 0x180)).use {
75 file -> file.withLock {
76 val merged = memory.emptyCopy()
77 for (entry in decodeHistory(file.readText()))
78 merged.add(entry)
79 var index = size - pending
80 while (index < size) {
81 merged.add(memory.get(index)!!.entry)
82 index += 1L
83 }
84 replaceHistoryFile(file, encodeHistory(merged))
85 }
86 }
87 pending = 0L
88 }
90 /** Returns an independent oldest-to-newest snapshot. */
91 public fun toArray(): Array<String> = memory.toArray()
94private fun replaceHistoryFile(file: File, text: String) {
95 file.seek(0L)
96 file.writeText(text)
97 file.truncate(text.byteLength)
98 file.flush()