Skip to content

Commit 5c50418

Browse files
committed
Support choosing the date-format for the history pane's commit date (issue #680)
The date column's format was frozen in the nib, so every row read "19 September 2026 at 14:05" and there was no way to ask for anything else. * Add a Date format preference offering the system short, medium and long styles plus a free pattern, defaulting to the long style the column has always shown. Its tooltip says which list the format belongs to, since the label has no room to. * Format the column through PBGitCommitDateFormatter, which builds one formatter for every row on screen and rebuilds it only when the preference moves. * Mark the date cells that are already drawn for redraw when the format changes. A text field asks its formatter for a string as it draws, and the dates themselves do not change, so nothing else marks them. * Size the column to the dates it is now showing, a tenth over, so that a shorter format gives its room back and a longer one does not clip. * Fall back to the long style for a pattern that is empty or all spaces, and show the rendered pattern under the field, so a pattern that does not say what its author meant is visible before it reaches the list. * Move the Git Executable row to Integration and put it first: it is the integration a git client rests on, and it frees the room in General that the date rows now take.
1 parent b54bb4b commit 5c50418

12 files changed

Lines changed: 814 additions & 54 deletions

‎Classes/Controllers/PBGitHistoryController.m‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#import "PBGitStash.h"
3232
#import "PBGitSidebarController.h"
3333
#import "NSString_Truncate.h"
34+
#import "PBGitCommitDateFormatter.h"
3435

3536
#define kHistorySelectedDetailIndexKey @"PBHistorySelectedDetailIndex"
3637
#define kHistoryDetailViewIndex 0
@@ -68,6 +69,7 @@ @interface PBGitHistoryController () <NSTableViewDelegate, NSMenuItemValidation>
6869
- (void)updateBranchFilterMatrix;
6970
- (void)restoreFileBrowserSelection;
7071
- (void)saveFileBrowserSelection;
72+
- (void)resizeDateColumn:(NSTableColumn *)column toFitDates:(NSArray<NSString *> *)dates inFont:(NSFont *)font;
7173

7274
@end
7375

@@ -201,10 +203,59 @@ - (void)loadView
201203

202204
// listen for updates
203205
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_repositoryUpdatedNotification:) name:PBGitRepositoryEventNotification object:repository];
206+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_commitDateFormatChangedNotification:) name:PBGitCommitDateFormatDidChangeNotification object:nil];
204207

205208
[super awakeFromNib];
206209
}
207210

