Add extremely basic editing and message sending

master
C. McEnroe 2020-02-05 00:20:39 -05:00
parent 0df8bd51aa
commit 4cce893eab
5 changed files with 72 additions and 10 deletions

View File

@ -6,6 +6,7 @@ CFLAGS += -std=c11 -Wall -Wextra -Wpedantic
LDLIBS = -lcurses -lcrypto -ltls LDLIBS = -lcurses -lcrypto -ltls
OBJS += chat.o OBJS += chat.o
OBJS += command.o
OBJS += edit.o OBJS += edit.o
OBJS += handle.o OBJS += handle.o
OBJS += irc.o OBJS += irc.o

12
chat.h
View File

@ -20,6 +20,7 @@
#include <string.h> #include <string.h>
#include <sysexits.h> #include <sysexits.h>
#include <time.h> #include <time.h>
#include <wchar.h>
#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0])) #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
#define BIT(x) x##Bit, x = 1 << x##Bit, x##Bit_ = x##Bit #define BIT(x) x##Bit, x = 1 << x##Bit, x##Bit_ = x##Bit
@ -109,6 +110,7 @@ void ircFormat(const char *format, ...)
__attribute__((format(printf, 1, 2))); __attribute__((format(printf, 1, 2)));
void handle(struct Message msg); void handle(struct Message msg);
void command(size_t id, char *input);
enum Heat { Cold, Warm, Hot }; enum Heat { Cold, Warm, Hot };
void uiInit(void); void uiInit(void);
@ -122,8 +124,14 @@ void uiFormat(
size_t id, enum Heat heat, const time_t *time, const char *format, ... size_t id, enum Heat heat, const time_t *time, const char *format, ...
) __attribute__((format(printf, 4, 5))); ) __attribute__((format(printf, 4, 5)));
const char *editHead(void); enum Edit {
const char *editTail(void); EditKill,
EditInsert,
EditEnter,
};
void edit(size_t id, enum Edit op, wchar_t ch);
char *editHead(void);
char *editTail(void);
static inline enum Color hash(const char *str) { static inline enum Color hash(const char *str) {
if (*str == '~') str++; if (*str == '~') str++;

32
command.c 100644
View File

@ -0,0 +1,32 @@
/* Copyright (C) 2020 C. McEnroe <june@causal.agency>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include "chat.h"
void command(size_t id, char *input) {
ircFormat("PRIVMSG %s :%s\r\n", idNames[id], input);
struct Message msg = {
.nick = self.nick,
// TODO: .user,
.cmd = "PRIVMSG",
.params[0] = idNames[id],
.params[1] = input,
};
handle(msg);
}

32
edit.c
View File

@ -23,22 +23,40 @@
#include "chat.h" #include "chat.h"
enum { Cap = 512 }; enum { Cap = 512 };
static wchar_t buf[Cap] = L"foo\0033bar\3baz"; static wchar_t buf[Cap];
static size_t len = 12; static size_t len;
static size_t pos = 6; static size_t pos;
const char *editHead(void) { char *editHead(void) {
static char mbs[MB_LEN_MAX * Cap]; static char mbs[MB_LEN_MAX * Cap];
const wchar_t *ptr = buf; const wchar_t *ptr = buf;
size_t n = wcsnrtombs(mbs, &ptr, pos, sizeof(mbs), NULL); size_t n = wcsnrtombs(mbs, &ptr, pos, sizeof(mbs) - 1, NULL);
assert(n != (size_t)-1); assert(n != (size_t)-1);
mbs[n] = '\0';
return mbs; return mbs;
} }
const char *editTail(void) { char *editTail(void) {
static char mbs[MB_LEN_MAX * Cap]; static char mbs[MB_LEN_MAX * Cap];
const wchar_t *ptr = &buf[pos]; const wchar_t *ptr = &buf[pos];
size_t n = wcsnrtombs(mbs, &ptr, len - pos, sizeof(mbs), NULL); size_t n = wcsnrtombs(mbs, &ptr, len - pos, sizeof(mbs) - 1, NULL);
assert(n != (size_t)-1); assert(n != (size_t)-1);
mbs[n] = '\0';
return mbs; return mbs;
} }
void edit(size_t id, enum Edit op, wchar_t ch) {
switch (op) {
break; case EditKill: len = pos = 0;
break; case EditInsert: {
if (len == Cap) break;
buf[pos++] = ch;
len++;
}
break; case EditEnter: {
pos = 0;
command(id, editTail());
len = 0;
}
}
}

5
ui.c
View File

@ -498,8 +498,11 @@ static void keyMeta(wchar_t ch) {
} }
static void keyCtrl(wchar_t ch) { static void keyCtrl(wchar_t ch) {
size_t id = windows.active->id;
switch (ch) { switch (ch) {
break; case L'J': edit(id, EditEnter, 0);
break; case L'L': clearok(curscr, true); break; case L'L': clearok(curscr, true);
break; case L'U': edit(id, EditKill, 0);
} }
} }
@ -518,7 +521,7 @@ void uiRead(void) {
} else if (iswcntrl(ch)) { } else if (iswcntrl(ch)) {
keyCtrl(ch ^ L'@'); keyCtrl(ch ^ L'@');
} else { } else {
// TODO: Insert. edit(windows.active->id, EditInsert, ch);
} }
meta = false; meta = false;
} }