-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
CommandHistoryPopup.m
102 lines (89 loc) · 3.06 KB
/
CommandHistoryPopup.m
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
//
// CommandHistoryPopup.m
// iTerm
//
// Created by George Nachman on 1/14/14.
//
//
#import "CommandHistoryPopup.h"
#import "CommandHistory.h"
#import "CommandHistoryEntry.h"
#import "NSDateFormatterExtras.h"
#import "PopupModel.h"
@implementation CommandHistoryPopupEntry
- (void)dealloc {
[_command release];
[_date release];
[super dealloc];
}
@end
@implementation CommandHistoryPopupWindowController {
IBOutlet NSTableView *_tableView;
int _partialCommandLength;
}
- (instancetype)init
{
self = [super initWithWindowNibName:@"CommandHistoryPopup"
tablePtr:nil
model:[[[PopupModel alloc] init] autorelease]];
if (self) {
[self setTableView:_tableView];
}
return self;
}
- (void)dealloc {
[_tableView setDelegate:nil];
[_tableView setDataSource:nil];
[super dealloc];
}
- (NSArray *)commandsForHost:(VT100RemoteHost *)host
partialCommand:(NSString *)partialCommand
expand:(BOOL)expand {
CommandHistory *history = [CommandHistory sharedInstance];
if (expand) {
return [history autocompleteSuggestionsWithPartialCommand:partialCommand onHost:host];
} else {
return [history commandHistoryEntriesWithPrefix:partialCommand onHost:host];
}
}
- (void)loadCommands:(NSArray *)commands partialCommand:(NSString *)partialCommand {
[[self unfilteredModel] removeAllObjects];
_partialCommandLength = partialCommand.length;
for (id obj in commands) {
CommandHistoryPopupEntry *popupEntry = [[[CommandHistoryPopupEntry alloc] init] autorelease];
if ([obj isKindOfClass:[CommandUse class]]) {
CommandUse *commandUse = obj;
popupEntry.command = commandUse.command;
popupEntry.date = [NSDate dateWithTimeIntervalSinceReferenceDate:commandUse.time];
} else {
CommandHistoryEntry *entry = obj;
popupEntry.command = entry.command;
popupEntry.date = [NSDate dateWithTimeIntervalSinceReferenceDate:entry.lastUsed];
}
[popupEntry setMainValue:popupEntry.command];
[[self unfilteredModel] addObject:popupEntry];
}
[self reloadData:YES];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(NSInteger)rowIndex
{
CommandHistoryPopupEntry* entry = [[self model] objectAtIndex:[self convertIndex:rowIndex]];
if ([[aTableColumn identifier] isEqualToString:@"date"]) {
// Date
return [NSDateFormatter dateDifferenceStringFromDate:entry.date];
} else {
// Contents
return [super tableView:aTableView objectValueForTableColumn:aTableColumn row:rowIndex];
}
}
- (void)rowSelected:(id)sender
{
if ([_tableView selectedRow] >= 0) {
CommandHistoryPopupEntry* entry = [[self model] objectAtIndex:[self convertIndex:[_tableView selectedRow]]];
[self.delegate popupInsertText:[entry.command substringFromIndex:_partialCommandLength]];
[super rowSelected:sender];
}
}
@end