Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d9dbee9993 | ||
|
|
740445f425 | ||
|
|
f849e85e9f | ||
|
|
628b131741 | ||
|
|
fb3d6c319b | ||
|
|
04b8a6dcda |
@@ -29,6 +29,7 @@ src = [
|
|||||||
'src/util/process.c',
|
'src/util/process.c',
|
||||||
'src/util/strbuf.c',
|
'src/util/strbuf.c',
|
||||||
'src/util/str_util.c',
|
'src/util/str_util.c',
|
||||||
|
'src/util/term.c',
|
||||||
'src/util/thread.c',
|
'src/util/thread.c',
|
||||||
'src/util/tick.c',
|
'src/util/tick.c',
|
||||||
]
|
]
|
||||||
@@ -189,6 +190,7 @@ if get_option('buildtype') == 'debug'
|
|||||||
'src/options.c',
|
'src/options.c',
|
||||||
'src/util/strbuf.c',
|
'src/util/strbuf.c',
|
||||||
'src/util/str_util.c',
|
'src/util/str_util.c',
|
||||||
|
'src/util/term.c',
|
||||||
]],
|
]],
|
||||||
['test_clock', [
|
['test_clock', [
|
||||||
'tests/test_clock.c',
|
'tests/test_clock.c',
|
||||||
|
|||||||
@@ -4,12 +4,14 @@
|
|||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
#include "util/strbuf.h"
|
#include "util/strbuf.h"
|
||||||
#include "util/str_util.h"
|
#include "util/str_util.h"
|
||||||
|
#include "util/term.h"
|
||||||
|
|
||||||
#define STR_IMPL_(x) #x
|
#define STR_IMPL_(x) #x
|
||||||
#define STR(x) STR_IMPL_(x)
|
#define STR(x) STR_IMPL_(x)
|
||||||
@@ -312,7 +314,7 @@ static const struct sc_option options[] = {
|
|||||||
"\"lsuper\" and \"rsuper\".\n"
|
"\"lsuper\" and \"rsuper\".\n"
|
||||||
"A shortcut can consist in several keys, separated by '+'. "
|
"A shortcut can consist in several keys, separated by '+'. "
|
||||||
"Several shortcuts can be specified, separated by ','.\n"
|
"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"
|
"shortcuts, pass \"lctrl+lalt,lsuper\".\n"
|
||||||
"Default is \"lalt,lsuper\" (left-Alt or left-Super).",
|
"Default is \"lalt,lsuper\" (left-Alt or left-Super).",
|
||||||
},
|
},
|
||||||
@@ -680,7 +682,7 @@ error:
|
|||||||
|
|
||||||
static void
|
static void
|
||||||
print_option_usage(const struct sc_option *opt, unsigned cols) {
|
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) {
|
if (!opt->text) {
|
||||||
// Option not documented in help (for example because it is deprecated)
|
// Option not documented in help (for example because it is deprecated)
|
||||||
@@ -716,7 +718,7 @@ print_shortcuts_intro(unsigned cols) {
|
|||||||
|
|
||||||
static void
|
static void
|
||||||
print_shortcut(const struct sc_shortcut *shortcut, unsigned cols) {
|
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->shortcuts[0]); // At least one shortcut
|
||||||
assert(shortcut->text);
|
assert(shortcut->text);
|
||||||
|
|
||||||
@@ -740,17 +742,30 @@ print_shortcut(const struct sc_shortcut *shortcut, unsigned cols) {
|
|||||||
|
|
||||||
void
|
void
|
||||||
scrcpy_print_usage(const char *arg0) {
|
scrcpy_print_usage(const char *arg0) {
|
||||||
|
#define SC_TERM_COLS_DEFAULT 80
|
||||||
|
unsigned cols;
|
||||||
|
|
||||||
|
if (!isatty(STDERR_FILENO)) {
|
||||||
|
// Not a tty, use a default value
|
||||||
|
cols = SC_TERM_COLS_DEFAULT;
|
||||||
|
} else {
|
||||||
|
bool ok = sc_term_get_size(NULL, &cols);
|
||||||
|
if (!ok) {
|
||||||
|
cols = SC_TERM_COLS_DEFAULT; // default value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fprintf(stderr, "Usage: %s [options]\n\n"
|
fprintf(stderr, "Usage: %s [options]\n\n"
|
||||||
"Options:\n", arg0);
|
"Options:\n", arg0);
|
||||||
for (size_t i = 0; i < ARRAY_LEN(options); ++i) {
|
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
|
// Print shortcuts section
|
||||||
fprintf(stderr, "\nShortcuts:\n\n");
|
fprintf(stderr, "\nShortcuts:\n\n");
|
||||||
print_shortcuts_intro(80);
|
print_shortcuts_intro(cols);
|
||||||
for (size_t i = 0; i < ARRAY_LEN(shortcuts); ++i) {
|
for (size_t i = 0; i < ARRAY_LEN(shortcuts); ++i) {
|
||||||
print_shortcut(&shortcuts[i], 80);
|
print_shortcut(&shortcuts[i], cols);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -213,6 +213,8 @@ utf8_from_wide_char(const wchar_t *ws) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
char *wrap_lines(const char *input, unsigned columns, unsigned indent) {
|
char *wrap_lines(const char *input, unsigned columns, unsigned indent) {
|
||||||
|
assert(indent < columns);
|
||||||
|
|
||||||
struct sc_strbuf buf;
|
struct sc_strbuf buf;
|
||||||
|
|
||||||
// The output string should not be a lot longer than the input string (just
|
// 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(indent < columns);
|
|
||||||
|
|
||||||
#define APPEND(S,N) if (!sc_strbuf_append(&buf, S, N)) goto error
|
#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_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
|
#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;
|
size_t col = indent;
|
||||||
while (*input) {
|
while (*input) {
|
||||||
size_t sep_idx = strcspn(input, "\n ");
|
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];
|
char sep = input[sep_idx];
|
||||||
if (sep == ' ')
|
if (sep == ' ')
|
||||||
|
|||||||
51
app/src/util/term.c
Normal file
51
app/src/util/term.c
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#include "term.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
# include <windows.h>
|
||||||
|
#else
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <sys/ioctl.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_term_get_size(unsigned *rows, unsigned *cols) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||||
|
|
||||||
|
bool ok =
|
||||||
|
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
|
||||||
|
if (!ok) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rows) {
|
||||||
|
assert(csbi.srWindow.Bottom >= csbi.srWindow.Top);
|
||||||
|
*rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cols) {
|
||||||
|
assert(csbi.srWindow.Right >= csbi.srWindow.Left);
|
||||||
|
*cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
#else
|
||||||
|
struct winsize ws;
|
||||||
|
int r = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
|
||||||
|
if (r == -1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rows) {
|
||||||
|
*rows = ws.ws_row;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cols) {
|
||||||
|
*cols = ws.ws_col;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
11
app/src/util/term.h
Normal file
11
app/src/util/term.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef SC_TERM_H
|
||||||
|
#define SC_TERM_H
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
bool
|
||||||
|
sc_term_get_size(unsigned *rows, unsigned *cols);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -301,7 +301,7 @@ static void test_strlist_contains(void) {
|
|||||||
|
|
||||||
static void test_wrap_lines(void) {
|
static void test_wrap_lines(void) {
|
||||||
const char *s = "This is a text to test line wrapping. The lines must be "
|
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"
|
"\n"
|
||||||
"This rectangle must remains a rectangle because it is "
|
"This rectangle must remains a rectangle because it is "
|
||||||
"drawn in lines having lengths lower than the specified "
|
"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"
|
const char *expected = " This is a text to\n"
|
||||||
" test line wrapping.\n"
|
" test line wrapping.\n"
|
||||||
" The lines must be\n"
|
" The lines must be\n"
|
||||||
" wrapped either at a\n"
|
" wrapped at a space\n"
|
||||||
" space or a line\n"
|
" or a line break.\n"
|
||||||
" break.\n"
|
|
||||||
" \n"
|
" \n"
|
||||||
" This rectangle must\n"
|
" This rectangle must\n"
|
||||||
" remains a rectangle\n"
|
" remains a rectangle\n"
|
||||||
|
|||||||
Reference in New Issue
Block a user