-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
nix flake show: add the description if it exists #10980
Conversation
This has been really helpful for my team. What do I need to do to get it merged? |
The problem with showing descriptions in |
@edolstra I can think of a few approaches:
I can tackle either one if I get some direction. |
ff1c83b
to
496e6d5
Compare
I've pushed a change to stop at the first new line |
I'm not quite sure how to test the output. The tests in |
As a bystander, I think the following implementation might make the most sense:
You could try using |
fdafe03
to
868f759
Compare
I chose 80 and added tests. |
@edolstra is this a reasonable solution to the tree rendering? |
74da3f4
to
1cc808c
Compare
@Ericson2314 is there any thing I can do to move this along? |
(cherry picked from commit 8cd1d02f90eb9915e640c5d370d919fad9833c65) nix flake show: Only print up to the first new line if it exists. (cherry picked from commit 5281a44927bdb51bfe6e5de12262d815c98f6fe7) add tests (cherry picked from commit 74ae0fbdc70a5079a527fe143c4832d1357011f7) Handle long strings, embedded new lines and empty descriptions (cherry picked from commit 2ca7b3afdbbd983173a17fa0a822cf7623601367) Account for total length of 80 (cherry picked from commit 1cc808c18cbaaf26aaae42bb1d7f7223f25dd364) docs: add nix flake show description release note fix: remove white space nix flake show: trim length based on terminal size test: account for terminal size docs(flake-description): before and after commands; add myself to credits Upstream-PR: NixOS/nix#10980 Change-Id: Ie1c667dc816b3dd81e65a1f5395e57ea48ee0362
It looks like |
src/nix/flake.cc
Outdated
name); | ||
"package"; | ||
if (description && !description->empty()) { | ||
// Trim the string and only display the first line of the description. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be done using filterANSIEscapes()
:
/**
* Truncate a string to 'width' printable characters. If 'filterAll'
* is true, all ANSI escape sequences are filtered out. Otherwise,
* some escape sequences (such as colour setting) are copied but not
* included in the character count. Also, tabs are expanded to
* spaces.
*/
std::string filterANSIEscapes(std::string_view s,
bool filterAll = false,
unsigned int width = std::numeric_limits<unsigned int>::max());
I.e. something like filterANSIEscapes(s, false, getWindowSize().second())
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@edolstra At this point getWindowSize()
returns 0,0. I tried calling updateWindowSize()
manually but I keep getting 0,0 in the test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be running headless in tests. Does it work when calling it interactive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, I see values if I run nix flake show
on the console. Do I need to provide a maximum length for headless mode? If so then what is the recommended way to detect it (isTTY
?)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty nice would be to output relative to current terminal? (Or 80-ish if headless)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty nice would be to output relative to current terminal? (Or 80-ish if headless)
I think that's what the getWindowSize
approach is supposed to do unless I'm misunderstanding something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly, I was thinking that it would call getWindowSize, if 0, use 80-ish, otherwise use what was returned. Be careful about some pathological tiny windows!
1cc808c
to
1c5f1de
Compare
I think the macos test failure is spurious |
I tried this out and I still get overflow. Am I reading the logic wrong? Shouldn't the window size limitation be on the entire string rather than only the
|
I guess it could be. I was thinking it should just be on the description but maybe it should be on the whole string but we should only cut off the description if too long to match existing behavior |
src/nix/flake.cc
Outdated
auto line = beginningOfLine + restOfLine; | ||
// FIXME: Specifying `true` here gives the correct length | ||
// BUT removes colors/bold so something is not quite right here. | ||
line = filterANSIEscapes(line, true, maxLength); | ||
|
||
// NOTE: This test might be incorrect since I get things like: | ||
// 168 or 161 > maxLength. | ||
if (line.length() > maxLength) { | ||
line = line.replace(line.length() - 3, 3, "..."); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the FIXME:
here. I think this is pretty close but it's not 100% correct yet. filterANSIEscapes(..., true)
seems to give a reasonable character count but I'm losing color information. filterANSIEscapes(..., false)
gives funky length values so I think I'm missing something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there might be a known number of escapes that you need to correct for, but then also use the original line (plus some terminating ANSI codes). Perhaps do:
- check length without codes
- start with string with codes
- subtract the difference from the max
- subtract a additional known amount as correction
- pad with a "reset" ANSI sequence
- ????
- Happiness
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is all fairly terrible. The filtering function definitely doesn't strip ascii codes out and this all depends on the nested level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a less perfect solution we could implement for now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice if filterANSIEscapes
filtered out color codes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new version uses a modified version of filterANSIEscapes
with the tree characters taken into account. It's kind of ugly but seems to work!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is all fairly terrible.
Absolutely. Terminal color logic, layout logic and flake attribute logic should all be separated out into distinct functions, classes, etc.
However, since we know the solution for the tech debt and we're not dealing with crazy compatibility requirements in this part of the code, I'd be happy to merge this and do the refactoring after.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be great
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@roberth Are we good to go as-is?
Tested and works as expected. Thanks! |
Revert "Merge pull request #10980 from kjeremy/flake-show-description"
Motivation
Displays the description of the package if it exists. This information is already present in the json output.
I have a flake with many packages and dev shells and this helps discoverability.
Context
#10977
Priorities and Process
Add 👍 to pull requests you find important.
The Nix maintainer team uses a GitHub project board to schedule and track reviews.