Skip to content

Commit

Permalink
Feature: Add the dashed style. (ArthurSonzogni#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurSonzogni authored Mar 15, 2023
1 parent 2dc95d6 commit 9efa0f7
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ current (development)
- Feature: Support `ResizableSplit` with customizable separator.
- Breaking: MenuDirection enum is renamed Direction

### Dom
- Feature: Add the dashed style for border and separator.

###
- Breaking: Direction enum is renamed WidthOrHeight
- Breaking: GaugeDirection enum is renamed Direction
Expand Down
1 change: 1 addition & 0 deletions examples/dom/border_style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ int main(int argc, const char* argv[]) {

auto document = vbox({
text("borderLight") | borderLight,
text("borderDashed") | borderDashed,
text("borderHeavy") | borderHeavy,
text("borderDouble") | borderDouble,
text("borderRounded") | borderRounded,
Expand Down
6 changes: 6 additions & 0 deletions examples/dom/separator_style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ int main(int argc, const char* argv[]) {
hbox(text("left"), separatorLight(), text("right")),
}) | borderLight,

vbox({
text("separatorDashed"),
separatorDashed(),
hbox(text("left"), separatorDashed(), text("right")),
}) | borderDashed,

vbox({
text("separatorHeavy"),
separatorHeavy(),
Expand Down
11 changes: 10 additions & 1 deletion include/ftxui/dom/elements.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ using Elements = std::vector<Element>;
using Decorator = std::function<Element(Element)>;
using GraphFunction = std::function<std::vector<int>(int, int)>;

enum BorderStyle { LIGHT, HEAVY, DOUBLE, ROUNDED, EMPTY };
enum BorderStyle {
LIGHT,
DASHED,
HEAVY,
DOUBLE,
ROUNDED,
EMPTY,
};

// Pipe elements into decorator togethers.
// For instance the next lines are equivalents:
Expand All @@ -37,6 +44,7 @@ Element text(std::string text);
Element vtext(std::string text);
Element separator();
Element separatorLight();
Element separatorDashed();
Element separatorHeavy();
Element separatorDouble();
Element separatorEmpty();
Expand All @@ -59,6 +67,7 @@ Element gaugeDown(float progress);
Element gaugeDirection(float progress, Direction direction);
Element border(Element);
Element borderLight(Element);
Element borderDashed(Element);
Element borderHeavy(Element);
Element borderDouble(Element);
Element borderRounded(Element);
Expand Down
54 changes: 48 additions & 6 deletions src/ftxui/dom/border.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
namespace ftxui {

using Charset = std::array<std::string, 6>; // NOLINT
using Charsets = std::array<Charset, 5>; // NOLINT
using Charsets = std::array<Charset, 6>; // NOLINT
// NOLINTNEXTLINE
static Charsets simple_border_charset = {
Charset{"", "", "", "", "", ""},
Charset{"", "", "", "", "", ""},
Charset{"", "", "", "", "", ""},
Charset{"", "", "", "", "", ""},
Charset{" ", " ", " ", " ", " ", " "},
Charset{"", "", "", "", "", ""}, // LIGHT
Charset{"", "", "", "", "", ""}, // DASHED
Charset{"", "", "", "", "", ""}, // HEAVY
Charset{"", "", "", "", "", ""}, // DOUBLE
Charset{"", "", "", "", "", ""}, // ROUNDED
Charset{" ", " ", " ", " ", " ", " "}, // EMPTY
};

// For reference, here is the charset for normal border:
Expand Down Expand Up @@ -173,6 +174,7 @@ class BorderPixel : public Node {
/// @ingroup dom
/// @see border
/// @see borderLight
/// @see borderDashed
/// @see borderDouble
/// @see borderHeavy
/// @see borderEmpty
Expand Down Expand Up @@ -223,6 +225,42 @@ Decorator borderStyled(BorderStyle style) {
/// @ingroup dom
/// @see border
/// @see borderLight
/// @see borderDashed
/// @see borderDouble
/// @see borderHeavy
/// @see borderRounded
/// @see borderEmpty
/// @see borderStyled
/// @see borderWith
///
/// Add a border around an element
///
/// ### Example
///
/// ```cpp
/// // Use 'borderDash' as a function...
/// Element document = borderDash(text("The element"));
///
/// // ...Or as a 'pipe'.
/// Element document = text("The element") | borderDAsh;
/// ```
///
/// ### Output
///
/// ```bash
/// ┏╍╍╍╍╍╍╍╍╍╍╍╍╍╍┓
/// ╏The element ╏
/// ┗╍╍╍╍╍╍╍╍╍╍╍╍╍╍┛
/// ```
Element borderDashed(Element child) {
return std::make_shared<Border>(unpack(std::move(child)), DASHED);
}

/// @brief Draw a dashed border around the element.
/// @ingroup dom
/// @see border
/// @see borderLight
/// @see borderDashed
/// @see borderDouble
/// @see borderHeavy
/// @see borderRounded
Expand Down Expand Up @@ -257,6 +295,7 @@ Element borderLight(Element child) {
/// @ingroup dom
/// @see border
/// @see borderLight
/// @see borderDashed
/// @see borderDouble
/// @see borderHeavy
/// @see borderRounded
Expand Down Expand Up @@ -291,6 +330,7 @@ Element borderHeavy(Element child) {
/// @ingroup dom
/// @see border
/// @see borderLight
/// @see borderDashed
/// @see borderDouble
/// @see borderHeavy
/// @see borderRounded
Expand Down Expand Up @@ -325,6 +365,7 @@ Element borderDouble(Element child) {
/// @ingroup dom
/// @see border
/// @see borderLight
/// @see borderDashed
/// @see borderDouble
/// @see borderHeavy
/// @see borderRounded
Expand Down Expand Up @@ -359,6 +400,7 @@ Element borderRounded(Element child) {
/// @ingroup dom
/// @see border
/// @see borderLight
/// @see borderDashed
/// @see borderDouble
/// @see borderHeavy
/// @see borderRounded
Expand Down
58 changes: 52 additions & 6 deletions src/ftxui/dom/separator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ namespace ftxui {

namespace {
using Charset = std::array<std::string, 2>; // NOLINT
using Charsets = std::array<Charset, 5>; // NOLINT
using Charsets = std::array<Charset, 6>; // NOLINT
// NOLINTNEXTLINE
const Charsets charsets = {
Charset{"", ""}, //
Charset{"", ""}, //
Charset{"", ""}, //
Charset{"", ""}, //
Charset{" ", " "}, //
Charset{"", ""}, // LIGHT
Charset{"", ""}, // DASHED
Charset{"", ""}, // HEAVY
Charset{"", ""}, // DOUBLE
Charset{"", ""}, // ROUNDED
Charset{" ", " "}, // EMPTY
};

} // namespace
Expand Down Expand Up @@ -98,6 +99,7 @@ class SeparatorWithPixel : public SeparatorAuto {
/// @ingroup dom
/// @see separator
/// @see separatorLight
/// @see separatorDashed
/// @see separatorDouble
/// @see separatorHeavy
/// @see separatorEmpty
Expand Down Expand Up @@ -135,6 +137,7 @@ Element separator() {
/// @ingroup dom
/// @see separator
/// @see separatorLight
/// @see separatorDashed
/// @see separatorDouble
/// @see separatorHeavy
/// @see separatorEmpty
Expand Down Expand Up @@ -171,6 +174,7 @@ Element separatorStyled(BorderStyle style) {
/// @ingroup dom
/// @see separator
/// @see separatorLight
/// @see separatorDashed
/// @see separatorDouble
/// @see separatorHeavy
/// @see separatorEmpty
Expand Down Expand Up @@ -202,11 +206,49 @@ Element separatorLight() {
return std::make_shared<SeparatorAuto>(LIGHT);
}

/// @brief Draw a vertical or horizontal separation in between two other
/// elements, using the DASHED style.
/// @ingroup dom
/// @see separator
/// @see separatorLight
/// @see separatorDashed
/// @see separatorDouble
/// @see separatorHeavy
/// @see separatorEmpty
/// @see separatorRounded
/// @see separatorStyled
/// @see separatorCharacter
///
/// Add a visual separation in between two elements.
///
/// ### Example
///
/// ```cpp
/// // Use 'border' as a function...
/// Element document = vbox({
/// text("up"),
/// separatorLight(),
/// text("down"),
/// });
/// ```
///
/// ### Output
///
/// ```bash
/// up
/// ╍╍╍╍
/// down
/// ```
Element separatorDashed() {
return std::make_shared<SeparatorAuto>(DASHED);
}

/// @brief Draw a vertical or horizontal separation in between two other
/// elements, using the HEAVY style.
/// @ingroup dom
/// @see separator
/// @see separatorLight
/// @see separatorDashed
/// @see separatorDouble
/// @see separatorHeavy
/// @see separatorEmpty
Expand Down Expand Up @@ -243,6 +285,7 @@ Element separatorHeavy() {
/// @ingroup dom
/// @see separator
/// @see separatorLight
/// @see separatorDashed
/// @see separatorDouble
/// @see separatorHeavy
/// @see separatorEmpty
Expand Down Expand Up @@ -279,6 +322,7 @@ Element separatorDouble() {
/// @ingroup dom
/// @see separator
/// @see separatorLight
/// @see separatorDashed
/// @see separatorDouble
/// @see separatorHeavy
/// @see separatorEmpty
Expand Down Expand Up @@ -316,6 +360,7 @@ Element separatorEmpty() {
/// @ingroup dom
/// @see separator
/// @see separatorLight
/// @see separatorDashed
/// @see separatorDouble
/// @see separatorHeavy
/// @see separatorEmpty
Expand Down Expand Up @@ -351,6 +396,7 @@ Element separatorCharacter(std::string value) {
/// @ingroup dom
/// @see separator
/// @see separatorLight
/// @see separatorDashed
/// @see separatorHeavy
/// @see separatorDouble
/// @see separatorStyled
Expand Down
14 changes: 14 additions & 0 deletions src/ftxui/dom/separator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ TEST(SeparatorTest, Light) {
"down");
}

TEST(SeparatorTest, Dashed) {
auto element = vbox({
text("top"),
separatorDashed(),
text("down"),
});
Screen screen(4, 3);
Render(screen, element);
EXPECT_EQ(screen.ToString(),
"top \r\n"
"╍╍╍╍\r\n"
"down");
}

TEST(SeparatorTest, Double) {
auto element = vbox({
text("top"),
Expand Down
13 changes: 7 additions & 6 deletions src/ftxui/dom/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ bool IsCell(int x, int y) {
}

// NOLINTNEXTLINE
static std::string charset[5][6] = {
{"", "", "", "", "", ""}, //
{"", "", "", "", "", ""}, //
{"", "", "", "", "", ""}, //
{"", "", "", "", "", ""}, //
{" ", " ", " ", " ", " ", " "}, //
static std::string charset[6][6] = {
{"", "", "", "", "", ""}, // LIGHT
{"", "", "", "", "", ""}, // DASHED
{"", "", "", "", "", ""}, // HEAVY
{"", "", "", "", "", ""}, // DOUBLE
{"", "", "", "", "", ""}, // ROUNDED
{" ", " ", " ", " ", " ", " "}, // EMPTY
};

int Wrap(int input, int modulo) {
Expand Down
2 changes: 2 additions & 0 deletions src/ftxui/screen/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,11 @@ struct TileEncoding {
const std::map<std::string, TileEncoding> tile_encoding = { // NOLINT
{"", {1, 0, 1, 0, 0}},
{"", {2, 0, 2, 0, 0}},
{"", {2, 0, 2, 0, 0}},

{"", {0, 1, 0, 1, 0}},
{"", {0, 2, 0, 2, 0}},
{"", {0, 2, 0, 2, 0}},

{"", {0, 0, 1, 1, 0}},
{"", {0, 0, 2, 1, 0}},
Expand Down

0 comments on commit 9efa0f7

Please sign in to comment.