Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions modules/history-utils/mod.nu
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,41 @@ def flatten_fields [args] {
def sql [q] {
[
[$q.select ['select', ' as ', ', ']]
[$q.from ['from', ' as ', ' join ']]
[[$q.from] ['from', ' as ', ' join ']]
[$q.where? ['where', ' ', ' and ']]
[$q.whereOr? ['or', ' ', ' or ']]
[$q.groupBy? ['group by', null, ', ']]
[$q.orderBy? ['order by', ' ', ', ']]
[$q.limit? ['limit', null, ' offset ']]
[[$q.limit?] ['limit', null, ' offset ']]
]
| each {|x| $x.0 | flatten_fields $x.1 }
| flatten
| str join ' '
}

# Displays the recent command history
export def 'history timing' [
pattern?
--exclude(-x): string
--num(-n)=10
--all(-a)
pattern?: string # Show commands matching the pattern
--exclude(-x): string # Exclude commands mathing the pattern
--num(-n)=10 # Maximum number or results
--all(-a) # Show commands for all directories (default: cwd)
] {
open $nu.history-path | query db (sql {
from: history
where: [
"cmd not like 'history timing%'"
(if ($pattern | is-not-empty) {[cmd like (quote '%' $pattern '%')]})
(if ($exclude | is-not-empty) {[cmd not like (quote '%' $exclude '%')]})
(if not $all {[cwd = (quote $env.PWD)]})
]
orderBy: [[start desc]]
select: [
[duration_ms duration]
[command_line cmd]
[start_timestamp start]
(if $all {[$"replace\(cwd, '($env.HOME)', '~')" cwd]})
[exit_status exit]
]
where: [
"cmd not like 'history timing%'"
(if ($pattern | is-not-empty) {[cmd like (quote '%' $pattern '%')]})
(if ($exclude | is-not-empty) {[cmd not like (quote '%' $exclude '%')]})
(if not $all {[cwd = (quote $env.PWD)]})
]
orderBy: [[start desc]]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No code change here. Just moved it down for a consistent order (from > select > where > groupBy > orderBy > limit).

limit: $num
})
| update duration {|x| $x.duration | default 0 | do { $in * 1_000_000 } | into duration }
Expand All @@ -66,8 +67,8 @@ export def 'history timing' [

def "nu-complete history dir" [] {
open $nu.history-path | query db (sql {
select: [cwd ['count(1)' count]]
from: history
select: [cwd ['count(1)' count]]
groupBy: [cwd]
orderBy: ['count desc']
limit: 20
Expand All @@ -80,7 +81,7 @@ export def 'history top' [
num=10
--before (-b): duration
--dir (-d)
--path(-p): list<string@"nu-complete history dir">
--path(-p): list<directory>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this changed from 'nu-complete history dir'? it probably shouldn't be a type but it looks like it's intended to provid completions with --path

Copy link
Copy Markdown
Contributor Author

@jatinderjit jatinderjit Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a syntax error for this. I'm guessing this isn't supported.
image

I couldn't find a way to support autocomplete for lists. I could think of two alternatives:

  1. The cop out: support only a single path
  2. Use spread operator ...path: string@"nu-complete history dir".
    • It kind-of works, but has issues. num is a positional argument, and has to be populated first. Probably it would need to be changed to a flagged param.
    • We'd like to hide already selected paths from "nu-complete history dir". We'd need to use context, but I couldn't figure out how it'd handle paths with spaces.

] {
open $nu.history-path | query db (sql {
from: history
Expand Down
Loading