Compare commits

..

4 Commits

Author SHA1 Message Date
Romain Vimont
d9dbee9993 Adapt help to terminal size
If the error stream is a terminal, and we can retrieve the terminal
size, wrap the lines at terminal boundaries.
2021-11-11 11:34:39 +01:00
Romain Vimont
740445f425 Add util function to get terminal size 2021-11-11 11:34:35 +01:00
Romain Vimont
f849e85e9f 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-11 11:34:35 +01:00
Romain Vimont
628b131741 Structure shortcuts help 2021-11-11 11:34:35 +01:00
4 changed files with 75 additions and 9 deletions

View File

@@ -29,6 +29,7 @@ src = [
'src/util/process.c',
'src/util/strbuf.c',
'src/util/str_util.c',
'src/util/term.c',
'src/util/thread.c',
'src/util/tick.c',
]
@@ -189,6 +190,7 @@ if get_option('buildtype') == 'debug'
'src/options.c',
'src/util/strbuf.c',
'src/util/str_util.c',
'src/util/term.c',
]],
['test_clock', [
'tests/test_clock.c',

View File

@@ -4,15 +4,14 @@
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "options.h"
#include "util/log.h"
#include "util/strbuf.h"
#include "util/str_util.h"
#include <termios.h>
#include <sys/ioctl.h>
#include "util/term.h"
#define STR_IMPL_(x) #x
#define STR(x) STR_IMPL_(x)
@@ -743,13 +742,16 @@ print_shortcut(const struct sc_shortcut *shortcut, unsigned cols) {
void
scrcpy_print_usage(const char *arg0) {
unsigned cols = 80;
#define SC_TERM_COLS_DEFAULT 80
unsigned cols;
struct winsize ws;
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) != -1) {
cols = ws.ws_col;
if (cols < 20) {
cols = 20;
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
}
}

51
app/src/util/term.c Normal file
View 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
View 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