7
7
//
8
8
9
9
#import " GLFileView.h"
10
+ #import " PBGitGradientBarView.h"
10
11
12
+ #define GROUP_LABEL @" Label" // string
13
+ #define GROUP_SEPARATOR @" HasSeparator" // BOOL as NSNumber
14
+ #define GROUP_SELECTION_MODE @" SelectionMode" // MGScopeBarGroupSelectionMode (int) as NSNumber
15
+ #define GROUP_ITEMS @" Items" // array of dictionaries, each containing the following keys:
16
+ #define ITEM_IDENTIFIER @" Identifier" // string
17
+ #define ITEM_NAME @" Name" // string
11
18
12
19
@implementation GLFileView
13
20
@@ -17,17 +24,170 @@ - (void) awakeFromNib
17
24
// repository = historyController.repository;
18
25
[super awakeFromNib ];
19
26
[historyController.treeController addObserver: self forKeyPath: @" selection" options: 0 context: @" treeController" ];
27
+
28
+ self.groups = [NSMutableArray arrayWithCapacity: 0 ];
29
+
30
+ NSArray *items = [NSArray arrayWithObjects:
31
+ [NSDictionary dictionaryWithObjectsAndKeys:
32
+ startFile, ITEM_IDENTIFIER,
33
+ @" Source" , ITEM_NAME,
34
+ nil ],
35
+ [NSDictionary dictionaryWithObjectsAndKeys:
36
+ @" blame" , ITEM_IDENTIFIER,
37
+ @" Blame" , ITEM_NAME,
38
+ nil ],
39
+ nil ];
40
+ [self .groups addObject: [NSDictionary dictionaryWithObjectsAndKeys:
41
+ [NSNumber numberWithBool: NO ], GROUP_SEPARATOR,
42
+ [NSNumber numberWithInt: MGRadioSelectionMode], GROUP_SELECTION_MODE, // single selection group.
43
+ items, GROUP_ITEMS,
44
+ nil ]];
45
+ [typeBar reloadData ];
20
46
}
21
47
22
48
- (void ) observeValueForKeyPath : (NSString *)keyPath ofObject : (id )object change : (NSDictionary *)change context : (void *)context
23
49
{
24
50
// NSLog(@"keyPath=%@ change=%@ context=%@ object=%@ \n %@",keyPath,change,context,object,[historyController.treeController selectedObjects]);
51
+ [self showFile ];
52
+ }
53
+
54
+ - (void ) showFile
55
+ {
25
56
NSArray *files=[historyController.treeController selectedObjects ];
26
57
if ([files count ]>0 ) {
27
58
PBGitTree *file=[files objectAtIndex: 0 ];
28
- NSString *fileTxt=[file textContents ];
59
+
60
+ NSString *fileTxt=@" " ;
61
+ if (startFile==@" fileview" )
62
+ fileTxt=[file textContents ];
63
+ if (startFile==@" blame" )
64
+ fileTxt=[self parseBlame: [file blame ]];
65
+
29
66
id script = [view windowScriptObject ];
30
67
[script callWebScriptMethod: @" showFile" withArguments: [NSArray arrayWithObject: fileTxt]];
31
68
}
32
69
}
70
+
71
+ #pragma mark MGScopeBarDelegate methods
72
+
73
+
74
+ - (int )numberOfGroupsInScopeBar : (MGScopeBar *)theScopeBar
75
+ {
76
+ return [self .groups count ];
77
+ }
78
+
79
+
80
+ - (NSArray *)scopeBar : (MGScopeBar *)theScopeBar itemIdentifiersForGroup : (int )groupNumber
81
+ {
82
+ return [[self .groups objectAtIndex: groupNumber] valueForKeyPath: [NSString stringWithFormat: @" %@ .%@ " , GROUP_ITEMS, ITEM_IDENTIFIER]];
83
+ }
84
+
85
+
86
+ - (NSString *)scopeBar : (MGScopeBar *)theScopeBar labelForGroup : (int )groupNumber
87
+ {
88
+ return [[self .groups objectAtIndex: groupNumber] objectForKey: GROUP_LABEL]; // might be nil, which is fine (nil means no label).
89
+ }
90
+
91
+
92
+ - (NSString *)scopeBar : (MGScopeBar *)theScopeBar titleOfItem : (NSString *)identifier inGroup : (int )groupNumber
93
+ {
94
+ NSArray *items = [[self .groups objectAtIndex: groupNumber] objectForKey: GROUP_ITEMS];
95
+ if (items) {
96
+ for (NSDictionary *item in items) {
97
+ if ([[item objectForKey: ITEM_IDENTIFIER] isEqualToString: identifier]) {
98
+ return [item objectForKey: ITEM_NAME];
99
+ break ;
100
+ }
101
+ }
102
+ }
103
+ return nil ;
104
+ }
105
+
106
+
107
+ - (MGScopeBarGroupSelectionMode)scopeBar : (MGScopeBar *)theScopeBar selectionModeForGroup : (int )groupNumber
108
+ {
109
+ return [[[self .groups objectAtIndex: groupNumber] objectForKey: GROUP_SELECTION_MODE] intValue ];
110
+ }
111
+
112
+ - (void )scopeBar : (MGScopeBar *)theScopeBar selectedStateChanged : (BOOL )selected forItem : (NSString *)identifier inGroup : (int )groupNumber
113
+ {
114
+ startFile=identifier;
115
+ NSString *path = [NSString stringWithFormat: @" html/views/%@ " , identifier];
116
+ NSString *html = [[NSBundle mainBundle ] pathForResource: @" index" ofType: @" html" inDirectory: path];
117
+ // NSLog(@"[FileViewerController scopeBar:selectedStateChanged] -> file: '%@' (%@)",html,identifier);
118
+ NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL fileURLWithPath: html]];
119
+ [[view mainFrame ] loadRequest: request];
120
+ }
121
+
122
+ - (void ) didLoad
123
+ {
124
+ [self showFile ];
125
+ }
126
+
127
+ - (NSString *) parseBlame : (NSString *)txt
128
+ {
129
+ txt=[txt stringByReplacingOccurrencesOfString: @" <" withString: @" <" ];
130
+ txt=[txt stringByReplacingOccurrencesOfString: @" >" withString: @" >" ];
131
+
132
+ NSArray *lines = [txt componentsSeparatedByString: @" \n " ];
133
+ NSString *line;
134
+ NSMutableDictionary *headers=[NSMutableDictionary dictionary ];
135
+ NSMutableString *res=[NSMutableString string ];
136
+
137
+ [res appendString: @" <table class='blocks'>\n " ];
138
+ int i=0 ;
139
+ while (i<[lines count ]){
140
+ line=[lines objectAtIndex: i];
141
+ NSArray *header=[line componentsSeparatedByString: @" " ];
142
+ if ([header count ]==4 ){
143
+ int nLines=[(NSString *)[header objectAtIndex: 3 ] intValue ];
144
+ [res appendFormat: @" <tr class='block l%d '>\n " ,nLines];
145
+ line=[lines objectAtIndex: ++i];
146
+ if ([[[line componentsSeparatedByString: @" " ] objectAtIndex: 0 ] isEqual: @" author" ]){
147
+ NSString *author=line;
148
+ NSString *summary=nil ;
149
+ while (summary==nil ){
150
+ line=[lines objectAtIndex: i++];
151
+ if ([[[line componentsSeparatedByString: @" " ] objectAtIndex: 0 ] isEqual: @" summary" ]){
152
+ summary=line;
153
+ }
154
+ }
155
+ NSString *block=[NSString stringWithFormat: @" <td><p class='author'>%@ </p><p class='summary'>%@ </p></td>\n <td>\n " ,author,summary];
156
+ [headers setObject: block forKey: [header objectAtIndex: 0 ]];
157
+ }
158
+ [res appendString: [headers objectForKey: [header objectAtIndex: 0 ]]];
159
+
160
+ NSMutableString *code=[NSMutableString string ];
161
+ do {
162
+ line=[lines objectAtIndex: i++];
163
+ }while ([line characterAtIndex: 0 ]!=' \t ' );
164
+ line=[line stringByReplacingOccurrencesOfString: @" \t " withString: @" " ];
165
+ [code appendString: line];
166
+ [code appendString: @" \n " ];
167
+
168
+ int n;
169
+ for (n=1 ;n<nLines;n++){
170
+ line=[lines objectAtIndex: i++];
171
+ do {
172
+ line=[lines objectAtIndex: i++];
173
+ }while ([line characterAtIndex: 0 ]!=' \t ' );
174
+ line=[line stringByReplacingOccurrencesOfString: @" \t " withString: @" " ];
175
+ [code appendString: line];
176
+ [code appendString: @" \n " ];
177
+ }
178
+ [res appendFormat: @" <pre class='first-line: %@ ;brush: objc'>%@ </pre>" ,[header objectAtIndex: 2 ],code];
179
+ [res appendString: @" </td>\n " ];
180
+ }else {
181
+ break ;
182
+ }
183
+ [res appendString: @" </tr>\n " ];
184
+ }
185
+ [res appendString: @" </table>\n " ];
186
+ // NSLog(@"%@",res);
187
+
188
+ return (NSString *)res;
189
+ }
190
+
191
+ @synthesize groups;
192
+
33
193
@end
0 commit comments