From 6b2a30239309b33d2b1dee2de214231be9875403 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 27 May 2017 14:32:32 +0300 Subject: [PATCH] Terminal-style command history in the Cocoa debugger --- Cocoa/Document.xib | 2 +- Cocoa/GBTerminalTextFieldCell.h | 5 +++ Cocoa/GBTerminalTextFieldCell.m | 73 +++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 Cocoa/GBTerminalTextFieldCell.h create mode 100644 Cocoa/GBTerminalTextFieldCell.m diff --git a/Cocoa/Document.xib b/Cocoa/Document.xib index 0f2159e..18114a3 100644 --- a/Cocoa/Document.xib +++ b/Cocoa/Document.xib @@ -112,7 +112,7 @@ - + diff --git a/Cocoa/GBTerminalTextFieldCell.h b/Cocoa/GBTerminalTextFieldCell.h new file mode 100644 index 0000000..eae02e5 --- /dev/null +++ b/Cocoa/GBTerminalTextFieldCell.h @@ -0,0 +1,5 @@ +#import + +@interface GBTerminalTextFieldCell : NSTextFieldCell + +@end diff --git a/Cocoa/GBTerminalTextFieldCell.m b/Cocoa/GBTerminalTextFieldCell.m new file mode 100644 index 0000000..95f6551 --- /dev/null +++ b/Cocoa/GBTerminalTextFieldCell.m @@ -0,0 +1,73 @@ +#import +#import "GBTerminalTextFieldCell.h" + +@interface GBTerminalTextView : NSTextView +@end + +@implementation GBTerminalTextFieldCell +{ + GBTerminalTextView *field_editor; +} + +- (NSTextView *)fieldEditorForView:(NSView *)controlView +{ + if (field_editor) { + return field_editor; + } + field_editor = [[GBTerminalTextView alloc] init]; + [field_editor setFieldEditor:YES]; + return field_editor; +} + +@end + +@implementation GBTerminalTextView +{ + NSMutableOrderedSet *lines; + NSUInteger current_line; +} + +- (instancetype)init +{ + self = [super init]; + if (!self) { + return NULL; + } + lines = [[NSMutableOrderedSet alloc] init]; + return self; +} + +- (void)moveUp:(id)sender +{ + if (current_line != 0) { + current_line--; + [self setString:[lines objectAtIndex:current_line]]; + } +} + +- (void)moveDown:(id)sender +{ + if (current_line == [lines count]) { + return; + } + current_line++; + if (current_line == [lines count]) { + [self setString:@""]; + } + else { + [self setString:[lines objectAtIndex:current_line]]; + } +} + +-(void) insertNewline:(id)sender +{ + if ([self.string length]) { + NSString *string = [self.string copy]; + [lines removeObject:string]; + [lines addObject:string]; + } + [super insertNewline:sender]; + current_line = [lines count]; +} + +@end