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

Showcase: nutils: Utilities for Nu #559

Closed
dch82 opened this issue Jul 22, 2023 · 11 comments
Closed

Showcase: nutils: Utilities for Nu #559

dch82 opened this issue Jul 22, 2023 · 11 comments

Comments

@dch82
Copy link

dch82 commented Jul 22, 2023

Hello,

I just wanted to showcase my ongoing project, nutils. It's a collection of utilities and wrappers to replace programs provided by traditional POSIX-compatible utility libraries.

So far I have:

  • df: Basic functionality and --all flag.
Output
╭───┬────────────────┬───────────┬───────────┬───────────┬─────────┬────────────────╮
│ # │   filesystem   │   size    │   used    │   avail   │  used%  │   mountpoint   │
├───┼────────────────┼───────────┼───────────┼───────────┼─────────┼────────────────┤
│ 0 │ devtmpfs       │ 791.7 MiB │       0 B │ 791.7 MiB │       0 │ /dev           │
│ 1 │ tmpfs          │   7.7 GiB │  76.5 MiB │   7.7 GiB │  0.9668 │ /dev/shm       │
│ 2 │ tmpfs          │   3.9 GiB │   7.3 MiB │   3.9 GiB │  0.1839 │ /run           │
│ 3 │ tmpfs          │   7.7 GiB │ 408.0 KiB │   7.7 GiB │  0.0050 │ /run/wrappers  │
│ 4 │ none           │   2.0 GiB │  83.3 MiB │   1.9 GiB │  4.0678 │ /              │
│ 5 │ /dev/nvme0n1p2 │ 476.4 GiB │  25.9 GiB │ 450.5 GiB │  5.4462 │ /nix           │
│ 6 │ /dev/nvme0n1p1 │ 510.7 MiB │  82.7 MiB │ 428.0 MiB │ 16.1978 │ /boot/efi      │
│ 7 │ tmpfs          │   1.5 GiB │  96.0 KiB │   1.5 GiB │  0.0059 │ /run/user/1000 │
╰───┴────────────────┴───────────┴───────────┴───────────┴─────────┴────────────────╯
  • lsr: A replacement to tree and ls -r which displays in recursive tables.
Output
╭───┬────────────────┬────────────────────────────────────╮
│ # │      name      │              contents              │
├───┼────────────────┼────────────────────────────────────┤
│ 0 │ nixos-inserter │ ╭───┬────────────────┬──────────╮  │
│   │                │ │ # │      name      │ contents │  │
│   │                │ ├───┼────────────────┼──────────┤  │
│   │                │ │ 0 │ LICENSE        │    ❎    │  │
│   │                │ │ 1 │ README.md      │    ❎    │  │
│   │                │ │ 2 │ nixos-inserter │    ❎    │  │
│   │                │ ╰───┴────────────────┴──────────╯  │
│ 1 │ nutils         │ ╭───┬─────────────────┬──────────╮ │
│   │                │ │ # │      name       │ contents │ │
│   │                │ ├───┼─────────────────┼──────────┤ │
│   │                │ │ 0 │ CONTRIBUTING.md │    ❎    │ │
│   │                │ │ 1 │ LICENSE         │    ❎    │ │
│   │                │ │ 2 │ README.md       │    ❎    │ │
│   │                │ │ 3 │ nutils.nu       │    ❎    │ │
│   │                │ ╰───┴─────────────────┴──────────╯ │
╰───┴────────────────┴────────────────────────────────────╯
  • who
Output
╭───┬───────┬───────┬───────────┬───────┬──────┬────────────────╮
│ # │ name  │  tty  │   date    │ idle  │ pid  │    comment     │
├───┼───────┼───────┼───────────┼───────┼──────┼────────────────┤
│ 0 │ dch82 │ seat0 │ a day ago │ ?     │ 2588 │ (login screen) │
│ 1 │ dch82 │ tty2  │ a day ago │ 18:47 │ 2588 │ (tty2)         │
╰───┴───────┴───────┴───────────┴───────┴──────┴────────────────╯
@fdncred
Copy link
Collaborator

fdncred commented Jul 22, 2023

Nice work on integrating nushell with other externals! The only thing that I'd like to see different is to not use jc if possible. Here's are two different techniques on how to avoid it to make them more nushelly and not require people to have other externals.

Technique 1 - detect columns

 df -B1 |
   str replace "Mounted on" "Mountpoint" |
   detect columns |
   rename filesystem size used avail used% mountpoint |
   into filesize size used avail |
   upsert used% {|r| 100 * (1 - $r.avail / $r.size)}
╭───┬────────────────┬──────────┬──────────┬──────────┬───────┬────────────────────────────╮
 # │   filesystem   │   size   │   used   │  avail   │ used% │         mountpoint         │
