Skip to content

Instantly share code, notes, and snippets.

@amtoine
Created October 6, 2023 17:33
Show Gist options
  • Save amtoine/ad20230ede2910cf2d91d180f94ba3f2 to your computer and use it in GitHub Desktop.
Save amtoine/ad20230ede2910cf2d91d180f94ba3f2 to your computer and use it in GitHub Desktop.
List Gists of a GitHub user with the `unfold` command of Nushell

i wanted to play a bit with the new unfold command introduced by @hudclark in nushell/nushell#10489 and went with a gist listing task 😏

πŸ’‘ Note
see nushell/nushell#10630

def "gh list gists" [
    user: string, # the user to list all the Gists of
    --page-size: int = 100, # the number of Gists per page
]: nothing -> table<url: string, forks_url: string, commits_url: string, id: string, node_id: string, git_pull_url: string, git_push_url: string, html_url: string, files record, public: bool, created_at: string, updated_at: string, description: string, comments: int, user: nothing, comments_url: string, owner: record<login: string, id: int, node_id: string, avatar_url: string, gravatar_id: string, url: string, html_url: string, followers_url: string, following_url: string, gists_url: string, starred_url: string, subscriptions_url: string, organizations_url: string, repos_url: string, events_url: string, received_events_url: string, type: string, site_admin: bool>, truncated: bool, page: int> {
    unfold 1 { |page|
        print $"getting page ($page)..."
        let resp = http get (
            {
                scheme: https,
                host: "api.github.com",
                path: $"/users/($user)/gists",
                params: {
                    page: $page,
                    per_page: $page_size
                }
            } | url join)

        if ($resp | length) < $page_size {
            {out: $resp}
        } else {
            {out: $resp, next: ($page + 1)}
        }
    }
        | enumerate
        | each {|it| $it.item | insert page $it.index}
        | flatten
}

an example with my gists

gh list gists "amtoine" | select id description

and the output

getting page 1...
#─┬───────────────id───────────────┬──────────────────────────────────────description──────────────────────────────────────
0 β”‚3fe1435edb656964d8066b5b7da9af5fβ”‚Plot startup times of Nushell
1 β”‚535f21c16a8c1ebeb180db2c49c494b4β”‚Nushell dependency graph
2 β”‚9531ea725991177ac9e2dcfe19193c5eβ”‚`arkworks-rs` dependency graph
3 β”‚d3f216b30ff331d1765702172b4eeafcβ”‚compare HTTP files
4 β”‚84ad5a5d760c91393d58c22c02c358b6β”‚auto update from HTTP file
5 β”‚a46c5cb1da94b3f0791b735663c03d9eβ”‚inspect all nushell issues
6 β”‚e3e4d75f6d0bd3b3a439077df1380421β”‚a wrapper around the `gstat` plugin for `nushell`
7 β”‚c68b9d5122454f0c2d2bc626d5e491f9β”‚list contributions in a *GitHub* repo
8 β”‚4c748237ccd09a4ad745ed46d87fea3dβ”‚make a system alias
9 β”‚be49729de499efdffa57e21b3bbc5c45β”‚measure completion impact on the startup time of `nushell`
10β”‚eee36e71a0a4b40a9ad3a8e5d4785341β”‚pull github repos without `gh` CLI
11β”‚d24a14956eb61c5c1a06df57dcd8dadeβ”‚list `nushell` search terms
12β”‚8b028e16747bf7f850a8f55cfc4c1327β”‚list contributions between PRs
13β”‚a2d45c6a804d61280a31a9ffc4b69910β”‚a PNG parser for `nushell`
14β”‚9384a03b428edbd85324e6f8ac099a58β”‚Some tips and observations on my journey on Ubuntu for my job
15β”‚69c8be3dd38ec75afb86da78210c524dβ”‚The JS source from "Juan Benet: Enter the Merkle Forest" (https://youtu.be/Bqs_LzBjQyk)
──┴────────────────────────────────┴───────────────────────────────────────────────────────────────────────────────────────
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment