-
Notifications
You must be signed in to change notification settings - Fork 0
/
hr17.cpp
65 lines (54 loc) · 1.17 KB
/
hr17.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* hr.cpp - writes horizontal bar to terminal
* (c) Copyright 2019-2020 Bartosz Mierzynski
* Written in C++17
*/
#include <iostream>
#include <cstring>
#ifdef __WIN32__
#include <windows.h>
#else
#include <sys/ioctl.h>
#endif /* __WIN32__ */
using std::cout, std::endl, std::strlen;
void get_columns(int &n) {
#ifdef __WIN32__
const char *columns;
CONSOLE_SCREEN_BUFFER_INFO csbi;
if((columns = getenv("COLUMNS")) && (strlen(columns)))
n = atoi(columns);
else
if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
n = csbi.dwSize.X;
else
n = 80;
#else
const char *columns;
struct winsize w;
if((columns = getenv("COLUMNS")) && (strlen(columns))) {
n = atoi(columns);
} else {
ioctl(0, TIOCGWINSZ, &w);
n = w.ws_col > 0 ? w.ws_col : 80;
}
#endif /* __WIN32__ */
}
void hr1(int n = 80) {
while(n--)
cout.put('#');
endl(cout);
}
void hr2(int n = 80, const char *s = "#") {
auto len = strlen(s);
for(auto i = 0; i < n; ++i)
cout.put(s[i%len]);
endl(cout);
}
int main(int argc, char *argv[]) {
auto n = 0;
get_columns(n);
if(argc < 2)
hr1(n);
else
for(--argc, ++argv; argc; --argc, ++argv)
hr2(n, *argv);
}