forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandHistory.m
More file actions
233 lines (201 loc) · 8.07 KB
/
Copy pathCommandHistory.m
File metadata and controls
233 lines (201 loc) · 8.07 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// CommandHistory.m
// iTerm
//
// Created by George Nachman on 1/6/14.
//
//
#import "CommandHistory.h"
#import "CommandHistoryEntry.h"
#import "CommandUse.h"
#import "PreferencePanel.h"
#import "VT100RemoteHost.h"
NSString *const kCommandHistoryDidChangeNotificationName = @"kCommandHistoryDidChangeNotificationName";
NSString *const kCommandHistoryHasEverBeenUsed = @"kCommandHistoryHasEverBeenUsed";
static const int kMaxResults = 200;
// Top level serialization keys
static NSString *const kHostname = @"hostname";
static NSString *const kCommands = @"commands";
static const NSTimeInterval kMaxTimeToRememberCommands = 60 * 60 * 24 * 90;
static const int kMaxCommandsToSavePerHost = 200;
@interface CommandHistory ()
@property(nonatomic, retain) NSMutableDictionary *hosts;
@end
@implementation CommandHistory {
NSString *_path;
}
+ (instancetype)sharedInstance {
static id instance;
static dispatch_once_t once;
dispatch_once(&once, ^{
instance = [[self alloc] init];
});
return instance;
}
- (id)init {
self = [super init];
if (self) {
_hosts = [[NSMutableDictionary alloc] init];
_path = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,
NSUserDomainMask,
YES) lastObject];
NSString *appname = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleNameKey];
_path = [_path stringByAppendingPathComponent:appname];
[[NSFileManager defaultManager] createDirectoryAtPath:_path
withIntermediateDirectories:YES
attributes:nil
error:NULL];
_path = [[_path stringByAppendingPathComponent:@"commandhistory.plist"] copy];
[self loadCommandHistory];
}
return self;
}
- (void)dealloc {
[_hosts release];
[_path release];
[super dealloc];
}
#pragma mark - APIs
+ (void)showInformationalMessage {
if (NSRunInformationalAlertPanel(@"About Command History",
@"To use command history your shell must be properly configured.",
@"Learn More…",
@"Ok",
nil) == NSAlertDefaultReturn) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://iterm2.com/shell_integration.html"]];
}
}
- (BOOL)commandHistoryHasEverBeenUsed {
return (_hosts.count > 0 ||
[[NSUserDefaults standardUserDefaults] boolForKey:kCommandHistoryHasEverBeenUsed]);
}
- (void)addCommand:(NSString *)command
onHost:(VT100RemoteHost *)host
inDirectory:(NSString *)directory
withMark:(VT100ScreenMark *)mark {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kCommandHistoryHasEverBeenUsed];
NSMutableArray *commands = [self commandsForHost:host];
CommandHistoryEntry *theEntry = nil;
for (CommandHistoryEntry *entry in commands) {
if ([entry.command isEqualToString:command]) {
theEntry = entry;
break;
}
}
if (!theEntry) {
theEntry = [CommandHistoryEntry commandHistoryEntry];
theEntry.command = command;
[commands addObject:theEntry];
}
theEntry.uses = theEntry.uses + 1;
theEntry.lastUsed = [NSDate timeIntervalSinceReferenceDate];
CommandUse *commandUse = [[[CommandUse alloc] init] autorelease];
commandUse.time = theEntry.lastUsed;
commandUse.mark = mark;
commandUse.directory = directory;
[theEntry.useTimes addObject:commandUse];
if ([[PreferencePanel sharedInstance] savePasteHistory]) {
[NSKeyedArchiver archiveRootObject:[self dictionaryForEntries] toFile:_path];
}
[[NSNotificationCenter defaultCenter] postNotificationName:kCommandHistoryDidChangeNotificationName
object:nil];
}
- (BOOL)haveCommandsForHost:(VT100RemoteHost *)host {
return [[self commandsForHost:host] count] > 0;
}
- (NSArray *)autocompleteSuggestionsWithPartialCommand:(NSString *)partialCommand
onHost:(VT100RemoteHost *)host {
BOOL emptyPartialCommand = (partialCommand.length == 0);
NSMutableArray *result = [NSMutableArray array];
for (CommandHistoryEntry *entry in [self commandsForHost:host]) {
NSRange match;
if (!emptyPartialCommand) {
match = [entry.command rangeOfString:partialCommand];
}
if (emptyPartialCommand || match.location == 0) {
// The FinalTerm algorithm doesn't require |partialCommand| to be a prefix of the
// history entry, but based on how our autocomplete works, it makes sense to only
// accept prefixes. Their scoring algorithm is implemented in case this should change.
entry.matchLocation = match.location;
[result addObject:entry];
}
}
// TODO: Cache this.
NSArray *sortedEntries = [result sortedArrayUsingSelector:@selector(compare:)];
return [sortedEntries subarrayWithRange:NSMakeRange(0, MIN(kMaxResults, sortedEntries.count))];
}
- (NSArray *)entryArrayByExpandingAllUsesInEntryArray:(NSArray *)array {
NSMutableArray *result = [NSMutableArray array];
for (CommandHistoryEntry *entry in array) {
for (CommandUse *commandUse in entry.useTimes) {
CommandHistoryEntry *singleUseEntry = [[entry copy] autorelease];
[singleUseEntry.useTimes removeAllObjects];
[singleUseEntry.useTimes addObject:commandUse];
singleUseEntry.lastUsed = commandUse.time;
[result addObject:singleUseEntry];
}
}
return [result sortedArrayUsingSelector:@selector(compareUseTime:)];
}
#pragma mark - Private
- (NSString *)keyForHost:(VT100RemoteHost *)host {
if (host) {
return [NSString stringWithFormat:@"%@@%@", host.username, host.hostname];
} else {
return @"";
}
}
- (NSMutableArray *)commandsForHost:(VT100RemoteHost *)host {
NSString *key = [self keyForHost:host];
NSMutableArray *result = _hosts[key];
if (!result) {
_hosts[key] = result = [NSMutableArray array];
}
return result;
}
- (void)loadCommandHistory {
NSDictionary *archive = [NSKeyedUnarchiver unarchiveObjectWithFile:_path];
for (NSString *host in archive) {
NSMutableArray *commands = _hosts[host];
if (!commands) {
_hosts[host] = commands = [NSMutableArray array];
}
for (NSDictionary *commandDict in archive[host]) {
[commands addObject:[CommandHistoryEntry entryWithDictionary:commandDict]];
}
}
}
- (NSDictionary *)dictionaryForEntries {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
for (NSString *key in _hosts) {
NSArray *array = [self arrayForCommandEntries:_hosts[key]];
if (array.count) {
[dictionary setObject:array
forKey:key];
}
}
return dictionary;
}
- (NSArray *)arrayForCommandEntries:(NSArray *)entries {
NSMutableArray *array = [NSMutableArray array];
NSTimeInterval minLastUse = [NSDate timeIntervalSinceReferenceDate] - kMaxTimeToRememberCommands;
for (CommandHistoryEntry *entry in entries) {
if (entry.lastUsed >= minLastUse) {
[array addObject:[entry dictionary]];
}
}
if (array.count > kMaxCommandsToSavePerHost) {
return [array subarrayWithRange:NSMakeRange(array.count - kMaxCommandsToSavePerHost,
kMaxCommandsToSavePerHost)];
} else {
return array;
}
}
- (void)eraseHistory {
[_hosts release];
_hosts = [[NSMutableDictionary alloc] init];
[[NSFileManager defaultManager] removeItemAtPath:_path error:NULL];
[[NSNotificationCenter defaultCenter] postNotificationName:kCommandHistoryDidChangeNotificationName
object:nil];
}
@end