You are not logged in.

#1 Yesterday 15:20:59

lenhuppe
Member
From: New Hampshire USA
Registered: 2018-12-10
Posts: 315
Website

SOLVED Configuring custom Nushell functions

I am tinkering with Nushell lately and I have a configuration issue. I have a number of custom scripts and functions that I use, and in Fish I am able to add my functions to my scripts. However in Nushell that is not yet the case.

So far I can run my scripts from the command line by storing them in the autoload subdirectory. I can also run my functions from the command line by keeping them in a single file that I source from my config.nu file.

My issue is that I cannot include my functions in my scripts. I have been reading about modules but so far I have been unable to get that approach to work. I am running out of ideas to try.

I understand that Nushell is still in the early stages at version 0.116.1 but there are people using it in production even now. Any Nushell gurus out there?

Here are my functions kept in a module named funcs:

functions.nu

#
# my functions
#

module funcs {

    # print status messages to the screen
    export def prnt [arg: string] {
        $"\n\t(ansi green)($arg)(ansi reset)\n"
    }

    # print informational messages to the screen
    export def mesg [arg: string] {
        $"\n\t(ansi yellow)($arg)(ansi reset)\n"
    }

    # print alert messages to the screen
    export def alrt [arg: string] {
        $"\n\t(ansi red)($arg)(ansi reset)\n"
    }

    # confirm before continuing a script
    export def confirm [] {
        while true {
            let reply = (input "Continue? [y/n]: ")
            if $reply == y {
                break
            } else if $reply == n {
                print $"(ansi red)Aborting(ansi reset)"
                return 1
            } else {
                print $"(ansi red)Please enter y or n(ansi reset)"
                continue
            }
        }
    }
}

Here is my config so far:

config.nu

#
# nu config
#

# functions
source ~/.config/nushell/functions.nu
use funcs * # for interactive mode

# hooks
source ~/.config/nushell/hooks.nu

# abbreviations expose the executed command
# $env.config.abbreviations = {
#    ls: "ls -as"
# }

# aliases hide the executed command
# do not alias: ls
alias blk = lsblk -o name,fstype,label,tran,mountpoints
alias eza = eza --icons -F=always -aaglM@
alias fdupes = fdupes -rS
alias free = sys mem
alias g-d = gnome_desktop.fish
alias gdu = gdu -a
alias graphical = sudo systemctl set-default graphical.target
alias grep = ugrep -izr
alias lock = xdg-screensaver lock
alias lsa = ls -as
alias moon = curl wttr.in/moon
alias multi-user = sudo systemctl set-default multi-user.target
alias nvim = nvim -n
alias opn = nautilus ($env.PWD)
alias pkgfile = pkgfile -i
alias tree = tree -apCF
alias vi = nvim -n
alias vim = nvim -n
alias wttr = curl wttr.in/Manchester+NH

# prompt
use ~/.config/nushell/my-prompt.nu *
$env.PROMPT_INDICATOR = {|| "" }
$env.PROMPT_COMMAND = {|| left-prompt }
$env.PROMPT_COMMAND_RIGHT = {||
    date now | format date "%d-%a %r"
}

# fastfetch
fastfetch -c paleofetch

And my env setup:

env.nu

#
# nu env
#

# table --list
# env.config.table.mode = ''

# path is inheritrd from fish?
# $env.PATH ++= [ "~/.local/bin" ]
# $env.PATH = [ "~/.local/bin" ] ++ $env.PATH

# Add custom directories:
# const NU_LIB_DIRS = [
#     ($nu.default-config-dir | path join 'scripts')
#     ($nu.data-dir | path join 'completions')
# ]

# An environment variable version ($env.NU_LIB_DIRS) is searched after the constant.

# misc
$env.rm_always_trash = false
$env.auto_cd_implicit = true
$env.show_hints = true
$env.use_kitty_protocol = true
$env.config.show_banner = false

# env setup
export-env {
    load-env {
        BROWSER: "firefox"
        PAGER: "less"
        # MAN_PAGER: "bat -plman --strip-ansi=auto"
        HOSTNAME:  (sys host | get hostname) # uname | get nodename
    }
}

# editor
if (sys host | get hostname) == archie {
    export-env {
        load-env {
            BUFFER_EDITOR: "gnome-text-editor"
            SUDO_EDITOR: "gnome-text-editor"
            EDITOR: "gnome-text-editor"
            VISUAL: "gnome-text-editor"
        }
    }
} else if (sys host | get hostname) == lurch {
    export-env {
        load-env {
            BUFFER_EDITOR: "nvim -n"
            SUDO_EDITOR: "nvim -n"
            EDITOR: "nvim -n"
            VISUAL: "nvim -n"
        }
    }
}

