#import "JubCLIChatUI.h"
#import "JubCLIColor.h"

#import "linenoise.h"

@implementation JubCLIChatUI
- initWithTitle: (OFString*)title
     closeBlock: (jub_close_block_t)closeBlock
      sendBlock: (jub_send_block_t)sendBlock
{
	self = [super init];

	@try {
		_sendBlock = [sendBlock copy];
		_title = [title copy];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_sendBlock release];
	[_title release];
	[super dealloc];
}

- (void)addMessage: (OFString*)text
	    sender: (OFString*)sender
{
	[of_stdout writeString: @"\r" COL_IN(@"-> ")];
	[of_stdout writeFormat: BOLD(@"%@:") @" %@\n", sender, text];
	[[Linenoise sharedLinenoise] refreshLine];
}

- (void)send: (OFString*)text
{
	[of_stdout writeString: @"\033[1A" COL_OUT(@"<- ")];
	[of_stdout writeFormat: BOLD(@"%@:") @" %@\n", _title, text];

	_sendBlock(text);
}
@end
