/
History.java
163 lines (132 loc) · 3.53 KB
/
History.java
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Copyright (c) 2002-2018, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* http://www.opensource.org/licenses/bsd-license.php
*/
package org.jline.reader;
import java.io.IOException;
import java.time.Instant;
import java.util.Iterator;
import java.util.ListIterator;
/**
* Console history.
*
* @author <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
* @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
* @since 2.3
*/
public interface History extends Iterable<History.Entry>
{
/**
* Initialize the history for the given reader.
* @param reader the reader to attach to
*/
void attach(LineReader reader);
/**
* Load history.
* @throws IOException if a problem occurs
*/
void load() throws IOException;
/**
* Save history.
* @throws IOException if a problem occurs
*/
void save() throws IOException;
/**
* Purge history.
* @throws IOException if a problem occurs
*/
void purge() throws IOException;
int size();
default boolean isEmpty() {
return size() == 0;
}
int index();
int first();
int last();
String get(int index);
default void add(String line) {
add(Instant.now(), line);
}
void add(Instant time, String line);
//
// Entries
//
interface Entry
{
int index();
Instant time();
String line();
}
ListIterator<Entry> iterator(int index);
default ListIterator<Entry> iterator() {
return iterator(first());
}
default Iterator<Entry> reverseIterator() {
return reverseIterator(last());
}
default Iterator<Entry> reverseIterator(int index) {
return new Iterator<Entry>() {
private final ListIterator<Entry> it = iterator(index + 1);
@Override
public boolean hasNext() {
return it.hasPrevious();
}
@Override
public Entry next() {
return it.previous();
}
};
}
//
// Navigation
//
/**
* Return the content of the current buffer.
*
* @return the content of the current buffer
*/
String current();
/**
* Move the pointer to the previous element in the buffer.
*
* @return true if we successfully went to the previous element
*/
boolean previous();
/**
* Move the pointer to the next element in the buffer.
*
* @return true if we successfully went to the next element
*/
boolean next();
/**
* Moves the history index to the first entry.
*
* @return Return false if there are no iterator in the history or if the
* history is already at the beginning.
*/
boolean moveToFirst();
/**
* This moves the history to the last entry. This entry is one position
* before the moveToEnd() position.
*
* @return Returns false if there were no history iterator or the history
* index was already at the last entry.
*/
boolean moveToLast();
/**
* Move to the specified index in the history
*
* @param index The index to move to.
* @return Returns true if the index was moved.
*/
boolean moveTo(int index);
/**
* Move to the end of the history buffer. This will be a blank entry, after
* all of the other iterator.
*/
void moveToEnd();
}