Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:pass rendered before indentation #66

Closed
lread opened this issue Sep 28, 2019 · 4 comments · Fixed by #69
Closed

:pass rendered before indentation #66

lread opened this issue Sep 28, 2019 · 4 comments · Fixed by #69

Comments

@lread
Copy link
Contributor

lread commented Sep 28, 2019

Hello fippsters! Thanks for your cool tool and all the hard work you've put into it!

I am new to fipp so I could very well be misunderstanding how to express myself in fipp and/or desired vs actual behavior, but here I go anyway:

Given the following code:

(require '[fipp.engine :as fe])

(fe/pprint-document
   [:group
    [:span [:pass "<"] "AA" [:pass ">"]]
    [:align
     :line
     [:span [:pass "<"] "BB" [:pass ">"]]
     :line
     [:span [:pass "<"] "CC" [:pass ">"]]
     [:align
      :line
      [:span [:pass "<"] "DD" [:pass ">"]]]]]
   {:width 6})

I get the following output:

<AA>
<  BB>
<  CC>
<    DD>

When I was more expecting something like:

<AA>
  <BB>
  <CC>
    <DD>

I noticed this while exploring using background colors in puget: greglook/puget#44.

@brandonbloom
Copy link
Owner

Very interesting! Thanks for the report.

First, I want to make clear that putting visible characters in a :pass node is a definite no-no, except for debugging as you're doing here. Very clever. I would not have thought of that.

Second, I think you've indeed found a bug. Or at the very least, a design oversight. Look at the pass node logic here:

fipp/src/fipp/engine.cljc

Lines 195 to 196 in 29f894c

:pass
(rf res (:text node))
and compare to the text node logic just above:

fipp/src/fipp/engine.cljc

Lines 180 to 186 in 29f894c

(let [text (:text node)
res* (if (zero? @column)
(do (vswap! column + indent)
(rf res (apply str (repeat indent \space))))
res)]
(vswap! column + (count text))
(rf res* text))

The pass nodes don't handle producing the indent and instead write directly at column zero when that's the current position. I think it would be A-OK to copy/paste that logic down to the pass codepath. The rule would then be that passthrough text is still subject to the normal indenting policies. If you wanted to highlight the background of the indent, you'd need to insert your color commands prior to the newline character.

I think the code would be:

:pass
  (let [text (:text node)
        res* (if (zero? @column)
               (do (vswap! column + indent)
                   (rf res (apply str (repeat indent \space))))
               res)]
    (rf res* text))

The only difference between the text and pass node cases would be that pass skips the (vswap! column + (count text)) step. This is similar to how the escape nodes treat multiple bytes as a single byte by doing that step as (vswap! column inc). Seems pretty safe to do exactly the same logic, but with size 0. In fact, may be time those three things become a local function (fn [text width] ...).

If you want to give that a try and maybe add a test case, I'd be happy to accept that PR.

@lread
Copy link
Contributor Author

lread commented Sep 30, 2019

Thanks so much for the thoughtful reply @brandonbloom!

Yes, I should have made clear that I was :passing visible chars for illustrative purposes. I actually took the inspiration from some comment code I found here:

fipp/src/fipp/engine.cljc

Lines 292 to 294 in 29f894c

(pprint-document
[:group [:pass "<"] "AB" [:pass ">"] :line "B" :line "C"]
{:width 6}))

It was very kind of you to walkthrough and explain the code, I would be happy to take a stab at a PR.

@brandonbloom
Copy link
Owner

Ha! I said I would never have thought of that, but apparently I had thought of it previously :)

@lread
Copy link
Contributor Author

lread commented Oct 1, 2019

Hey, at least there's comfort that you found it very clever! :-)

lread added a commit to lread/fipp that referenced this issue Oct 3, 2019
lread added a commit to lread/fipp that referenced this issue Oct 3, 2019
brandonbloom pushed a commit that referenced this issue Oct 5, 2019
Followed my understanding of guidance provided by @brandonbloom:
#66 (comment)

Closes #66
lread added a commit to lread/puget that referenced this issue Oct 6, 2019
Bumped fipp to 0.6.21 to pick up brandonbloom/fipp#66
which resolves greglook#44.

To verify, beefed up ansi unit tests to include a test that almost matches
example in puget README and a test that clearly shows indentation.

Of note: While developing these tests I used kaocha because it has excellent
expected vs actual diff reporting. Because kaocha uses puget, I figured it might
not be the best idea to use kaocha for puget testing and therefore have not
proposed to include it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants