From d2422771eb8e4186841ebab8ef486328e9a07d28 Mon Sep 17 00:00:00 2001 From: isabel Date: Thu, 27 Jun 2024 15:35:55 -0400 Subject: [PATCH] nix flake show: add the description if it exists (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: https://github.com/NixOS/nix/pull/10980 Change-Id: Ie1c667dc816b3dd81e65a1f5395e57ea48ee0362 --- doc/manual/change-authors.yml | 7 ++++ .../rl-next/nix-flake-show-description.md | 37 +++++++++++++++++++ package.nix | 3 ++ src/nix/flake.cc | 36 +++++++++++++----- tests/functional/flakes/show.sh | 28 ++++++++++++++ 5 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 doc/manual/rl-next/nix-flake-show-description.md diff --git a/doc/manual/change-authors.yml b/doc/manual/change-authors.yml index f56a1e6fb..49df7f5df 100644 --- a/doc/manual/change-authors.yml +++ b/doc/manual/change-authors.yml @@ -65,10 +65,17 @@ horrors: iFreilicht: github: iFreilicht +isabelroses: + forgejo: isabelroses + github: isabelroses + jade: forgejo: jade github: lf- +kjeremy: + github: kjeremy + kloenk: forgejo: kloenk github: kloenk diff --git a/doc/manual/rl-next/nix-flake-show-description.md b/doc/manual/rl-next/nix-flake-show-description.md new file mode 100644 index 000000000..e819a3a29 --- /dev/null +++ b/doc/manual/rl-next/nix-flake-show-description.md @@ -0,0 +1,37 @@ +--- +synopsis: "Lix will now show the package descriptions in when running `nix flake show`." +cls: [1540] +issues: [] +credits: [kjeremy, isabelroses] +category: Improvements +--- + +When running `nix flake show`, Lix will now show the package descriptions, if they exist. + +Before: + +```shell +$ nix flake show +path:/home/isabel/dev/lix-show?lastModified=1721736108&narHash=sha256-Zo8HP1ur7Q2b39hKUEG8EAh/opgq8xJ2jvwQ/htwO4Q%3D +└───packages + └───x86_64-linux + ├───aNoDescription: package 'simple' + ├───bOneLineDescription: package 'simple' + ├───cMultiLineDescription: package 'simple' + ├───dLongDescription: package 'simple' + └───eEmptyDescription: package 'simple' +``` + +After: + +```shell +$ nix flake show +path:/home/isabel/dev/lix-show?lastModified=1721736108&narHash=sha256-Zo8HP1ur7Q2b39hKUEG8EAh/opgq8xJ2jvwQ/htwO4Q%3D +└───packages + └───x86_64-linux + ├───aNoDescription: package 'simple' + ├───bOneLineDescription: package 'simple' - 'one line' + ├───cMultiLineDescription: package 'simple' - 'line one' + ├───dLongDescription: package 'simple' - 'abcdefghijklmnopqrstuvwxyz' + └───eEmptyDescription: package 'simple' +``` diff --git a/package.nix b/package.nix index 435a265d1..b9ef0273c 100644 --- a/package.nix +++ b/package.nix @@ -20,6 +20,7 @@ doxygen, editline-lix ? __forDefaults.editline-lix, editline, + expect, git, gtest, jq, @@ -253,6 +254,8 @@ stdenv.mkDerivation (finalAttrs: { ++ lib.optional (hostPlatform.canExecute buildPlatform) aws-sdk-cpp-nix ++ lib.optionals (finalAttrs.dontBuild) maybePropagatedInputs; + nativeCheckInputs = [ expect ]; + checkInputs = [ gtest rapidcheck diff --git a/src/nix/flake.cc b/src/nix/flake.cc index 63b250d13..9d18b81b8 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -15,6 +15,7 @@ #include "registry.hh" #include "eval-cache.hh" #include "markdown.hh" +#include "terminal.hh" #include #include @@ -1225,25 +1226,42 @@ struct CmdFlakeShow : FlakeCommand, MixJSON auto showDerivation = [&]() { auto name = visitor.getAttr(state->sName)->getString(); + std::optional description; + if (auto aMeta = visitor.maybeGetAttr(state->sMeta)) { + if (auto aDescription = aMeta->maybeGetAttr(state->sDescription)) + description = aDescription->getString(); + } + if (json) { - std::optional description; - if (auto aMeta = visitor.maybeGetAttr(state->sMeta)) { - if (auto aDescription = aMeta->maybeGetAttr(state->sDescription)) - description = aDescription->getString(); - } j.emplace("type", "derivation"); j.emplace("name", name); if (description) j.emplace("description", *description); } else { - logger->cout("%s: %s '%s'", - headerPrefix, + auto type = attrPath.size() == 2 && attrPathS[0] == "devShell" ? "development environment" : attrPath.size() >= 2 && attrPathS[0] == "devShells" ? "development environment" : attrPath.size() == 3 && attrPathS[0] == "checks" ? "derivation" : attrPath.size() >= 1 && attrPathS[0] == "hydraJobs" ? "derivation" : - "package", - name); + "package"; + if (description && !description->empty()) { + // Trim the string and only display the first line of the description. + auto desc = nix::trim(*description); + auto firstLineDesc = desc.substr(0, desc.find('\n')); + + std::string output = fmt("%s: %s '%s' - '%s'", headerPrefix, type, name, firstLineDesc); + if (output.size() > getWindowSize().second) { + // we resize to 4 less then the window size to account for the "...'" we append to + // the final string, we also include the ' since that is removed when we truncate the string + output.resize(getWindowSize().second - 4); + output.append("...'"); + } + + logger->cout("%s", output.c_str()); + } + else { + logger->cout("%s: %s '%s'", headerPrefix, type, name); + } } }; diff --git a/tests/functional/flakes/show.sh b/tests/functional/flakes/show.sh index a3d300552..25f481575 100644 --- a/tests/functional/flakes/show.sh +++ b/tests/functional/flakes/show.sh @@ -85,3 +85,31 @@ assert show_output.legacyPackages.${builtins.currentSystem}.AAAAAASomeThingsFail assert show_output.legacyPackages.${builtins.currentSystem}.simple.name == "simple"; true ' + +cat >flake.nix< show-output.txt +' +test "$(awk -F '[:] ' '/aNoDescription/{print $NF}' ./show-output.txt)" = "package 'simple'" +test "$(awk -F '[:] ' '/bOneLineDescription/{print $NF}' ./show-output.txt)" = "package 'simple' - 'one line'" +test "$(awk -F '[:] ' '/cMultiLineDescription/{print $NF}' ./show-output.txt)" = "package 'simple' - 'line one'" +test "$(awk -F '[:] ' '/dLongDescription/{print $NF}' ./show-output.txt)" = "package 'simple' - 'abcdefghijklmnopqrs...'" +test "$(awk -F '[:] ' '/eEmptyDescription/{print $NF}' ./show-output.txt)" = "package 'simple'"