211+
// A text field asks its formatter for a string as it draws, so a row that is
212+
// not marked for redraw goes on showing the format it was drawn with. The dates
213+
// themselves do not change, so nothing else marks them.
214+
- (void)_commitDateFormatChangedNotification:(NSNotification *)notification
215+
{
216+
NSInteger dateColumn = [commitList columnWithIdentifier:@"DateColumn"];
217+
if (dateColumn == -1)
218+
return;
219+
220+
// A field hands back the string its formatter makes of the value it holds,
221+
// so the dates collected here are already in the format just chosen.
222+
NSMutableArray<NSString *> *dates = [NSMutableArray array];
223+
__block NSFont *font = nil;
224+
225+
PBCommitList *list = commitList;
226+
[list enumerateAvailableRowViewsUsingBlock:^(NSTableRowView *rowView, NSInteger row) {
227+
NSTextField *field = ((NSTableCellView *)[list viewAtColumn:dateColumn row:row makeIfNecessary:NO]).textField;
228+
if (!field)
229+
return;
230+
231+
[field setNeedsDisplay:YES];
232+
[dates addObject:field.stringValue];
233+
font = field.font;
234+
}];
235+
236+
[self resizeDateColumn:list.tableColumns[dateColumn] toFitDates:dates inFont:font];
237+
}
238+
239+
// A tenth over the widest date on screen, so that a longer month name or a
240+
// two-digit day further down the list does not arrive clipped. The header is a
241+
// floor, since a pattern can be shorter than the word "Date".
242+
static const CGFloat PBDateColumnSlack = 1.1;
243+
244+
- (void)resizeDateColumn:(NSTableColumn *)column toFitDates:(NSArray<NSString *> *)dates inFont:(NSFont *)font
245+
{
246+
if (!dates.count || !font)
247+
return;
248+
249+
NSDictionary *attributes = @{NSFontAttributeName : font};
250+
CGFloat widest = 0;
251+
for (NSString *date in dates)
252+
widest = MAX(widest, [date sizeWithAttributes:attributes].width);
253+
254+
CGFloat width = MAX(widest * PBDateColumnSlack, column.headerCell.cellSize.width);
255+
256+
column.width = MIN(MAX(ceil(width), column.minWidth), column.maxWidth);
257+
}
258+
208259
- (void)_repositoryUpdatedNotification:(NSNotification *)notification
209260
{
210261
PBGitRepositoryWatcherEventType eventType = [(NSNumber *)[[notification userInfo] objectForKey:kPBGitRepositoryEventTypeUserInfoKey] unsignedIntValue];

‎Classes/Controllers/PBPrefsWindowController.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
IBOutlet NSView *integrationPrefsView;
1616
IBOutlet NSView *updatesPrefsView;
1717

18+
/* Variables for the General View */
19+
IBOutlet NSPopUpButton *commitDateFormatPopup;
20+
IBOutlet NSTextField *commitDateCustomFormatField;
21+
IBOutlet NSTextField *commitDateSampleField;
22+
1823
/* Variables for the Integration View */
1924
IBOutlet NSPopUpButton *terminalHandlerPopup;
20-
21-
/* Variables for the Updates View */
2225
IBOutlet NSPathControl *gitPathController;
2326
IBOutlet NSImageView *badGitPathIcon;
2427
IBOutlet NSView *gitPathOpenAccessory;
@@ -31,5 +34,6 @@
3134
- (IBAction)resetGitPath:sender;
3235
- (IBAction)resetAllDialogWarnings:(id)sender;
3336
- (IBAction)changeTerminalHandler:(id)sender;
37+
- (IBAction)changeCommitDateFormat:(id)sender;
3438

3539
@end

‎Classes/Controllers/PBPrefsWindowController.m‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "PBGitRepository.h"
1111
#import "PBGitDefaults.h"
1212
#import "PBTerminalUtil.h"
13+
#import "PBGitCommitDateFormatter.h"
1314

1415
#define kPreferenceViewIdentifier @"PBGitXPreferenceViewIdentifier"
1516

@@ -22,6 +23,7 @@ - (void)windowDidLoad
2223
[super windowDidLoad];
2324

2425
[self populateTerminalHandlers];
26+
[self updateCommitDateSample];
2527
}
2628

2729
- (void)setupToolbar
@@ -121,6 +123,37 @@ - (IBAction)changeTerminalHandler:(id)sender
121123
[PBGitDefaults setTerminalHandler:handler];
122124
}
123125

126+
#pragma mark -
127+
#pragma mark Commit date format
128+
129+
// The popup and the field are read rather than the preferences they write, so
130+
// the sample shows what was just picked whichever of them the change came from.
131+
- (void)updateCommitDateSample
132+
{
133+
PBCommitDateFormatSetting setting = commitDateFormatPopup.selectedTag;
134+
NSString *customFormat = commitDateCustomFormatField.stringValue;
135+
136+
commitDateCustomFormatField.enabled = (setting == PBCommitDateFormatCustom);
137+
138+
NSDateFormatter *formatter = [PBGitCommitDateFormatter dateFormatterForSetting:setting customFormat:customFormat];
139+
NSString *sample = [formatter stringFromDate:[NSDate date]];
140+
141+
// A pattern can be well formed and still render to nothing, so the sample
142+
// says the column would be empty instead of going blank itself.
143+
commitDateSampleField.stringValue = sample.length ? sample : NSLocalizedString(@"(nothing)", @"Preferences: what an empty commit date pattern shows");
144+
}
145+
146+
- (IBAction)changeCommitDateFormat:(id)sender
147+
{
148+
[self updateCommitDateSample];
149+
}
150+
151+
- (void)controlTextDidChange:(NSNotification *)notification
152+
{
153+
if (notification.object == commitDateCustomFormatField)
154+
[self updateCommitDateSample];
155+
}
156+
124157
#pragma mark -
125158
#pragma mark Git Path open panel actions
126159

