Compare commits

..

5 Commits

Author SHA1 Message Date
Romain Vimont
315f467cc7 winsize 2021-11-07 23:08:09 +01:00
Romain Vimont
e98122c110 Generate getopt params from option structures
Use the option descriptions to generate the optstring and longopts
parameters for the getopt_long() command.

That way, the options are completely described in a single place.
2021-11-07 23:08:09 +01:00
Romain Vimont
0c5d5b6339 Structure shortcuts help 2021-11-07 23:08:09 +01:00
Romain Vimont
fb3d6c319b Structure command line options help
It will allow to correctly print the help for the current terminal size
(even if for now it is hardcoded to 80 columns).
2021-11-07 23:08:09 +01:00
Romain Vimont
04b8a6dcda Add line wrapper
Add a tool to wrap lines at words boundaries (spaces) to fit in a
specified number of columns, left-indented by a specified number of
spaces.
2021-11-07 23:08:08 +01:00
3 changed files with 30 additions and 13 deletions

View File

@@ -11,6 +11,9 @@
#include "util/strbuf.h"
#include "util/str_util.h"
#include <termios.h>
#include <sys/ioctl.h>
#define STR_IMPL_(x) #x
#define STR(x) STR_IMPL_(x)
@@ -312,7 +315,7 @@ static const struct sc_option options[] = {
"\"lsuper\" and \"rsuper\".\n"
"A shortcut can consist in several keys, separated by '+'. "
"Several shortcuts can be specified, separated by ','.\n"
"For example, to use either LCtrl+LAlt or LSuper for scrcpy\n"
"For example, to use either LCtrl+LAlt or LSuper for scrcpy "
"shortcuts, pass \"lctrl+lalt,lsuper\".\n"
"Default is \"lalt,lsuper\" (left-Alt or left-Super).",
},
@@ -680,7 +683,7 @@ error:
static void
print_option_usage(const struct sc_option *opt, unsigned cols) {
assert(cols > 20); // We need some space
assert(cols > 8); // wrap_lines() requires indent < columns
if (!opt->text) {
// Option not documented in help (for example because it is deprecated)
@@ -716,7 +719,7 @@ print_shortcuts_intro(unsigned cols) {
static void
print_shortcut(const struct sc_shortcut *shortcut, unsigned cols) {
assert(cols > 20); // We need some space
assert(cols > 8); // wrap_lines() requires indent < columns
assert(shortcut->shortcuts[0]); // At least one shortcut
assert(shortcut->text);
@@ -740,17 +743,27 @@ print_shortcut(const struct sc_shortcut *shortcut, unsigned cols) {
void
scrcpy_print_usage(const char *arg0) {
unsigned cols = 80;
struct winsize ws;
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) != -1) {
cols = ws.ws_col;
if (cols < 20) {
cols = 20;
}
}
fprintf(stderr, "Usage: %s [options]\n\n"
"Options:\n", arg0);
for (size_t i = 0; i < ARRAY_LEN(options); ++i) {
print_option_usage(&options[i], 80);
print_option_usage(&options[i], cols);
}
// Print shortcuts section
fprintf(stderr, "\nShortcuts:\n\n");
print_shortcuts_intro(80);
print_shortcuts_intro(cols);
for (size_t i = 0; i < ARRAY_LEN(shortcuts); ++i) {
print_shortcut(&shortcuts[i], 80);
print_shortcut(&shortcuts[i], cols);
}
}

View File

@@ -213,6 +213,8 @@ utf8_from_wide_char(const wchar_t *ws) {
#endif
char *wrap_lines(const char *input, unsigned columns, unsigned indent) {
assert(indent < columns);
struct sc_strbuf buf;
// The output string should not be a lot longer than the input string (just
@@ -224,8 +226,6 @@ char *wrap_lines(const char *input, unsigned columns, unsigned indent) {
return false;
}
assert(indent < columns);
#define APPEND(S,N) if (!sc_strbuf_append(&buf, S, N)) goto error
#define APPEND_CHAR(C) if (!sc_strbuf_append_char(&buf, C)) goto error
#define APPEND_N(C,N) if (!sc_strbuf_append_n(&buf, C, N)) goto error
@@ -241,7 +241,12 @@ char *wrap_lines(const char *input, unsigned columns, unsigned indent) {
size_t col = indent;
while (*input) {
size_t sep_idx = strcspn(input, "\n ");
bool wrap = col + sep_idx > columns;
size_t new_col = col + sep_idx;
if (pending == ' ') {
// The pending space counts
++new_col;
}
bool wrap = new_col > columns;
char sep = input[sep_idx];
if (sep == ' ')

View File

@@ -301,7 +301,7 @@ static void test_strlist_contains(void) {
static void test_wrap_lines(void) {
const char *s = "This is a text to test line wrapping. The lines must be "
"wrapped either at a space or a line break.\n"
"wrapped at a space or a line break.\n"
"\n"
"This rectangle must remains a rectangle because it is "
"drawn in lines having lengths lower than the specified "
@@ -315,9 +315,8 @@ static void test_wrap_lines(void) {
const char *expected = " This is a text to\n"
" test line wrapping.\n"
" The lines must be\n"
" wrapped either at a\n"
" space or a line\n"
" break.\n"
" wrapped at a space\n"
" or a line break.\n"
" \n"
" This rectangle must\n"
" remains a rectangle\n"