]> cgit.babelmonkeys.de Git - jubjub.git/blob - src/gui/cli/JubCLIChatUI.m
Add line editing support to the CLI UI
[jubjub.git] / src / gui / cli / JubCLIChatUI.m
1 #import "JubCLIChatUI.h"
2 #import "JubCLIColor.h"
3
4 #import "linenoise.h"
5
6 @implementation JubCLIChatUI
7 - initWithTitle: (OFString*)title
8      closeBlock: (jub_close_block_t)closeBlock
9       sendBlock: (jub_send_block_t)sendBlock
10 {
11         self = [super init];
12
13         @try {
14                 _sendBlock = [sendBlock copy];
15                 _title = [title copy];
16         } @catch (id e) {
17                 [self release];
18                 @throw e;
19         }
20
21         return self;
22 }
23
24 - (void)dealloc
25 {
26         [_sendBlock release];
27         [_title release];
28         [super dealloc];
29 }
30
31 - (void)addMessage: (OFString*)text
32             sender: (OFString*)sender
33 {
34         [of_stdout writeString: @"\r" COL_IN(@"-> ")];
35         [of_stdout writeFormat: BOLD(@"%@:") @" %@\n", sender, text];
36         [[Linenoise sharedLinenoise] refreshLine];
37 }
38
39 - (void)send: (OFString*)text
40 {
41         [of_stdout writeString: @"\033[1A" COL_OUT(@"<- ")];
42         [of_stdout writeFormat: BOLD(@"%@:") @" %@\n", _title, text];
43
44         _sendBlock(text);
45 }
46 @end