jcs
/subtext
/amendments
/447
board: Word-wrap post bodies
jcs made amendment 447 about 1 year ago
--- board.c Sat Mar 25 21:29:51 2023
+++ board.c Sun Mar 26 10:18:26 2023
@@ -840,7 +840,8 @@ board_post_read(struct session *s, struct board *board
"Re: " : ""), fpost.subject);
session_flush(s);
session_printf(s, "\r\n");
- session_output(s, fpost.body, fpost.body_size);
+ session_output_word_wrapped(s, fpost.body, fpost.body_size,
+ s->terminal_columns - 1);
session_printf(s, "\r\n");
} else {
size = bile_read_alloc(board->bile, BOARD_POST_RTYPE, id, &data);
@@ -887,7 +888,8 @@ board_post_read(struct session *s, struct board *board
(post.parent_post_id ? "Re: " : ""), thread.subject);
session_flush(s);
session_printf(s, "\r\n");
- session_output(s, post.body, post.body_size);
+ session_output_word_wrapped(s, post.body, post.body_size,
+ s->terminal_columns - 1);
session_printf(s, "\r\n");
}
--- session.c Sat Mar 25 21:31:44 2023
+++ session.c Sun Mar 26 10:28:02 2023
@@ -573,6 +573,47 @@ session_output(struct session *session, const char *st
}
size_t
+session_output_word_wrapped(struct session *session, const char *str,
+ size_t len, size_t line_len)
+{
+ size_t olen = 0, n, adv, last_space = 0;
+
+ while (len && !session->ending) {
+ for (n = 0, adv = 0; n < line_len && n < len; n++) {
+ if (str[n] == '\r') {
+ adv = last_space = n + 1;
+ if (str[n + 1] == '\n')
+ adv++;
+ break;
+ } else if (str[n] == '\n') {
+ adv = last_space = n + 1;
+ break;
+ } else if (str[n] == ' ') {
+ adv = last_space = n + 1;
+ }
+ }
+ if (last_space == 0) {
+ /* no space, break hard */
+ last_space = MIN(line_len, len);
+ adv = last_space;
+ }
+ if (len == n)
+ adv = last_space = len;
+
+ session_output(session, str, last_space);
+ olen += last_space;
+ str += adv;
+ len -= adv;
+ if (len) {
+ session_output(session, "\r\n", 2);
+ olen += 2;
+ }
+ }
+
+ return olen;
+}
+
+size_t
session_printf(struct session *session, const char *format, ...)
{
va_list ap;
--- session.h Thu Mar 23 17:53:14 2023
+++ session.h Sat Mar 25 21:55:51 2023
@@ -136,6 +136,8 @@ void session_flush(struct session *session);
size_t session_logf(struct session *session, const char *format, ...);
size_t session_logf_buffered(struct session *session, const char *format, ...);
size_t session_output(struct session *session, const char *str, size_t len);
+size_t session_output_word_wrapped(struct session *session, const char *str,
+ size_t len, size_t line_len);
size_t session_printf(struct session *session, const char *format, ...);
size_t session_output_view_or_printf(struct session *session, short id,
const char *format, ...);