Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #3 - makes it work with wide unicode characters #4

Merged
merged 2 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/show_lines.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ while printf ''; do
{"text":"json object"},
{"text":"json with urgent flag", "urgent":true},
{"text":"json with highlight flag", "highlight": true},
{"text":"multi-byte unicode: •"},
{"text":"json <i>with</i> <b>markup</b> <b><i>flag</i></b>", "markup": true},
{"text":"json <i>toggling</i> <b>markup</b> flag", "markup": $toggleMarkup},
{"text":"json <i>without</i> <b>markup</b> <b><i>flag</i></b>", "markup": false},
Expand All @@ -29,4 +30,4 @@ EOF
sleep 1;
if [ "$toggleMarkup" = "true" ]; then toggleMarkup="false"; else toggleMarkup="true"; fi

done | rofi -modi blocks -show blocks "$@"
done | rofi -modi blocks -show blocks "$@"
60 changes: 53 additions & 7 deletions src/blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,45 @@ typedef struct
utils
***************/

void json_escape(char *in, char *out) {
while (*in) {
switch (*in) {
case '\\':
*(out++) = '\\';
*(out++) = *in;
break;
case '"':
*(out++) = '\\';
*(out++) = '"';
break;
case '\t':
*(out++) = '\\';
*(out++) = 't';
break;
case '\r':
*(out++) = '\\';
*(out++) = 'r';
break;
case '\f':
*(out++) = '\\';
*(out++) = 'f';
break;
case '\b':
*(out++) = '\\';
*(out++) = 'b';
break;
case '\n':
*(out++) = '\\';
*(out++) = 'n';
break;
default:
*(out++) = *in;
break;
}
in++;
}
}

// Result is an allocated a new string
char *str_replace(const char *orig, const char *rep, const char *with) {
char *result; // the return string
Expand Down Expand Up @@ -199,7 +238,12 @@ char *str_replace_in(char **orig, const char *rep, const char *with) {
}

char *str_replace_in_escaped(char **orig, const char *rep, const char *with) {
const gchar * escaped_with = g_strescape(with, NULL);
int len = strlen(with);

gchar * escaped_with = NULL;
escaped_with = (char*)calloc(len*2, sizeof(gchar));

json_escape(with, escaped_with);
char * result = str_replace_in(orig, rep, escaped_with);
g_free((char *) escaped_with);
return result;
Expand Down Expand Up @@ -460,12 +504,14 @@ static gboolean on_new_input ( GIOChannel *source, GIOCondition condition, gpoin
gboolean newline = FALSE;

GError * error = NULL;
gchar unichar;
gsize bytes_read;
gunichar unichar;
GIOStatus status;

status = g_io_channel_read_unichar(source, &unichar, &error);

g_io_channel_read_chars(source, &unichar, 1, &bytes_read, &error);
while(bytes_read > 0) {
g_string_append_c(buffer, unichar);
//when there is nothing to read, status is G_IO_STATUS_AGAIN
while(status == G_IO_STATUS_NORMAL) {
g_string_append_unichar(buffer, unichar);
if( unichar == '\n' ){
if(buffer->len > 1){ //input is not an empty line
g_debug("received new line: %s", buffer->str);
Expand All @@ -474,7 +520,7 @@ static gboolean on_new_input ( GIOChannel *source, GIOCondition condition, gpoin
}
g_string_set_size(buffer, 0);
}
g_io_channel_read_chars(source, &unichar, 1, &bytes_read, &error);
status = g_io_channel_read_unichar(source, &unichar, &error);
}

if(newline){
Expand Down