summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2020-09-20 16:41:04 +0430
committerAndreas Kling <kling@serenityos.org>2020-09-27 21:14:18 +0200
commitaa65f664a97abca040663768936b59fa29c2a9e6 (patch)
tree4d4b23a7b5e93bcdd00c5afb0632da9e058582fd
parent5fbec2b003081f0b444f88b18e3250a96dbdd13f (diff)
downloadserenity-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.
-rw-r--r--Libraries/LibMarkdown/Block.h2
-rw-r--r--Libraries/LibMarkdown/CodeBlock.cpp2
-rw-r--r--Libraries/LibMarkdown/CodeBlock.h2
-rw-r--r--Libraries/LibMarkdown/Document.cpp4
-rw-r--r--Libraries/LibMarkdown/Document.h2
-rw-r--r--Libraries/LibMarkdown/Heading.cpp2
-rw-r--r--Libraries/LibMarkdown/Heading.h2
-rw-r--r--Libraries/LibMarkdown/List.cpp2
-rw-r--r--Libraries/LibMarkdown/List.h2
-rw-r--r--Libraries/LibMarkdown/Paragraph.cpp2
-rw-r--r--Libraries/LibMarkdown/Paragraph.h2
-rw-r--r--Userland/md.cpp20
12 files changed, 30 insertions, 14 deletions
diff --git a/Libraries/LibMarkdown/Block.h b/Libraries/LibMarkdown/Block.h
index b1d96146c4..239c473165 100644
--- a/Libraries/LibMarkdown/Block.h
+++ b/Libraries/LibMarkdown/Block.h
@@ -36,7 +36,7 @@ public:
virtual ~Block() { }
virtual String render_to_html() const = 0;
- virtual String render_for_terminal() const = 0;
+ virtual String render_for_terminal(size_t view_width = 0) const = 0;
};
}
diff --git a/Libraries/LibMarkdown/CodeBlock.cpp b/Libraries/LibMarkdown/CodeBlock.cpp
index 16703853fa..b6501ad03c 100644
--- a/Libraries/LibMarkdown/CodeBlock.cpp
+++ b/Libraries/LibMarkdown/CodeBlock.cpp
@@ -74,7 +74,7 @@ String CodeBlock::render_to_html() const
return builder.build();
}
-String CodeBlock::render_for_terminal() const
+String CodeBlock::render_for_terminal(size_t) const
{
StringBuilder builder;
diff --git a/Libraries/LibMarkdown/CodeBlock.h b/Libraries/LibMarkdown/CodeBlock.h
index 5b5159de6d..fb12fb4b65 100644
--- a/Libraries/LibMarkdown/CodeBlock.h
+++ b/Libraries/LibMarkdown/CodeBlock.h
@@ -42,7 +42,7 @@ public:
virtual ~CodeBlock() override { }
virtual String render_to_html() const override;
- virtual String render_for_terminal() const override;
+ virtual String render_for_terminal(size_t view_width = 0) const override;
static OwnPtr<CodeBlock> parse(Vector<StringView>::ConstIterator& lines);
private:
diff --git a/Libraries/LibMarkdown/Document.cpp b/Libraries/LibMarkdown/Document.cpp
index 6958ce8229..922dda5b37 100644
--- a/Libraries/LibMarkdown/Document.cpp
+++ b/Libraries/LibMarkdown/Document.cpp
@@ -52,12 +52,12 @@ String Document::render_to_html() const
return builder.build();
}
-String Document::render_for_terminal() const
+String Document::render_for_terminal(size_t view_width) const
{
StringBuilder builder;
for (auto& block : m_blocks) {
- auto s = block.render_for_terminal();
+ auto s = block.render_for_terminal(view_width);
builder.append(s);
}
diff --git a/Libraries/LibMarkdown/Document.h b/Libraries/LibMarkdown/Document.h
index 259aa09046..752cd12f4c 100644
--- a/Libraries/LibMarkdown/Document.h
+++ b/Libraries/LibMarkdown/Document.h
@@ -35,7 +35,7 @@ namespace Markdown {
class Document final {
public:
String render_to_html() const;
- String render_for_terminal() const;
+ String render_for_terminal(size_t view_width = 0) const;
static OwnPtr<Document> parse(const StringView&);
diff --git a/Libraries/LibMarkdown/Heading.cpp b/Libraries/LibMarkdown/Heading.cpp
index c489f1e603..e0374c97c9 100644
--- a/Libraries/LibMarkdown/Heading.cpp
+++ b/Libraries/LibMarkdown/Heading.cpp
@@ -38,7 +38,7 @@ String Heading::render_to_html() const
return builder.build();
}
-String Heading::render_for_terminal() const
+String Heading::render_for_terminal(size_t) const
{
StringBuilder builder;
diff --git a/Libraries/LibMarkdown/Heading.h b/Libraries/LibMarkdown/Heading.h
index 8988bc0f88..f68ebd2cd3 100644
--- a/Libraries/LibMarkdown/Heading.h
+++ b/Libraries/LibMarkdown/Heading.h
@@ -44,7 +44,7 @@ public:
virtual ~Heading() override { }
virtual String render_to_html() const override;
- virtual String render_for_terminal() const override;
+ virtual String render_for_terminal(size_t view_width = 0) const override;
static OwnPtr<Heading> parse(Vector<StringView>::ConstIterator& lines);
private:
diff --git a/Libraries/LibMarkdown/List.cpp b/Libraries/LibMarkdown/List.cpp
index 52bc2816fe..2f043ea59c 100644
--- a/Libraries/LibMarkdown/List.cpp
+++ b/Libraries/LibMarkdown/List.cpp
@@ -47,7 +47,7 @@ String List::render_to_html() const
return builder.build();
}
-String List::render_for_terminal() const
+String List::render_for_terminal(size_t) const
{
StringBuilder builder;
diff --git a/Libraries/LibMarkdown/List.h b/Libraries/LibMarkdown/List.h
index 50ac609ef6..b853ccb801 100644
--- a/Libraries/LibMarkdown/List.h
+++ b/Libraries/LibMarkdown/List.h
@@ -43,7 +43,7 @@ public:
virtual ~List() override { }
virtual String render_to_html() const override;
- virtual String render_for_terminal() const override;
+ virtual String render_for_terminal(size_t view_width = 0) const override;
static OwnPtr<List> parse(Vector<StringView>::ConstIterator& lines);
diff --git a/Libraries/LibMarkdown/Paragraph.cpp b/Libraries/LibMarkdown/Paragraph.cpp
index 764e555fbc..f44da06fbb 100644
--- a/Libraries/LibMarkdown/Paragraph.cpp
+++ b/Libraries/LibMarkdown/Paragraph.cpp
@@ -38,7 +38,7 @@ String Paragraph::render_to_html() const
return builder.build();
}
-String Paragraph::render_for_terminal() const
+String Paragraph::render_for_terminal(size_t) const
{
StringBuilder builder;
builder.append(m_text.render_for_terminal());
diff --git a/Libraries/LibMarkdown/Paragraph.h b/Libraries/LibMarkdown/Paragraph.h
index 39c86624cb..d5e63a03c2 100644
--- a/Libraries/LibMarkdown/Paragraph.h
+++ b/Libraries/LibMarkdown/Paragraph.h
@@ -41,7 +41,7 @@ public:
virtual ~Paragraph() override { }
virtual String render_to_html() const override;
- virtual String render_for_terminal() const override;
+ virtual String render_for_terminal(size_t view_width = 0) const override;
static OwnPtr<Paragraph> parse(Vector<StringView>::ConstIterator& lines);
private:
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());
}