diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2020-09-20 16:41:04 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-09-27 21:14:18 +0200 |
commit | aa65f664a97abca040663768936b59fa29c2a9e6 (patch) | |
tree | 4d4b23a7b5e93bcdd00c5afb0632da9e058582fd /Userland/md.cpp | |
parent | 5fbec2b003081f0b444f88b18e3250a96dbdd13f (diff) | |
download | serenity-aa65f664a97abca040663768936b59fa29c2a9e6.zip |
LibMarkdown: Take a 'view_width' argument for render_for_terminal()
Some constructs will require the width of the terminal (or a general
'width') to be rendered correctly, such as tables.
Diffstat (limited to 'Userland/md.cpp')
-rw-r--r-- | Userland/md.cpp | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/Userland/md.cpp b/Userland/md.cpp index f7dc74349b..2a608e05dc 100644 --- a/Userland/md.cpp +++ b/Userland/md.cpp @@ -32,22 +32,38 @@ #include <LibMarkdown/Document.h> #include <stdio.h> #include <string.h> +#include <sys/ioctl.h> +#include <unistd.h> int main(int argc, char* argv[]) { - if (pledge("stdio rpath", nullptr) < 0) { + if (pledge("stdio rpath tty", nullptr) < 0) { perror("pledge"); return 1; } const char* file_name = nullptr; bool html = false; + int view_width = 0; Core::ArgsParser args_parser; args_parser.add_option(html, "Render to HTML rather than for the terminal", "html", 'H'); + args_parser.add_option(view_width, "Viewport width for the terminal (defaults to current terminal width)", "view-width", 0, "width"); args_parser.add_positional_argument(file_name, "Path to Markdown file", "path", Core::ArgsParser::Required::No); args_parser.parse(argc, argv); + if (!html && view_width == 0) { + if (isatty(STDOUT_FILENO)) { + struct winsize ws; + if (ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) < 0) + view_width = 80; + else + view_width = ws.ws_col; + + } else { + view_width = 80; + } + } auto file = Core::File::construct(); bool success; if (file_name == nullptr) { @@ -77,6 +93,6 @@ int main(int argc, char* argv[]) return 1; } - String res = html ? document->render_to_html() : document->render_for_terminal(); + String res = html ? document->render_to_html() : document->render_for_terminal(view_width); printf("%s", res.characters()); } |