1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 06:37:46 +00:00

Port before_v0.60/config_management before_v0.60/language before_v0.60/prompt before_v0.60/tests (#851)

This PR is part of porting all old scripts #221 and includes a set of
modules:

- `config_management`
- `language`
- `tests`
- `prompt`

## 12 files changed

### `config_management`
This module was removed, because it was related to the old `config.toml`
format
1. `README.md`: removed
2. `config.toml`: removed
3. `startup.nu`: removed

### `language`
4. `playground.nu` : unchanged because this is just `source ...`
5. `std.nu` : unchanged because this is just `source ...`
6. `playground/lib.nu` -> `playground/mod.nu` and all commands have been
exported
7. `std/date.nu` + `export` keyword

### `tests`
This module contained only the usage of `language` module, so I merged
it with `language`
8. `tests/language/std/date.nu` -> `modules/language/tests/date.nu`
9. `main.nu`: removed because it was just `source date.nu`

### `prompt`
10. `git_status_prompt.nu` -> `modules/git_status_prompt.nu` + `export`
keyword
11. `left_and_right_prompt.nu` -> `modules/left_and_right_prompt.nu` +
`export` keyword
12. `README.md` : has been removed as a duplicate of
`modules/prompt/README.md`
This commit is contained in:
Igor 2024-05-26 21:37:01 +04:00 committed by GitHub
parent e8df70a406
commit 9d399d8902
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 273 additions and 336 deletions

View file

@ -0,0 +1 @@
source language/playground/lib.nu

View file

@ -0,0 +1,54 @@
export def main [topic, closure] {
with-env {N: 5 REJECT: slow } {
print (echo $topic " tests" (char newline) | str join)
do $closure
}
}
export def scene [
topic: any
--tag: string
closure: closure
] {
print $" ($topic)(char newline)"
do $closure
}
export def play [
topic: any
--tag: string
closure: closure
] {
let is_tag_empty = ($tag | is-empty);
let should_run_all = ($env | get -i RUN_ALL | default false);
if $is_tag_empty {
do $closure $topic
} else {
if $tag == $env.REJECT and $should_run_all {
$" ($topic) ... (ansi yellow)skipped(ansi reset) (char newline)"
} else {
do $closure $topic
}
}
}
export def expect [
topic: string
actual: list<any>
--to-be: list<any>
] {
let are_equal = (($actual | length) == ($to_be | length)) and ($actual | zip $to_be | all {|case|
$case.0 == $case.1
}
)
let line = (if true == $are_equal {
$"(ansi green)ok(ansi reset)(char newline)"
} else {
$"(ansi red)failed(ansi reset)(char newline)"
}
)
$" ($topic) ... ($line)"
}

1
modules/language/std.nu Normal file
View file

@ -0,0 +1 @@
source language/std/date.nu

View file

@ -0,0 +1,7 @@
export def "date local" [now] {
insert time {|value|
let converted = ($now | date to-timezone $value.tz);
$converted | format date '%c'
}
}

View file

@ -0,0 +1,34 @@
use ../playground *
use ../std/date.nu *
def mock-now [] {
"2021-08-29 03:31:21" | into datetime
}
def people [] {
[
[ 'name', 'tz'];
[ 'andres', 'America/Guayaquil']
[ 'fdncred', 'US/Central']
]
}
playground "std/date" {
scene 'command: "date local"' {
play "adds times in local timezone" {|topic|
let expected_times = [
"Sun Aug 29 03:31:21 2021"
"Sun Aug 29 03:31:21 2021"
] | into datetime;
let actual = (people | date local (mock-now) | get time | into datetime)
expect $topic $actual --to-be $expected_times
}
}
}

View file

@ -0,0 +1,97 @@
# Displays a prompt
export def git-status-prompt [] {
let not_windows = ($nu.os-info.name !~ "windows")
$"(ansi reset)(ansi green)(if $not_windows {
$env.USER
} else {
$env.USERNAME
}
)(ansi reset)@(hostname | str trim):(ansi green_dimmed)(prompt-pwd)(ansi reset)(git-branch-icon)(ansi reset)(char newline)(char prompt) "
}
# Returns a shortened pwd for use in prompt
def prompt-pwd [] {
let not_windows = ($nu.os-info.name !~ "windows")
let path = (pwd | if $not_windows { split row "/" } else { split row '\' })
let home = (if $not_windows {
($env.HOME | split row "/")
} else {
(echo [$env.HOMEDRIVE $env.HOMEPATH] | path join | split row '\')
}
)
if ($path | length) > 1 {
if ($home | all {|it| $it in $path }) {
let path_without_home = ($path | skip ($home | length))
if ($path_without_home | wrap path | compact | length) > 0 {
let parent = ($path | skip ($home | length) | drop)
if ($parent | wrap parent | compact | length) > 0 {
let short_part = ($parent | each { |part|
if ($part | str starts-with ".") {
$"($part | str substring [0 2])/"
} else {
$"($part | str substring [0 1])/"
}
})
$"~/( | str join)($path | last)"
} else {
$"~/($path | last)"
}
} else {
"~"
}
} else {
let parent = (echo $path | drop | str substring [0 1] | each {|it| echo $it "/" })
$"/($parent)($path | last)"
}
} else {
pwd
}
}
# Map of git status codes to ANSI colour codes
def git-prompt-map [] {
echo a m r c d "??" u |
rotate --ccw |
reject column0 | append (
echo (ansi green) (ansi yellow_bold) (ansi cyan) (ansi blue) (ansi red) (ansi red_dimmed) (ansi red) |
rotate --ccw |
reject column0
) | headers
}
# Gets an icon and a colour for a given git status code
def git-prompt-icons [k] {
let icns = ["✚ " "* " "➜ " "⇒ " "✖ " "? " "! "];
git-prompt-map |
transpose status colour | enumerate | each { |icon|
let idx = $icon.index;
if $icon.item.status == $k {
$"($icon.item.colour)($icns | get $idx)"
}
} | compact
}
# Checks git status of current working directory and displays an icon
def git-branch-icon [] {
do -i {
let branch = (do -i { git rev-parse --abbrev-ref HEAD } | str trim)
if ($branch | str length) > 0 {
let modified = (do -i { git status --porcelain } | split row "\n" | str trim | split column " " status file);
if ($modified | get 0 | first | is-empty) {
$"|(ansi green)($branch)(ansi reset):(ansi green)✓(ansi reset)"
} else {
let modified2 = (do -i { git status --porcelain } | split row "\n" | str substring [0 1])
let branch_colour = (if (echo $modified2 | each {|it| $it in [A M R C D] } | reduce {|it acc| $it or $acc }) {
"yellow"
} else {
"red"
}
)
$"|(ansi $branch_colour)($branch)(ansi reset):($modified | get status | uniq | str downcase | each {|it| git-prompt-icons $it })" | str join
}
}
}
}

View file

@ -0,0 +1,86 @@
# This is a work in progress. Not working yet but you can see where I'm going.
export def construct_prompt [] {
# let decorator = (char prompt)
let decorator = (create_second_line)
# not using machine name
# let machine_name = (sys | get host.hostname)
# the current working directory
# let current_dir = (pwd)
let current_dir = (home_abbrev)
# the current bit branch
# let git_status = (git -c core.quotepath=false -c color.status=false status -uall --short --branch)
let git_info = (do -i { git rev-parse --abbrev-ref HEAD } | str trim -c (char nl) | str join )
# what to put in the title
let title_bar = (set_title)
# get the terminal width
let term_width = (term size).columns
# get the curren time
let current_time = (date now | format date '%I:%M:%S%.3f %p')
# let's construct the left and right prompt
# the left side of the prompt with ansi colors
let left_colored = $"(ansi gb)($current_dir)(ansi cb)(char lparen)($git_info)(char rparen)(ansi reset)"
# the left prompt length without the ansi escapes
let left_len = ($left_colored | ansi strip | str length)
# the right side of the prompt with ansi colors
let right_colored = $"(ansi blue)($env.CMD_DURATION_MS)|(ansi dark_gray)($current_time)(ansi reset)"
# let's calcuate the length of the right prompt so we know how much to pad the left prompt
let calculated_right_len = ($term_width - $left_len)
# finally, let's make the prompt
let the_prompt = $"($left_colored)($right_colored | fill -a r -c ' ' -w $calculated_right_len)(char newline)($decorator) "
# let's update the title bar now
print -n $title_bar
# and last, but not least, let's print the prompt
echo $the_prompt
## put this in your config.toml
# prompt = "construct_prompt"
## also you need to source the file in your startup like
# "source C:\\Users\\username\\source\\some\\folder\\nu_scripts\\prompt\\left_and_right_prompt.nu",
}
# Abbreviate home path
def home_abbrev [] {
let is_home_in_path = (pwd | into string | str starts-with $nu.home-path)
if $is_home_in_path {
let lin_home = ($nu.home-path | into string | str replace -a '\\' '/' | str downcase)
let lin_pwd = (pwd | into string | str replace -a '\\' '/' | str downcase)
$lin_pwd | str replace $lin_home '~'
} else {
pwd
}
}
# Get Git Info custom commands
def git_br [] {
$"(ansi gb)(pwd)(ansi reset)(char lparen)(ansi cb)(do -i { git rev-parse --abbrev-ref HEAD } | str trim -c (char nl) | str join)(ansi reset)(char rparen)(char newline)(ansi yb)(date now | format date '%m/%d/%Y %I:%M:%S%.3f %p')(ansi reset)¯\\_(char lparen)ツ)_/¯(char prompt) "
}
# Set Title String custom commands
def set_title_str [str_arg] {
$"(ansi title) ($str_arg) (char bel)"
}
def get_abbrev_pwd_lin [] {
home_abbrev | split row '/' | first (home_abbrev | split row '/' | length | each { $in - 1} ) | each { str substring 0..1 | $'($in)/' } | append (home_abbrev | split row '/' | last ) | str join
}
def set_title [] {
set_title_str ([(get_abbrev_pwd_lin) ' ' (term size).columns 'x' (term size).rows ] | str join)
}
def create_second_line [] {
[(ansi gb) (char -u "2514") (char -u "2500") ' $ ' (ansi cb) (char prompt) (ansi reset)] | str join
}