‎Classes/PBGitCommitDateFormatter.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// PBGitCommitDateFormatter.h
3+
// GitX
4+
//
5+
6+
#import <Cocoa/Cocoa.h>
7+
8+
#import "PBGitDefaults.h"
9+
10+
// Posted when the commit date preference changes, so lists already on screen can
11+
// redraw the dates they are showing.
12+
extern NSString *const PBGitCommitDateFormatDidChangeNotification;
13+
14+
@interface PBGitCommitDateFormatter : NSFormatter
15+
16+
// A setting the user cannot reach through the popup, and a custom format that is
17+
// empty or all spaces, both fall back to the long style rather than showing
18+
// nothing at all.
19+
+ (NSDateFormatter *)dateFormatterForSetting:(PBCommitDateFormatSetting)setting customFormat:(NSString *)customFormat;
20+
21+
+ (NSDateFormatter *)currentDateFormatter;
22+
23+
@end

‎Classes/PBGitCommitDateFormatter.m‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// PBGitCommitDateFormatter.m
3+
// GitX
4+
//
5+
6+
#import "PBGitCommitDateFormatter.h"
7+
8+
NSString *const PBGitCommitDateFormatDidChangeNotification = @"PBGitCommitDateFormatDidChangeNotification";
9+
10+
// The history list makes one of these per visible row, so the built formatter is
11+
// shared by all of them and rebuilt only when the preference actually moves.
12+
static PBCommitDateFormatSetting builtSetting;
13+
static NSString *builtCustomFormat;
14+
static NSDateFormatter *builtDateFormatter;
15+
16+
static PBCommitDateFormatSetting announcedSetting;
17+
static NSString *announcedCustomFormat;
18+
19+
static BOOL formatsAreEqual(NSString *one, NSString *other)
20+
{
21+
return (one == other) || [one isEqualToString:other];
22+
}
23+
24+
@implementation PBGitCommitDateFormatter
25+
26+
+ (void)initialize
27+
{
28+
if (self != [PBGitCommitDateFormatter class])
29+
return;
30+
31+
announcedSetting = [PBGitDefaults commitDateFormat];
32+
announcedCustomFormat = [[PBGitDefaults commitDateCustomFormat] copy];
33+
34+
[[NSNotificationCenter defaultCenter] addObserver:self
35+
selector:@selector(userDefaultsChanged:)
36+
name:NSUserDefaultsDidChangeNotification
37+
object:nil];
38+
}
39+
40+
// Every defaults write reaches here, so the date preference is compared against
41+
// the one last announced before anything is said about it.
42+
+ (void)userDefaultsChanged:(NSNotification *)notification
43+
{
44+
PBCommitDateFormatSetting setting = [PBGitDefaults commitDateFormat];
45+
NSString *customFormat = [PBGitDefaults commitDateCustomFormat];
46+
47+
if ((setting == announcedSetting) && formatsAreEqual(customFormat, announcedCustomFormat))
48+
return;
49+
50+
announcedSetting = setting;
51+
announcedCustomFormat = [customFormat copy];
52+
53+
[[NSNotificationCenter defaultCenter] postNotificationName:PBGitCommitDateFormatDidChangeNotification object:nil];
54+
}
55+
56+
+ (NSDateFormatter *)dateFormatterForSetting:(PBCommitDateFormatSetting)setting customFormat:(NSString *)customFormat
57+
{
58+
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
59+
60+
NSString *pattern = [customFormat stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
61+
if ((setting == PBCommitDateFormatCustom) && pattern.length) {
62+
formatter.dateFormat = customFormat;
63+
return formatter;
64+
}
65+
66+
formatter.timeStyle = NSDateFormatterShortStyle;
67+
68+
switch (setting) {
69+
case PBCommitDateFormatShort:
70+
formatter.dateStyle = NSDateFormatterShortStyle;
71+
break;
72+
case PBCommitDateFormatMedium:
73+
formatter.dateStyle = NSDateFormatterMediumStyle;
74+
break;
75+
default:
76+
formatter.dateStyle = NSDateFormatterLongStyle;
77+
break;
78+
}
79+
80+
return formatter;
81+
}
82+
83+
// The preference is read rather than waited for, so a formatter built here is
84+
// the one the preference asks for however the setting got there.
85+
+ (NSDateFormatter *)currentDateFormatter
86+
{
87+
PBCommitDateFormatSetting setting = [PBGitDefaults commitDateFormat];
88+
NSString *customFormat = (setting == PBCommitDateFormatCustom) ? [PBGitDefaults commitDateCustomFormat] : nil;
89+
90+
if (!builtDateFormatter || (setting != builtSetting) || !formatsAreEqual(customFormat, builtCustomFormat)) {
91+
builtSetting = setting;
92+
builtCustomFormat = [customFormat copy];
93+
builtDateFormatter = [self dateFormatterForSetting:setting customFormat:customFormat];
94+
}
95+
96+
return builtDateFormatter;
97+
}
98+
99+
- (NSString *)stringForObjectValue:(id)date
100+
{
101+
if (![date isKindOfClass:[NSDate class]])
102+
return nil;
103+
104+
return [[[self class] currentDateFormatter] stringFromDate:date];
105+
}
106+
107+
@end

‎Classes/git/PBGitDefaults.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ typedef NS_ENUM(NSInteger, PBPruneOnFetchSetting) {
1414
PBPruneOnFetchNever = 2,
1515
};
1616

17+
typedef NS_ENUM(NSInteger, PBCommitDateFormatSetting) {
18+
PBCommitDateFormatShort = 0,
19+
PBCommitDateFormatMedium = 1,
20+
PBCommitDateFormatLong = 2,
21+
PBCommitDateFormatCustom = 3,
22+
};
23+
1724
@interface PBGitDefaults : NSObject {
1825
}
1926

@@ -39,6 +46,8 @@ typedef NS_ENUM(NSInteger, PBPruneOnFetchSetting) {
3946
+ (PBPruneOnFetchSetting)pruneOnFetch;
4047
+ (NSString *)terminalHandler;
4148
+ (void)setTerminalHandler:(NSString *)bundleIdentifier;
49+
+ (PBCommitDateFormatSetting)commitDateFormat;
50+
+ (NSString *)commitDateCustomFormat;
4251

4352

4453
// Suppressed Dialog Warnings

‎Classes/git/PBGitDefaults.m‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#define kUseRepositoryWatcher @"PBUseRepositoryWatcher"
3333
#define kPruneOnFetch @"PBPruneOnFetch"
3434
#define kTerminalHandler @"PBTerminalHandler"
35+
#define kCommitDateFormat @"PBCommitDateFormat"
36+
#define kCommitDateCustomFormat @"PBCommitDateCustomFormat"
37+
#define kDefaultCommitDateCustomFormat @"yyyy-MM-dd HH:mm"
3538

3639
@implementation PBGitDefaults
3740

@@ -70,6 +73,10 @@ + (void)initialize
7073
forKey:kPruneOnFetch];
7174
[defaultValues setObject:@"com.apple.Terminal"
7275
forKey:kTerminalHandler];
76+
[defaultValues setObject:[NSNumber numberWithInteger:PBCommitDateFormatLong]
77+
forKey:kCommitDateFormat];
78+
[defaultValues setObject:kDefaultCommitDateCustomFormat
79+
forKey:kCommitDateCustomFormat];
7380
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
7481
}
7582

@@ -246,4 +253,14 @@ + (void)setTerminalHandler:(NSString *)bundleIdentifier
246253
[[NSUserDefaults standardUserDefaults] setObject:bundleIdentifier forKey:kTerminalHandler];
247254
}
248255

256+
+ (PBCommitDateFormatSetting)commitDateFormat
257+
{
258+
return [[NSUserDefaults standardUserDefaults] integerForKey:kCommitDateFormat];
259+
}
260+
261+
+ (NSString *)commitDateCustomFormat
262+
{
263+
return [[NSUserDefaults standardUserDefaults] stringForKey:kCommitDateCustomFormat];
264+
}
265+
249266
@end

0 commit comments

Comments
 (0)