Skip to content

Commit

Permalink
handle encodings in printing output
Browse files Browse the repository at this point in the history
Only really tested for LANG=en_US.UTF-8, but in theory this is
encoding-independent.
  • Loading branch information
codekitchen committed Nov 26, 2019
1 parent 7801beb commit 015fc22
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"args": [],
"env": {
"TERM": "xterm-256color",
"LANG": "en_US.UTF-8",
},
"cwd": "${workspaceFolder}",
"preLaunchTask": "make",
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
### Changed

- Fixed potential crash in terminfo usage.
- Use the C locale to better support non-ASCII in the output.

## [1.1] - 2019-11-24

Expand Down
31 changes: 18 additions & 13 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "config.h"
#include <getopt.h>
#include <locale.h>
#include <ncurses.h>
#include <readline/readline.h>
#include <stdio.h>
Expand Down Expand Up @@ -54,25 +55,28 @@ void read_show_output(FILE *s, size_t *shown, size_t *total) {
termput0("cd");
char *line = NULL;
size_t len = 0;
for (;;) {
ssize_t read = getline(&line, &len, s);
if (read < 0)
break;
ssize_t read;
wchar_t *wideline = (wchar_t*)calloc(COLS+1, sizeof(wchar_t));
while ((read = getline(&line, &len, s)) >= 0) {
*total += 1;
// if we haven't filled the screen yet, display this line.
if (*shown < LINES - 2) {
// sometimes newline, sometimes not, so chomp off any newline and
// we'll add it ourselves for consistency.
if (line[read - 1] == '\n')
// first, convert to a wchar_t string so we can easily count characters.
// limiting to COLS here truncates # of chars to the screen width.
size_t nchars = abort_ltz(mbstowcs(wideline, line, COLS));
// then, convert back to a byte string for display.
read = wcstombs(line, wideline, read);
// sometimes newline, sometimes not, so chomp off any newline
// and we'll add it ourselves for consistency.
if (read > 0 && line[read - 1] == '\n') {
read -= 1;
// truncate lines that are wider than the screen.
if (read > COLS)
read = COLS;
}
printf("%.*s\n", (int)read, line);
*shown += 1;
}
*total += 1;
}
if (line)
free(line);
free(line);
free(wideline);
}

int read_command(const char *command, size_t *shown, size_t *total) {
Expand Down Expand Up @@ -174,6 +178,7 @@ void version() {
int main(int argc, char *const *argv) {
if (argc)
program_name = argv[0];
char *locale = setlocale(LC_ALL, "");
int c;
while ((c = getopt_long(argc, argv, "hv", long_options, NULL)) != -1) {
switch (c) {
Expand Down

0 comments on commit 015fc22

Please sign in to comment.