1 -# This is an initial script for nushell
2 -# It is expected that it is sourced in the `config.toml`:
3 -# `startup = ["source ~/.config/nushell/nurc"]`
6 -let-env NURC = (build-string (pwd) /nurc)
7 -let-env DOTFILES = ((build-string (pwd) /..) | path expand)
8 -let-env TODO = (build-string $nu.env.DOTFILES /TODO.md)
9 -# TODO: set this per machine
10 -let-env WORKSPACE = "/home/oli/Workspace"
18 -alias ll = exa --group-directories-first --classify --long --icons --git
19 -alias la = exa --group-directories-first --classify --all --long --icons --git
20 -alias tr = exa --tree
24 -# CUSTOM LITTLE JAVA COMMANDS
25 -alias aes = java (build-string $nu.env.DOTFILES /tools/ aes.java)
26 -alias download = java (build-string $nu.env.DOTFILES /tools/ download.java)
27 -alias download_jar = java (build-string $nu.env.DOTFILES /tools/ download_jar.java)
29 -# MY STDLIB UTIL MISC HELPER FUNCTIONS
31 -# Check whether table in pipeline has a column with given name
33 - name: string # Name of the column to check for
35 - pivot | any? Column0 == $name
38 -# Prints out a list of stuff.
39 -# Appends a newline at the end.
41 - ...rest: any # list of stuff to echo out
43 - echo (build-string ($rest | flatten | into string | str collect) )
47 -# Flattens a table into a string separated by a single space character.
48 -def to-string [] { flatten | into string | str collect ' ' }
52 -# Render my To-Do file in the terminal
53 -def todo [] { mdcat $nu.env.TODO }
54 -# Open my To-Do file for editing
55 -def "todo edit" [] { hx $nu.env.TODO }
56 -# Open the Nushell configuration file for editing
57 -def "config edit" [] { hx (config path) }
58 -# Open my Nushell startup script for editing
59 -def nurc [] { hx $nu.env.NURC }
61 -# Uploads a PUBLIC ssh key to GitHub.
62 -# Will prompt for the AES encryption password of my dotfiles.
63 -def github-upload-key [
64 - title: string # Name the key, so you will remember what it is used for
65 - key: path # Path to the PUBLIC key
67 - if ($key | path exists) {
68 - echo (build-string "uploading ssh public key " (ansi gi) $key (ansi reset) " with title " (ansi gb) $title (ansi reset) " to GitHub.")
70 - let encrypted_token_path = (build-string $nu.env.DOTFILES / tresor / 4cef0ce3-da3a-4f16-9412-2d094b4f7445)
71 - let decrypted_token_path = (build-string $nu.env.DOTFILES / tresor / gh_token)
72 - aes decrypt $encrypted_token_path $decrypted_token_path
73 - let token = (open --raw $decrypted_token_path)
74 - let key_content = (open --raw $key)
75 - rm -f $decrypted_token_path
76 - let result = (do -i { xh --bearer $token https://api.github.com/user/keys (build-string title = $title) (build-string key = $key_content) | from json})
77 - if ($result | has-column? errors) {
78 - let message = ($result | get message)
79 - let url = ($result | get documentation_url)
80 - let errors = ($result | get errors)
81 - echo (build-string (ansi rb) $message (ansi reset) (char newline))
84 - let resource = ($it | get resource)
85 - let code = ($it | get code)
86 - let field = ($it | get field)
87 - let message = ($it | get message)
88 - echo (build-string (ansi ui) $resource : $code : $field ": " (ansi rb) $message (ansi reset) (char newline))
90 - echo (build-string "Further information: " (ansi yi) $url (ansi reset) (char newline))
92 - let id = ($result | get id)
93 - let key = ($result | get key)
94 - let title = ($result | get title)
95 - let created_at = ($result | get created_at)
96 - echo (build-string "[" ID : $id "]" " key with title " (ansi gb) $title (ansi reset) " was created at " (ansi yi) $created_at (ansi reset) "." (char newline) )
97 - echo (build-string (ansi wu) "PUBLIC SSH KEY:" (ansi reset))
99 - echo ($key | ansi gradient --fgstart 0xaefe9f --fgend 0xfefefe)
100 - echo (char newline)
103 - echo (build-string "key " (ansi rb) $key (ansi reset) " not found!")
104 - echo (char newline)
108 -# Shows git log in a pretty table
110 - ...rest: string # Extra arguments to git log
112 - git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 25 $rest | lines | split column "»¦«" commit subject name email date | update date { get date | str to-datetime} | sort-by date
117 -# On Windows, a man web page is opened.
119 - ...rest: string # Arguments to man
121 - if (sys).host.name == "Windows" {
122 - start (build-string https://man.archlinux.org/man/ ($rest | to-string))
128 -# Search the given query on the web with the default browser in DuckDuckGo.
130 - ...rest:string # The search query
132 - # There seems to be an issue with the nu start command on windows: It does not handle URLs
133 - # Using windows command for now:
134 - start (build-string "https://duckduckgo.com/?q=" ($rest | to-string))