forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandHistoryEntry.m
More file actions
140 lines (119 loc) · 3.67 KB
/
CommandHistoryEntry.m
File metadata and controls
140 lines (119 loc) · 3.67 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
//
// CommandHistoryEntry.m
// iTerm
//
// Created by George Nachman on 1/19/14.
//
//
#import "CommandHistoryEntry.h"
#import "CommandUse.h"
// Keys for serializing an entry
static NSString *const kCommand = @"command";
static NSString *const kUses = @"uses";
static NSString *const kLastUsed = @"last used";
static NSString *const kUseTimes = @"use times";
@interface CommandHistoryEntry () <NSCopying>
@property(nonatomic, retain) NSMutableArray *useTimes;
@end
@implementation CommandHistoryEntry
+ (instancetype)commandHistoryEntry {
return [[[self alloc] init] autorelease];
}
+ (instancetype)entryWithDictionary:(NSDictionary *)dict {
CommandHistoryEntry *entry = [self commandHistoryEntry];
entry.command = dict[kCommand];
entry.uses = [dict[kUses] intValue];
entry.lastUsed = [dict[kLastUsed] doubleValue];
[entry setUseTimesFromSerializedArray:dict[kUseTimes]];
return entry;
}
- (id)init {
self = [super init];
if (self) {
_useTimes = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc {
[_command release];
[_useTimes release];
[super dealloc];
}
- (NSArray *)serializedUseTimes {
NSMutableArray *result = [NSMutableArray array];
for (CommandUse *use in self.useTimes) {
[result addObject:[use serializedValue]];
}
return result;
}
- (void)setUseTimesFromSerializedArray:(NSArray *)array {
for (NSArray *value in array) {
[self.useTimes addObject:[CommandUse commandUseFromSerializedValue:value]];
}
}
- (NSDictionary *)dictionary {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
if (self.command) {
dict[kCommand] = self.command;
}
dict[kUses] = @(self.uses);
dict[kLastUsed] = @(self.lastUsed);
dict[kUseTimes] = [self serializedUseTimes];
return dict;
}
- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p command=%@ uses=%d lastUsed=%@ matchLocation=%d>",
[self class],
self,
self.command,
self.uses,
[NSDate dateWithTimeIntervalSinceReferenceDate:self.lastUsed],
self.matchLocation];
}
- (VT100ScreenMark *)lastMark {
CommandUse *use = [self.useTimes lastObject];
return use.mark;
}
- (NSString *)lastDirectory {
CommandUse *use = [self.useTimes lastObject];
return use.directory.length > 0 ? use.directory : nil;
}
// Used to sort from highest to lowest score. So Ascending means self's score is higher
// than other's.
- (NSComparisonResult)compare:(CommandHistoryEntry *)other {
if (_matchLocation == 0 && other.matchLocation > 0) {
return NSOrderedDescending;
}
if (other.matchLocation == 0 && _matchLocation > 0) {
return NSOrderedAscending;
}
int otherUses = other.uses;
if (_uses < otherUses) {
return NSOrderedDescending;
} else if (_uses > other.uses) {
return NSOrderedAscending;
}
NSTimeInterval otherLastUsed = other.lastUsed;
if (_lastUsed < otherLastUsed) {
return NSOrderedDescending;
} else if (_lastUsed > otherLastUsed) {
return NSOrderedAscending;
} else {
return NSOrderedSame;
}
}
- (NSComparisonResult)compareUseTime:(CommandHistoryEntry *)other {
return [@(other.lastUsed) compare:@(self.lastUsed)];
}
- (id)copyWithZone:(NSZone *)zone {
CommandHistoryEntry *theCopy = [[CommandHistoryEntry alloc] init];
theCopy.command = self.command;
theCopy.uses = self.uses;
theCopy.lastUsed = self.lastUsed;
theCopy.useTimes = [NSMutableArray array];
for (CommandUse *use in self.useTimes) {
[theCopy.useTimes addObject:[[use copy] autorelease]];
}
return theCopy;
}
@end