Skip to content

Commit

Permalink
Improve ls / list --folders-first behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
xwmx committed Feb 16, 2024
1 parent 2971acd commit f4920e7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
12 changes: 9 additions & 3 deletions nb
Original file line number Diff line number Diff line change
Expand Up @@ -5317,7 +5317,7 @@ HEREDOC
_list() {
local _error_on_empty=0
local _excerpt_length=3
local _folders_first="${NB_LIST_FOLDERS_FIRST:-0}"
local _folders_first=0
local _force_line_wrap=0
local _limit=
local _line_wrap=1
Expand Down Expand Up @@ -5365,7 +5365,7 @@ _list() {
_list_filenames=1
_recursive_arguments+=("--filenames")
;;
--folders-first|--ff|-ff|--f|-f)
--folders*first|--ff|-ff|--f|-f)
_folders_first=1
;;
--limit|-n|--num|--number|--per*)
Expand All @@ -5379,6 +5379,9 @@ _list() {
--no*color)
_COLOR_ENABLED=0
;;
--no*folders*first|--no*ff|-no*ff|--no*f|-no*f)
_folders_first=0
;;
--no-id*|--noid*|--no*index)
_no_id=1
_recursive_arguments+=("--no-id")
Expand Down Expand Up @@ -6765,7 +6768,7 @@ _ls() {
local _delegate_arguments=()
local _fast_exit=0
local _filter_patterns=()
local _folders_first="${NB_LIST_FOLDERS_FIRST:-0}"
local _folders_first="${NB_LS_FOLDERS_FIRST:-0}"
local _header_flags=()
local _limit=
local _maybe_type=
Expand Down Expand Up @@ -6818,6 +6821,9 @@ _ls() {
-l)
: # Ignore accidental ls -l due to muscle memory.
;;
--no*folders*first|--no*ff|-no*ff|--no*f|-no*f)
_folders_first=0
;;
--no*footer)
NB_FOOTER=0
;;
Expand Down
11 changes: 5 additions & 6 deletions test/list.bats
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ load test_helper
.*[.*1.*].*\ File\ One\.md\ \·\ \"Example\ content\ one\.\" ]]
}

@test "'NB_LIST_FOLDERS_FIRST=1 list' prints folders first." {
@test "'NB_LS_FOLDERS_FIRST=1 list' has no effect." {
{
"${_NB}" init

Expand Down Expand Up @@ -200,7 +200,7 @@ load test_helper
[[ "${lines[4]}" =~ \
.*[.*1.*].*\ File\ One\.md\ \·\ \"Example\ content\ one\.\" ]]

NB_LIST_FOLDERS_FIRST=1 run "${_NB}" list
NB_LS_FOLDERS_FIRST=1 run "${_NB}" list

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
Expand All @@ -211,12 +211,11 @@ load test_helper
[[ "${lines[0]}" =~ \
.*[.*5.*].*\ 📂\ Folder\ Two ]]
[[ "${lines[1]}" =~ \
.*[.*2.*].*\ 📂\ Folder\ One ]]
[[ "${lines[2]}" =~ \
.*[.*4.*].*\ File\ Three\.md\ \·\ \"Example\ content\ three\.\" ]]
[[ "${lines[3]}" =~ \
[[ "${lines[2]}" =~ \
.*[.*3.*].*\ File\ Two\.md\ \·\ \"Example\ content\ two\.\" ]]

[[ "${lines[3]}" =~ \
.*[.*2.*].*\ 📂\ Folder\ One ]]
[[ "${lines[4]}" =~ \
.*[.*1.*].*\ File\ One\.md\ \·\ \"Example\ content\ one\.\" ]]
}
Expand Down
57 changes: 55 additions & 2 deletions test/ls.bats
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ HEREDOC
.*[.*1.*].*\ File\ One\.md\ \·\ \"Example\ content\ one\.\" ]]
}

@test "'NB_LIST_FOLDERS_FIRST=1 ls' prints folders first." {
@test "'NB_LS_FOLDERS_FIRST=1 ls' prints folders first." {
{
"${_NB}" init

Expand Down Expand Up @@ -182,7 +182,7 @@ HEREDOC
[[ "${lines[4]}" =~ \
.*[.*1.*].*\ File\ One\.md\ \·\ \"Example\ content\ one\.\" ]]

NB_LIST_FOLDERS_FIRST=1 run "${_NB}" ls
NB_LS_FOLDERS_FIRST=1 run "${_NB}" ls

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
Expand All @@ -203,6 +203,59 @@ HEREDOC
.*[.*1.*].*\ File\ One\.md\ \·\ \"Example\ content\ one\.\" ]]
}

@test "'NB_LS_FOLDERS_FIRST=1 ls --no-folders-first' prints without folders first." {
{
"${_NB}" init

"${_NB}" add "File One.md" --content "Example content one."
"${_NB}" folder add "Folder One"
"${_NB}" add "File Two.md" --content "Example content two."
"${_NB}" add "File Three.md" --content "Example content three."
"${_NB}" folder add "Folder Two"

export NB_FOOTER=0
export NB_HEADER=0
}

run "${_NB}" ls

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"

[[ "${status}" -eq 0 ]]
[[ "${#lines[@]}" -eq 5 ]]

[[ "${lines[0]}" =~ \
.*[.*5.*].*\ 📂\ Folder\ Two ]]
[[ "${lines[1]}" =~ \
.*[.*4.*].*\ File\ Three\.md\ \·\ \"Example\ content\ three\.\" ]]
[[ "${lines[2]}" =~ \
.*[.*3.*].*\ File\ Two\.md\ \·\ \"Example\ content\ two\.\" ]]
[[ "${lines[3]}" =~ \
.*[.*2.*].*\ 📂\ Folder\ One ]]
[[ "${lines[4]}" =~ \
.*[.*1.*].*\ File\ One\.md\ \·\ \"Example\ content\ one\.\" ]]

NB_LS_FOLDERS_FIRST=1 run "${_NB}" ls --no-folders-first

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"

[[ "${status}" -eq 0 ]]
[[ "${#lines[@]}" -eq 5 ]]

[[ "${lines[0]}" =~ \
.*[.*5.*].*\ 📂\ Folder\ Two ]]
[[ "${lines[1]}" =~ \
.*[.*4.*].*\ File\ Three\.md\ \·\ \"Example\ content\ three\.\" ]]
[[ "${lines[2]}" =~ \
.*[.*3.*].*\ File\ Two\.md\ \·\ \"Example\ content\ two\.\" ]]
[[ "${lines[3]}" =~ \
.*[.*2.*].*\ 📂\ Folder\ One ]]
[[ "${lines[4]}" =~ \
.*[.*1.*].*\ File\ One\.md\ \·\ \"Example\ content\ one\.\" ]]
}

# tags ########################################################################

@test "'--tags' exits with status 0 and prints tags in the current notebook." {
Expand Down

0 comments on commit f4920e7

Please sign in to comment.