# history
$env.config.history = {
    file_format: plaintext
    max_size: 500
    sync_on_enter: true
    isolation: false
}

# fzf
$env.FZF_DEFAULT_OPTS = '
    --color=info:#ffd800
    --color=bg:#111111
    --color=bg+:#222222
    --color=hl:#00ff00
    --color=hl+:#ff00ff
    --color=fg:white
    --color=fg+:cyan
    --color=pointer:#ff00ff
	--color=prompt:#00ff00
	--pointer="➤"
	--marker="◆"
	--prompt="❯ "
	--border="rounded"
	--border-label=""
	--preview-window="border-rounded"
	--separator="─"
	--scrollbar="│"
    '
# --color=spinner:#e6db74
# --color=header:red
# --color=marker:#ffd800
# --color=border:#808080

]

This is one of my functions:

pac.nu

#
# update system with pacman
#
export def pac [] {
    use funcs *
    prnt 'Updating with Pacman'
    sudo systemctl start mirrorlist.service
    echo
    sudo pacman -Syu
}

If I run pac from the command line, it runs ok but the message does not display.

Last edited by lenhuppe (Today 01:07:15)

Offline

#2 Yesterday 16:55:21

schard
Forum Moderator
From: Hannover
Registered: 2016-05-06
Posts: 2,711
Website

Re: SOLVED Configuring custom Nushell functions

I dont see pac.nu being imported anywhere.


Inofficial first vice president of the Rust Evangelism Strike Force

Offline

#3 Yesterday 17:19:20

lenhuppe
Member
From: New Hampshire USA
Registered: 2018-12-10
Posts: 315
Website

Re: SOLVED Configuring custom Nushell functions

schard wrote:

I dont see pac.nu being imported anywhere.

I have not figured out how to use a scripts directory even thought it is part of the NU_LIB_DIRS path.
As a workaround I have it in the autoload directory.
I tried sourcing pac.nu from config.nu but did not work

Offline

#4 Today 01:06:32

lenhuppe
Member
From: New Hampshire USA
Registered: 2018-12-10
Posts: 315
Website

Re: SOLVED Configuring custom Nushell functions

1) I found a discussion of the path issue here and came up with this hack for my config.nu file:

# workaround to source scripts
ls ~/.config/nushell/scripts
| get name
| each { str replace -r ^ 'source ' }
| to text
| save -f ~/.config/nushell/func_list.nu
source ~/.config/nushell/func_list.nu

2) I found another discussion here and added this to my env.nu file:

# add scripts directory to path
const NU_LIB_DIRS = [
    ($nu.default-config-dir | path join 'scripts')
]

3) I also added this to my config.nu:

# custom functions module
source ~/.config/nushell/functions.nu
use funcs * # <-- this is required for interactive use

4) I rewrote my functions.nu file to be nu native:

functions.nu

#
# my functions
#

module funcs {

    # print status messages to the screen
    export def prnt [arg: string] {
        print ([
            (char newline)
            (char tab)
            (ansi green)
            ($arg)
            (ansi reset)
            (char newline)
        ] | str join )
    }

    # print informational messages to the screen
    export def mesg [arg: string] {
        print ([
            (char newline)
            (char tab)
            (ansi yellow)
            ($arg)
            (ansi reset)
            (char newline)
        ] | str join )
    }

    # print alert messages to the screen
    export def alrt [arg: string] {
        print ([
            (char newline)
            (char tab)
            (ansi red)
            ($arg)
            (ansi reset)
            (char newline)
        ] | str join )
    }

    # confirm before continuing a script
    export def confirm [] {
        loop {
            let reply = input "Continue? [y/n]: "
            match $reply {
                y => { break }
                n => {
                    print ([
                (char newline)
                (ansi red)
                Abortig
                (ansi reset)
            ] | str join )
                    return 1
                }
                _ => {
                    print ([
                (char newline)
                (ansi yellow)
                'Please enter y or n'
                (char newline)
                (ansi reset)
            ] | str join )
                    continue
                }
            }
        }
    }
}

I hope that this helps others wanting to have a look at Nushell.
It's still early on in development so not all features are working yet.
The more that I tinker with it the more I like it.

Last edited by lenhuppe (Today 11:41:19)

Offline

Board footer

Powered by FluxBB