Skip to content

Commit 896c073

Browse files
committed
add result.nu
1 parent 6531787 commit 896c073

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

modules/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,7 @@ These scripts should be used to demonstrate how get your local weather and/or we
265265

266266
## [webscraping](../sourced/webscraping/)
267267
Simple scripts to demonstrate how to scrape websites in nushell. Requires `query web` plugin
268+
269+
## [result](./result.nu)
270+
A module to include in the config which enables storing and convenient access of previously output
271+
results.

modules/result.nu

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# A module for storing and accessing previously returned results.
2+
# To activate this module, one should set
3+
# `env.config.hooks.display_output = { result hook }`
4+
5+
export-env {
6+
$env.NU_RESULTS = {
7+
enable: true
8+
max_items: 10
9+
10+
# this is a table with columns value, metadata
11+
results: []
12+
}
13+
}
14+
15+
def display [
16+
meta: record = {}
17+
] {
18+
# the metadata *MUST* be set *FIRST* since the output of `table` is just a string
19+
if $meta.source? == ls { metadata set --datasource-ls } else {}
20+
| if (term size).columns >= 100 { table -e } else { table }
21+
}
22+
23+
def --env trunc [] {
24+
$env.NU_RESULTS.results = ($env.NU_RESULTS.results | last ((get-max) - 1))
25+
}
26+
27+
def horrible-dircolors-hack [] {
28+
let obj = $in
29+
let desc = $in | describe
30+
}
31+
32+
# enable result caching
33+
export def --env enable [] { $env.NU_RESULTS.enable = true }
34+
35+
# disable result caching
36+
export def --env disable [] { $env.NU_RESULTS.enable = false }
37+
38+
# check whether result caching is enabled
39+
export def --env is-enabled [] { $env.NU_RESULTS.enable }
40+
41+
# Hook to be used in `$nu.config.hooks.display_output`. This must be set to enable the
42+
# functionality of this module.
43+
export def --env hook [
44+
--keep-empty # keep empty values. it's not uncommon for external programs to output empty strings
45+
--keep-null # keep null values
46+
] {
47+
let meta = metadata # run this first to preserve original metadata
48+
let res = $in
49+
trunc
50+
if (
51+
(is-enabled) and
52+
($keep_null or ($res != null)) and
53+
($keep_empty or ($res | is-not-empty))
54+
) {
55+
$env.NU_RESULTS.results = $env.NU_RESULTS.results
56+
| append [{value: $res, metadata: $meta}]
57+
}
58+
enable
59+
$res | display $meta
60+
}
61+
62+
# returns the number of stored results
63+
export def count [] { $env.NU_RESULTS.results | length }
64+
65+
# get the currently set maximum number of stored results
66+
export def get-max [] { $env.NU_RESULTS.max_items }
67+
68+
# set the maximum number of stored results
69+
export def --env set-max [
70+
n: int = 10
71+
] {
72+
trunc
73+
$env.NU_RESULTS.max_items = $n
74+
}
75+
76+
77+
# Return all stored previous results.
78+
# This disables storing the output, so if you use it in a pipeline and want to store the
79+
# output, pass the `--keep` flag.
80+
export def --env ls [
81+
--keep(-k) # keep the result of this call in the list
82+
--metadata # return a table preserving the metadata
83+
] {
84+
if not $keep { disable }
85+
if $metadata {
86+
$env.NU_RESULTS.results
87+
} else {
88+
$env.NU_RESULTS.results | get value
89+
}
90+
}
91+
export alias l = ls
92+
93+
94+
# Return a stored result, the very last by default.
95+
# This disables storing the output, so if you use it in a pipeline and want to store the
96+
# output, pass the `--keep` flag.
97+
export def --env main [
98+
idx: int = -1 # number of previous result, -1 is last
99+
--keep(-k) # keep the result of this call in the list
100+
] {
101+
if not $keep { disable }
102+
# it's safer not to error because it seems to not enable in some cases
103+
try {
104+
$env.NU_RESULTS.results | get ((count) + $idx) | get value
105+
} catch {|err|
106+
print $"(ansi $dracula.comment)\(invalid result fetch\)(ansi reset)"
107+
}
108+
}
109+
110+
# get the display string for the entry
111+
def get-display [
112+
idx: int = -1
113+
] {
114+
let rec = $env.NU_RESULTS.results | get ((count) + $idx)
115+
$rec.value | display $rec.metadata
116+
}
117+
118+
# Interactively select previous result. Prettier with sk plugin.
119+
# This disables storing the output, so if you use it in a pipeline and want to store the
120+
# output, pass the `--keep` flag.
121+
export def --env select [
122+
--keep(-k) # keep the result of this call in the list
123+
--preview-pos: string = up # up down left right
124+
--preview-size: string = '80%' # height of sk preview window
125+
--height: string = '40%' # height of sk selection window
126+
] {
127+
(-1)..(0 - (count)) | each {} # convert to list
128+
| if 'skim' in (plugin list | get name) {
129+
let binds = {k: up, j: down}
130+
let lay = $preview_pos ++ ":" ++ $preview_size
131+
$in | (
132+
sk -p { get-display $in } --bind=$binds
133+
--preview-window=$lay --select-1 --exit-0 --height=$height
134+
) | main $in
135+
} else {
136+
each { main $in } | input list
137+
}
138+
}
139+
export alias sel = select
140+
141+
#====================================================================================================
142+
# NOTE:
143+
# lscolors display is dictated by the metadata.
144+
# Most operations do *not* preserve the metadata, so the only way to preserve it is to immediately
145+
# capture it and store it separately.
146+
#===================================================================================================#

0 commit comments

Comments
 (0)