From 655dfe3d48072e627d730a8def6a4ebc5a489b09 Mon Sep 17 00:00:00 2001 From: Bloxx12 Date: Tue, 15 Jul 2025 21:31:31 +0200 Subject: [PATCH] nushell dump --- nushell/completions.nu | 46 ++++++++++ nushell/config.nu | 179 ++++---------------------------------- nushell/env.nu | 26 ------ nushell/prompt.nu | 80 +++++++++++++++++ nushell/source_profile.nu | 23 +++++ nushell/zoxide.nu | 39 +++++---- 6 files changed, 187 insertions(+), 206 deletions(-) create mode 100644 nushell/completions.nu delete mode 100644 nushell/env.nu create mode 100644 nushell/prompt.nu create mode 100755 nushell/source_profile.nu diff --git a/nushell/completions.nu b/nushell/completions.nu new file mode 100644 index 0000000..e564b13 --- /dev/null +++ b/nushell/completions.nu @@ -0,0 +1,46 @@ +$env.config.completions = { + algorithm: prefix + sort: smart + case_sensitive: false + quick: true + partial: true + use_ls_colors: true + external: { + enable: true + max_results: 10 + completer: {|spans: list| + # if the current command is an alias, get it's expansion + let expanded_alias = (scope aliases | where name == $spans.0 | get -i 0 | get -i expansion) + + # overwrite + let spans = (if $expanded_alias != null { + # put the first word of the expanded alias first in the span + $spans | skip 1 | prepend ($expanded_alias | split row " " | take 1) + } else { + $spans | skip 1 | prepend ($spans.0) + }) + + carapace $spans.0 nushell ...$spans + | from json + } + } + +} +$env.config.menus ++= [ + { + marker: $"(ansi yellow)┣ " + name: completion_menu + only_buffer_difference: false + style: { + description_text: yellow + selected_text: red_reverse + text: yellow + } + type: { + col_padding: 2 + col_width: 20 + columns: 1 + layout: columnar + } + } +] diff --git a/nushell/config.nu b/nushell/config.nu index c048a83..8bf1135 100644 --- a/nushell/config.nu +++ b/nushell/config.nu @@ -1,6 +1,6 @@ $env.config = { bracketed_paste: true - buffer_editor: "" + buffer_editor: "hx" datetime_format: {} edit_mode: vi error_style: fancy @@ -22,65 +22,6 @@ $env.config = { } } -$env.config.color_config = { - binary: white - block: white - bool: {|| if $in { "light_green" } else { "light_red" } } - cell-path: white - date: purple - duration: white - empty: blue - filesize: cyan - float: white - header: green_bold - hints: dark_gray - int: white - list: white - nothing: white - range: white - record: white - row_index: green_bold - search_result: { bg: red fg: white } - separator: white - string: {|| if $in =~ "^(#|0x)[a-fA-F0-9]+$" { ($in | str replace "0x" "#") } else { "white" } } - leading_trailing_space_bg: { attr: n } - - shape_and: purple_bold - shape_binary: purple_bold - shape_block: blue_bold - shape_bool: light_cyan - shape_closure: green_bold - shape_custom: green - shape_datetime: cyan_bold - shape_directory: cyan - shape_external: cyan - shape_externalarg: green_bold - shape_filepath: cyan - shape_flag: blue_bold - shape_float: purple_bold - shape_garbage: { fg: white bg: red attr: b} - shape_globpattern: cyan_bold - shape_int: purple_bold - shape_internalcall: cyan_bold - shape_list: cyan_bold - shape_literal: blue - shape_match_pattern: green - shape_nothing: light_cyan - shape_operator: yellow - shape_or: purple_bold - shape_pipe: purple_bold - shape_range: yellow_bold - shape_record: cyan_bold - shape_redirection: purple_bold - shape_signature: green_bold - shape_string: green - shape_table: blue_bold - shape_vardecl: purple - shape_variable: purple - shape_matching_brackets: { attr: u } - shape_string_interpolation: cyan_bold -} - $env.config.ls = { clickable_links: true use_ls_colors: true @@ -125,46 +66,14 @@ $env.config.history = { sync_on_enter: true } -$env.config.completions = { - algorithm: prefix - case_sensitive: false - partial: true - quick: true - external: { - enable: true - max_results: 100 - completer: {|tokens: list| - let expanded = scope aliases | where name == $tokens.0 | get --ignore-errors expansion.0 - - mut expanded_tokens = if $expanded != null and $tokens.0 != "cd" { - $expanded | split row " " | append ($tokens | skip 1) - } else { - $tokens - } - - $expanded_tokens.0 = ($expanded_tokens.0 | str trim --left --char "^") - - fish --command $"complete '--do-complete=($expanded_tokens | str join ' ')'" - | $"value(char tab)description(char newline)" + $in - | from tsv --flexible --no-infer - } - } -} - $env.config.cursor_shape = { - vi_insert: block + vi_insert: line vi_normal: block } $env.config.hooks = { command_not_found: {||} - display_output: { - tee { table --expand | print } - | $env.last = $in - } - - pre_execution: [ { let prompt = commandline | str trim @@ -193,50 +102,15 @@ $env.config.hooks = { ] } -# Retrieve the output of the last command. -def _ []: nothing -> any { - $env.last -} - $env.config.menus = [ { - marker: "| " - name: completion_menu - only_buffer_difference: false - style: { - description_text: yellow - selected_text: green_reverse - text: green - } - type: { - col_padding: 2 - col_width: 20 - columns: 4 - layout: columnar - } - } - { - marker: "? " - name: history_menu - only_buffer_difference: true - style: { - description_text: yellow - selected_text: green_reverse - text: green - } - type: { - layout: list - page_size: 10 - } - } - { - marker: "? " + marker: $"(ansi yellow)╋ " name: help_menu only_buffer_difference: true style: { description_text: yellow - selected_text: green_reverse - text: green + selected_text: red_reverse + text: yellow } type: { col_padding: 2 @@ -257,7 +131,7 @@ $env.config.keybindings = [ mode: [ vi_normal vi_insert ] event: { send: executehostcommand - cmd: 'commandline | nu-highlight | $"```ansi\n($in)\n```" | copy' + cmd: 'commandline | nu-highlight | $"```ansi\n($in)\n```" | wl-copy $in' } } { @@ -273,13 +147,6 @@ $env.config.keybindings = [ ] } } - { - name: history_menu - modifier: control - keycode: char_h - mode: [ vi_insert vi_normal ] - event: { send: menu name: history_menu } - } { name: escape modifier: none @@ -434,29 +301,17 @@ $env.config.keybindings = [ } ] -source ~/.config/nushell/atuin.nu +source ./atuin.nu +source ./zoxide.nu +source ./source_profile.nu +source ./completions.nu +source ./prompt.nu -source ~/.config/nushell/zoxide.nu -# source ~/.config/nushell/source_profile.nu - -# fix sudo by prepending /run/wrappers/bin to the PATH -# $env.PATH = ($env.PATH | split row (char esep) | prepend '/run/wrappers/bin') - - - -$env.STARSHIP_SHELL = "nu" - -def create_left_prompt [] { - starship prompt --cmd-duration $env.CMD_DURATION_MS $'--status=($env.LAST_EXIT_CODE)' -} - -# Use nushell functions to define your right and left prompt -$env.PROMPT_COMMAND = { || create_left_prompt } -$env.PROMPT_COMMAND_RIGHT = "" +# fix sudo by prepending /run/wrappers/bin to the PATH. +$env.PATH = ($env.PATH | split row (char esep) | prepend '/run/wrappers/bin') +$env.LS_COLORS = (vivid generate lava) +$env.EDITOR = "hx" +$env.CARAPACE_BRIDGES = 'zsh,fish,bash,inshellisense' # The prompt indicators are environmental variables that represent -# the state of the prompt -$env.PROMPT_INDICATOR = "" -$env.PROMPT_INDICATOR_VI_INSERT = ": " -$env.PROMPT_INDICATOR_VI_NORMAL = "〉" -$env.PROMPT_MULTILINE_INDICATOR = "::: " +# the state of the prompt. diff --git a/nushell/env.nu b/nushell/env.nu deleted file mode 100644 index 61a542a..0000000 --- a/nushell/env.nu +++ /dev/null @@ -1,26 +0,0 @@ -# env.nu -# -# Installed by: -# version = "0.102.0" -# -# Previously, environment variables were typically configured in `env.nu`. -# In general, most configuration can and should be performed in `config.nu` -# or one of the autoload directories. -# -# This file is generated for backwards compatibility for now. -# It is loaded before config.nu and login.nu -# -# See https://www.nushell.sh/book/configuration.html -# -# Also see `help config env` for more options. -# -# You can remove these comments if you want or leave -# them for future reference. -# -# def --wrapped sudo [...rest] { -# let escaped_args = $rest -# | 1.. -# | each { || to nuon } -# | str join ' ' -# ^sudo env XDG_RUNTIME_DIR=/run/user/0 nu --login --commands $'($rest.0) ($escaped_args)' -# } diff --git a/nushell/prompt.nu b/nushell/prompt.nu new file mode 100644 index 0000000..bcee93d --- /dev/null +++ b/nushell/prompt.nu @@ -0,0 +1,80 @@ +use std null_device + +export-env { load-env { + PROMPT_COMMAND: {|| + let exit_code = $env.LAST_EXIT_CODE + let jj_root = try { ^jj root err> $null_device } catch { "" } + $env.LAST_EXIT_CODE = 0 + let vcs_root = if not ($jj_root | is-empty ) { $jj_root } else {""} + + mut path_segment = "" + if not (($vcs_root | is-empty) or ($env.PWD == $vcs_root)) { + # vcs_root is not empty, we're not in the vcs_root of the project + $path_segment = [($vcs_root | path basename), ($env.PWD | path basename)] | str join " @ " + } else { + $path_segment = $env.PWD | path basename + } + + let exit_code_segment = if ($exit_code == 0) { + "" + } else { + $"(ansi yellow)━┫(ansi red)($exit_code)(ansi yellow)┣━" + } + + [$"(ansi yellow)┏",$exit_code_segment,"━ ", $path_segment, "\n"] | str join + + } + TRANSIENT_PROMPT_COMMAND: {|| + let path_segment = $env.PWD | path basename + + let exit_code_segment = if ($env.LAST_EXIT_CODE == 0) { + "" + } else { + $"(ansi yellow)━┫(ansi red)($env.LAST_EXIT_CODE)(ansi yellow)┣━" + } + + [$"(ansi yellow)━",$exit_code_segment,"━ ", $path_segment] | str join + } +PROMPT_INDICATOR_VI_INSERT: $"(ansi yellow)┃ " +PROMPT_INDICATOR_VI_NORMAL: $"(ansi yellow)┋ " +# PROMPT_MULTILINE_INDICATOR: "::: " +PROMPT_MULTILINE_INDICATOR: $"(ansi yellow)┃ " +TRANSIENT_PROMPT_INDICATOR_VI_INSERT: " " +TRANSIENT_PROMPT_INDICATOR_VI_NORMAL: " " +TRANSIENT_PROMPT_MULTILINE_INDICATOR: $"(ansi yellow)┃ " + + config: ($env.config? | default {} | merge { + render_right_prompt_on_last_line: true + }) + +PROMPT_COMMAND_RIGHT: {|| + let jj_status = try { + jj --quiet --color always --ignore-working-copy log --no-graph --revisions @ --template ' + separate( + " ", + if(empty, label("empty", "(empty)")), + coalesce( + surround( + "\"", + "\"", + if( + description.first_line().substr(0, 24).starts_with(description.first_line()), + description.first_line().substr(0, 24), + description.first_line().substr(0, 23) ++ "…" + ) + ), + label(if(empty, "empty"), description_placeholder) + ), + bookmarks.join(", "), + change_id.shortest(), + commit_id.shortest(), + if(conflict, label("conflict", "(conflict)")), + if(divergent, label("divergent prefix", "(divergent)")), + if(hidden, label("hidden prefix", "(hidden)")), + ) + ' err> $null_device + } catch { + "" + } + $jj_status +}}} diff --git a/nushell/source_profile.nu b/nushell/source_profile.nu new file mode 100755 index 0000000..81dfe26 --- /dev/null +++ b/nushell/source_profile.nu @@ -0,0 +1,23 @@ +#! /usr/bin/env nu + +let env_vars_file = '/tmp/nushell-env-vars' +if not ($env_vars_file | path exists) { + open /etc/profile + | lines + | select 11 + | str trim + | split column ' ' del path + | get path.0 + | open $in + | str trim + | lines + | parse 'export {name}="{value}"' + | transpose --header-row --as-record + | tee {load-env $in} + | to nuon + | save --force $env_vars_file +} else { + open $env_vars_file + | from nuon + | load-env $in +} diff --git a/nushell/zoxide.nu b/nushell/zoxide.nu index 57d115f..079b95a 100644 --- a/nushell/zoxide.nu +++ b/nushell/zoxide.nu @@ -1,8 +1,3 @@ -# ============================================================================= -# -# Hook configuration for zoxide. -# - # Initialize hook to add new entries to the database. export-env { $env.config = ( @@ -23,13 +18,28 @@ export-env { } } -# ============================================================================= -# -# When using zoxide with --no-cmd, alias these internal functions as desired. -# +def "nu-complete zoxide path" [context: string] { + let parts = $context | split row -r "[ /]" | skip 1 + { + options: { + sort: false, + completion_algorithm: substring, + case_sensitive: false, + }, + completions: ( + ^zoxide query --list --exclude $env.PWD -- ...$parts + | lines + | first 10 + | each {|p| $p | path relative-to $env.PWD | $"($in)/"} + + # | each {|p| echo p | path basename} + ) + # | each {|p| echo p | path basename | $"($in)/"}), + } +} # Jump to a directory using only keywords. -def --env --wrapped __zoxide_z [...rest: string] { +def --env --wrapped __zoxide_z [...rest: string@"nu-complete zoxide path"] { let path = match $rest { [] => {'~'}, [ '-' ] => {'-'}, @@ -42,16 +52,9 @@ def --env --wrapped __zoxide_z [...rest: string] { } # Jump to a directory using interactive search. -def --env --wrapped __zoxide_zi [...rest:string] { +def --env --wrapped __zoxide_zi [...rest:string@"nu-complete zoxide path"] { cd $'(zoxide query --interactive -- ...$rest | str trim -r -c "\n")' } -# ============================================================================= -# -# Commands for zoxide. Disable these using --no-cmd. -# - alias cd = __zoxide_z alias ci = __zoxide_zi - -# =============================================================================