├───┼────────────────┼──────────┼──────────┼──────────┼───────┼────────────────────────────┤
 0  /dev/disk3s1s1  994.7 GB  455.1 GB  539.6 GB  45.75  /                          
 1  /dev/disk3s6    994.7 GB  455.1 GB  539.6 GB  45.75  /System/Volumes/VM         
 2  /dev/disk3s2    994.7 GB  455.1 GB  539.6 GB  45.75  /System/Volumes/Preboot    
 3  /dev/disk3s4    994.7 GB  455.1 GB  539.6 GB  45.75  /System/Volumes/Update     
 4  /dev/disk1s2    524.3 MB   20.5 MB  503.7 MB   3.92  /System/Volumes/xarts      
 5  /dev/disk1s1    524.3 MB   20.5 MB  503.7 MB   3.92  /System/Volumes/iSCPreboot 
 6  /dev/disk1s3    524.3 MB   20.5 MB  503.7 MB   3.92  /System/Volumes/Hardware   
├───┼────────────────┼──────────┼──────────┼──────────┼───────┼────────────────────────────┤
 # │   filesystem   │   size   │   used   │  avail   │ used% │         mountpoint         │
╰───┴────────────────┴──────────┴──────────┴──────────┴───────┴────────────────────────────╯

Technique 2 - parse

 who -u | parse '{user}  {tty}      {datetime}  {idle}          {pid}' | update datetime {|r| $r.datetime | into datetime}
╭───┬─────────┬─────────┬────────────┬──────┬─────╮
 # │  user   │   tty   │  datetime  │ idle │ pid │
├───┼─────────┼─────────┼────────────┼──────┼─────┤
 0  fdncred  console  a week ago  old   170 
                                            
╰───┴─────────┴─────────┴────────────┴──────┴─────╯

@dch82
Copy link
Author

dch82 commented Jul 22, 2023

Also... Is it possible to implement these 2 commands in pure Nu?

@fdncred
Copy link
Collaborator

fdncred commented Jul 22, 2023

nushell doesn't have df atm, although we've toyed with adding it before. we may still. we don't have anything that works like who in nushell, that i know of. with lsr we can do du -a | table -e which approximates your tree output.

@dch82
Copy link
Author

dch82 commented Jul 22, 2023

For unix-like systems, we could read from some special file to get results like who and df without external executables.

@fdncred
Copy link
Collaborator

fdncred commented Jul 22, 2023

ya, but then it's not cross-platform. we try really hard to be cross-platform at nushell.

@dch82
Copy link
Author

dch82 commented Jul 22, 2023

I mean df and who aren't necessarily cross-platform... (*cough* *cough* Microsoft *cough*)

@fdncred
Copy link
Collaborator

fdncred commented Jul 22, 2023

I think coreutils has a df and who that may be cross-platform. I haven't tried them on Windows yet though.

@amtoine
Copy link
Member

amtoine commented Jul 23, 2023

agree with @fdncred, i think your df and who can be written without external dependencies such as jc 😋

i have almost the same df as the one above, from my scripts

def df [] {
    ^df -h
        | str replace "Mounted on" "Mountpoint"
        | detect columns
        | rename filesystem size used avail used% mountpoint
        | into filesize size used avail
        | upsert used% {|it| 100 * (1 - $it.avail / $it.size)}
}

and for who, the command above returns an empty list to me 🤔
so i wrote the following

def who [] {
    ^who -u | str replace ' \s+' ',' --all | str trim | split column "," user tty datetime pid
}

which gives me

╭───┬─────────┬──────┬────────────────────────┬──────────╮
│ # │  user   │ tty  │        datetime        │   pid    │
├───┼─────────┼──────┼────────────────────────┼──────────┤
│ 0 │ amtoine │ tty1 │ 2023-07-23 10:01 00:19 │ 592 (:0) │
╰───┴─────────┴──────┴────────────────────────┴──────────╯

and with the fix in nushell/nushell#9775, you can even do

def who [] {
    ^who --all --heading --users | detect columns | rename name tty date idle pid comment exit | into datetime date
}

which gives

╭───┬─────────┬────────┬──────────────┬───────┬─────┬─────────┬──────╮
 # │  name   │  tty   │     date     │ idle  │ pid │ comment │ exit │
├───┼─────────┼────────┼──────────────┼───────┼─────┼─────────┼──────┤
 0           system  16 hours ago                            
 1  amtoine          16 hours ago  00:45  0                  
 2  amtoine  tty1    16 hours ago  00:46  627  (:0)          
╰───┴─────────┴────────┴──────────────┴───────┴─────┴─────────┴──────╯

@dch82
Copy link
Author

dch82 commented Jul 23, 2023

@amtoine @fdncred I implemented your oneliners into nutils!
BTW, if you have any ideas, just code it and submit a pr.

@dch82
Copy link
Author

dch82 commented Jul 24, 2023

Does anybody have ideas for new commands?

@fdncred
Copy link
Collaborator

fdncred commented Jul 24, 2023

not at the moment. let's close this for now since it's not an issue. feel free to drop by our discord and discuss nushell scripting more.

@fdncred fdncred closed this as completed Jul 24, 2023
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

No branches or pull requests

3 participants