From 03b2e320a7fc3d7567cf5e805a20de7e646aa784 Mon Sep 17 00:00:00 2001 From: Haruaki Tamada Date: Wed, 10 Jul 2024 15:53:27 +0900 Subject: [PATCH 1/5] feat: introduce version update script --- .bin/update_version.sh | 7 +++---- README.md | 6 +++--- docs/config.toml | 2 +- docs/content/_index.md | 4 ++-- docs/layouts/shortcodes/version.html | 5 +++++ 5 files changed, 14 insertions(+), 10 deletions(-) create mode 100644 docs/layouts/shortcodes/version.html diff --git a/.bin/update_version.sh b/.bin/update_version.sh index 64e4382..466dbec 100755 --- a/.bin/update_version.sh +++ b/.bin/update_version.sh @@ -15,11 +15,10 @@ if [[ "$PREV_VERSION" != "" && $VERSION == $PREV_VERSION ]]; then exit 1 fi -for i in README.md ; do - sed -e "s#Version-${PREV_VERSION}-information#Version-${VERSION//-/--}-information#g" -e "s#tag/v${PREV_VERSION}#tag/v${VERSION}#g" $i > a - mv a $i -done +echo "$PREV_VERSION -> $VERSION (${PREV_VERSION//-/--} -> ${VERSION//-/--})" +sed -e "s#Version-${PREV_VERSION//-/--}-information#Version-${VERSION//-/--}-information#g" -e "s#tag/v${PREV_VERSION}#tag/v${VERSION}#g" README.md > a; mv a README.md +sed -e "s#version = \".*\"#version = \"${VERSION}\"#g" docs/config.toml > a ; mv a docs/config.toml sed "s/^version= /version = "${VERSION}"/g" Cargo.toml > a && mv a Cargo.toml echo "Replace version from \"${PREV_VERSION}\" to \"${VERSION}\"" diff --git a/README.md b/README.md index a8b3e35..18a1f2b 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@ [![Rust Report Card](https://rust-reportcard.xuri.me/badge/github.com/tamada/sibling)](https://rust-reportcard.xuri.me/report/github.com/tamada/sibling) -[![License](https://img.shields.io/badge/License-WTFPL-green.svg)](https://github.com/tamada/sibling/blob/master/LICENSE) -[![Version](https://img.shields.io/badge/Version-2.0.0--beta--1-green.svg)](https://github.com/tamada/sibling/releases/tag/v2.0.0-beta-1) +[![License](https://img.shields.io/badge/License-WTFPL-information.svg)](https://github.com/tamada/sibling/blob/master/LICENSE) +[![Version](https://img.shields.io/badge/Version-2.0.0--beta--1-information.svg)](https://github.com/tamada/sibling/releases/tag/v2.0.0-beta-1) get the next/previous sibling directory name. @@ -88,7 +88,7 @@ The `--init` option accepts only `bash`. Other shell scripts are not supported, yet. ```shell -$(sibling --init bash) +eval "$(sibling --init bash)" ``` ### :muscle: Compiling yourself diff --git a/docs/config.toml b/docs/config.toml index 8f1725c..9e68cab 100644 --- a/docs/config.toml +++ b/docs/config.toml @@ -15,7 +15,7 @@ pygmentsStyle = "pygments" project_tagline = "get next/previous sibling directory name" dateFormat = "2006-01-02" katex = true - version = "1.3.0" + version = "2.0.0-beta-1" footer = "[![GitHub](https://img.shields.io/badge/GitHub-tamada/sibling-blueviolet.svg?logo=github)](https://github.com/tamada/sibling) Made with [Hugo](https://gohugo.io/). Theme by [Cayman](https://github.com/zwbetz-gh/cayman-hugo-theme). Deployed to [GitHub Pages](https://pages.github.com/)." [menu] diff --git a/docs/content/_index.md b/docs/content/_index.md index a99387b..924fad0 100644 --- a/docs/content/_index.md +++ b/docs/content/_index.md @@ -8,8 +8,8 @@ title: ":house: Home" [![Rust Report Card](https://rust-reportcard.xuri.me/badge/github.com/tamada/sibling)](https://rust-reportcard.xuri.me/report/github.com/tamada/sibling) -[![License](https://img.shields.io/badge/License-WTFPL-green.svg)](https://github.com/tamada/sibling/blob/master/LICENSE) -[![Version](https://img.shields.io/badge/Version-{{ .Site.Params.version }}-green.svg)](https://github.com/tamada/sibling/releases/tag/v{{ .Site.Params.version }}) +[![License](https://img.shields.io/badge/License-WTFPL-information.svg)](https://github.com/tamada/sibling/blob/master/LICENSE) +[![Version](https://img.shields.io/badge/Version-{{< version double_dash="true" >}}-information.svg)](https://github.com/tamada/sibling/releases/tag/v{{< version >}}) ## :speaking_head: Description diff --git a/docs/layouts/shortcodes/version.html b/docs/layouts/shortcodes/version.html new file mode 100644 index 0000000..9eb493b --- /dev/null +++ b/docs/layouts/shortcodes/version.html @@ -0,0 +1,5 @@ +{{- $version := $.Site.Params.version }} +{{- if eq (.Get "double_dash") "true" -}} +{{- $version = strings.Replace $version "-" "--" -}} +{{- end -}} +{{ $version }} \ No newline at end of file From dd88a3a3147e4e1b8ba0721c1b0e4fcc2c02ef7c Mon Sep 17 00:00:00 2001 From: Haruaki Tamada Date: Thu, 18 Jul 2024 07:45:22 +0900 Subject: [PATCH 2/5] eliminate duplicated blank lines --- .github/workflows/publish.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 3b464e3..1b915e0 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -46,8 +46,9 @@ jobs: extended: true - name: Build Site - run: + run: | (cd docs ; hugo) + - name: Create Distributions run: From 946ae287b4a8681db9f0552def6ba7a1a4b75da8 Mon Sep 17 00:00:00 2001 From: Haruaki Tamada Date: Thu, 18 Jul 2024 07:51:20 +0900 Subject: [PATCH 3/5] fix the failure of GitHub Action script --- .github/workflows/versionup.yaml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.github/workflows/versionup.yaml b/.github/workflows/versionup.yaml index 8f753e6..a2bbbc4 100644 --- a/.github/workflows/versionup.yaml +++ b/.github/workflows/versionup.yaml @@ -2,7 +2,7 @@ name: Version Up on: push: branches: - - 'release/v[0-9]+.[0-9]+.[0-9]*' + - 'release/v[0-9]+.[0-9]+.[0-9]*.*' jobs: versionup: @@ -16,9 +16,6 @@ jobs: - name: Checkout uses: actions/checkout@v4 - with: - ref: ${{ steps.vars.outputs.branch }} - fetch-depth: 0 - name: Initialize shell: bash @@ -26,8 +23,6 @@ jobs: git config --global user.name "GitHub Actions" git config --global user.email "action@github.com" auth_header="$(git config --local --get http.https://github.com/.extraheader)" - git submodule sync --recursive - git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1 - name: Version up id: updating_version From feed37de302117a758b65e8b4f74dc67f9e40d8c Mon Sep 17 00:00:00 2001 From: Haruaki Tamada Date: Thu, 18 Jul 2024 11:09:31 +0900 Subject: [PATCH 4/5] add completion script to assets --- assets/completions/bash/sibling | 66 +++++++++++++++++++++++++++ assets/completions/elvish/sibling | 43 +++++++++++++++++ assets/completions/fish/sibling | 10 ++++ assets/completions/powershell/sibling | 49 ++++++++++++++++++++ assets/completions/zsh/_sibling | 51 +++++++++++++++++++++ assets/{ => init}/init.bash | 0 src/init.rs | 2 +- 7 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 assets/completions/bash/sibling create mode 100644 assets/completions/elvish/sibling create mode 100644 assets/completions/fish/sibling create mode 100644 assets/completions/powershell/sibling create mode 100644 assets/completions/zsh/_sibling rename assets/{ => init}/init.bash (100%) diff --git a/assets/completions/bash/sibling b/assets/completions/bash/sibling new file mode 100644 index 0000000..05c8929 --- /dev/null +++ b/assets/completions/bash/sibling @@ -0,0 +1,66 @@ +_sibling() { + local i cur prev opts cmd + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + cmd="" + opts="" + + for i in ${COMP_WORDS[@]} + do + case "${cmd},${i}" in + ",$1") + cmd="sibling" + ;; + *) + ;; + esac + done + + case "${cmd}" in + sibling) + opts="-a -l -p -P -s -i -t -h -V --csv --absolute --list --progress --parent --step --init --type --help --version [DIR]..." + if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + fi + case "${prev}" in + --step) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + -s) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --init) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + -i) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + --type) + COMPREPLY=($(compgen -W "first last previous next random keep" -- "${cur}")) + return 0 + ;; + -t) + COMPREPLY=($(compgen -W "first last previous next random keep" -- "${cur}")) + return 0 + ;; + *) + COMPREPLY=() + ;; + esac + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + ;; + esac +} + +if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then + complete -F _sibling -o nosort -o bashdefault -o default sibling +else + complete -F _sibling -o bashdefault -o default sibling +fi diff --git a/assets/completions/elvish/sibling b/assets/completions/elvish/sibling new file mode 100644 index 0000000..9926c89 --- /dev/null +++ b/assets/completions/elvish/sibling @@ -0,0 +1,43 @@ + +use builtin; +use str; + +set edit:completion:arg-completer[sibling] = {|@words| + fn spaces {|n| + builtin:repeat $n ' ' | str:join '' + } + fn cand {|text desc| + edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc + } + var command = 'sibling' + for word $words[1..-1] { + if (str:has-prefix $word '-') { + break + } + set command = $command';'$word + } + var completions = [ + &'sibling'= { + cand -s 'specify the number of times to execute sibling' + cand --step 'specify the number of times to execute sibling' + cand -i 'generate the initialize script for the shell' + cand --init 'generate the initialize script for the shell' + cand -t 'specify the nexter type' + cand --type 'specify the nexter type' + cand --csv 'print the result in the csv format' + cand -a 'print the directory name in the absolute path' + cand --absolute 'print the directory name in the absolute path' + cand -l 'list the sibling directories' + cand --list 'list the sibling directories' + cand -p 'print the progress of traversing directories' + cand --progress 'print the progress of traversing directories' + cand -P 'print parent directory, when no more sibling directories are found' + cand --parent 'print parent directory, when no more sibling directories are found' + cand -h 'Print help' + cand --help 'Print help' + cand -V 'Print version' + cand --version 'Print version' + } + ] + $completions[$command] +} diff --git a/assets/completions/fish/sibling b/assets/completions/fish/sibling new file mode 100644 index 0000000..4215ca9 --- /dev/null +++ b/assets/completions/fish/sibling @@ -0,0 +1,10 @@ +complete -c sibling -s s -l step -d 'specify the number of times to execute sibling' -r +complete -c sibling -s i -l init -d 'generate the initialize script for the shell' -r +complete -c sibling -s t -l type -d 'specify the nexter type' -r -f -a "{first\t'',last\t'',previous\t'',next\t'',random\t'',keep\t''}" +complete -c sibling -l csv -d 'print the result in the csv format' +complete -c sibling -s a -l absolute -d 'print the directory name in the absolute path' +complete -c sibling -s l -l list -d 'list the sibling directories' +complete -c sibling -s p -l progress -d 'print the progress of traversing directories' +complete -c sibling -s P -l parent -d 'print parent directory, when no more sibling directories are found' +complete -c sibling -s h -l help -d 'Print help' +complete -c sibling -s V -l version -d 'Print version' diff --git a/assets/completions/powershell/sibling b/assets/completions/powershell/sibling new file mode 100644 index 0000000..3999568 --- /dev/null +++ b/assets/completions/powershell/sibling @@ -0,0 +1,49 @@ + +using namespace System.Management.Automation +using namespace System.Management.Automation.Language + +Register-ArgumentCompleter -Native -CommandName 'sibling' -ScriptBlock { + param($wordToComplete, $commandAst, $cursorPosition) + + $commandElements = $commandAst.CommandElements + $command = @( + 'sibling' + for ($i = 1; $i -lt $commandElements.Count; $i++) { + $element = $commandElements[$i] + if ($element -isnot [StringConstantExpressionAst] -or + $element.StringConstantType -ne [StringConstantType]::BareWord -or + $element.Value.StartsWith('-') -or + $element.Value -eq $wordToComplete) { + break + } + $element.Value + }) -join ';' + + $completions = @(switch ($command) { + 'sibling' { + [CompletionResult]::new('-s', 's', [CompletionResultType]::ParameterName, 'specify the number of times to execute sibling') + [CompletionResult]::new('--step', 'step', [CompletionResultType]::ParameterName, 'specify the number of times to execute sibling') + [CompletionResult]::new('-i', 'i', [CompletionResultType]::ParameterName, 'generate the initialize script for the shell') + [CompletionResult]::new('--init', 'init', [CompletionResultType]::ParameterName, 'generate the initialize script for the shell') + [CompletionResult]::new('-t', 't', [CompletionResultType]::ParameterName, 'specify the nexter type') + [CompletionResult]::new('--type', 'type', [CompletionResultType]::ParameterName, 'specify the nexter type') + [CompletionResult]::new('--csv', 'csv', [CompletionResultType]::ParameterName, 'print the result in the csv format') + [CompletionResult]::new('-a', 'a', [CompletionResultType]::ParameterName, 'print the directory name in the absolute path') + [CompletionResult]::new('--absolute', 'absolute', [CompletionResultType]::ParameterName, 'print the directory name in the absolute path') + [CompletionResult]::new('-l', 'l', [CompletionResultType]::ParameterName, 'list the sibling directories') + [CompletionResult]::new('--list', 'list', [CompletionResultType]::ParameterName, 'list the sibling directories') + [CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'print the progress of traversing directories') + [CompletionResult]::new('--progress', 'progress', [CompletionResultType]::ParameterName, 'print the progress of traversing directories') + [CompletionResult]::new('-P', 'P ', [CompletionResultType]::ParameterName, 'print parent directory, when no more sibling directories are found') + [CompletionResult]::new('--parent', 'parent', [CompletionResultType]::ParameterName, 'print parent directory, when no more sibling directories are found') + [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') + [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') + [CompletionResult]::new('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version') + [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version') + break + } + }) + + $completions.Where{ $_.CompletionText -like "$wordToComplete*" } | + Sort-Object -Property ListItemText +} diff --git a/assets/completions/zsh/_sibling b/assets/completions/zsh/_sibling new file mode 100644 index 0000000..b890783 --- /dev/null +++ b/assets/completions/zsh/_sibling @@ -0,0 +1,51 @@ +#compdef sibling + +autoload -U is-at-least + +_sibling() { + typeset -A opt_args + typeset -a _arguments_options + local ret=1 + + if is-at-least 5.2; then + _arguments_options=(-s -S -C) + else + _arguments_options=(-s -C) + fi + + local context curcontext="$curcontext" state line + _arguments "${_arguments_options[@]}" : \ +'-s+[specify the number of times to execute sibling]:COUNT: ' \ +'--step=[specify the number of times to execute sibling]:COUNT: ' \ +'-i+[generate the initialize script for the shell]:SHELL: ' \ +'--init=[generate the initialize script for the shell]:SHELL: ' \ +'-t+[specify the nexter type]:TYPE:(first last previous next random keep)' \ +'--type=[specify the nexter type]:TYPE:(first last previous next random keep)' \ +'--csv[print the result in the csv format]' \ +'-a[print the directory name in the absolute path]' \ +'--absolute[print the directory name in the absolute path]' \ +'-l[list the sibling directories]' \ +'--list[list the sibling directories]' \ +'-p[print the progress of traversing directories]' \ +'--progress[print the progress of traversing directories]' \ +'-P[print parent directory, when no more sibling directories are found]' \ +'--parent[print parent directory, when no more sibling directories are found]' \ +'-h[Print help]' \ +'--help[Print help]' \ +'-V[Print version]' \ +'--version[Print version]' \ +'*::dirs -- the target directory:_files' \ +&& ret=0 +} + +(( $+functions[_sibling_commands] )) || +_sibling_commands() { + local commands; commands=() + _describe -t commands 'sibling commands' commands "$@" +} + +if [ "$funcstack[1]" = "_sibling" ]; then + _sibling "$@" +else + compdef _sibling sibling +fi diff --git a/assets/init.bash b/assets/init/init.bash similarity index 100% rename from assets/init.bash rename to assets/init/init.bash diff --git a/src/init.rs b/src/init.rs index 3b44065..0ec592f 100644 --- a/src/init.rs +++ b/src/init.rs @@ -3,7 +3,7 @@ use rust_embed::Embed; use crate::cli::{Result, SiblingError}; #[derive(Embed)] -#[folder = "assets"] +#[folder = "assets/init"] struct Assets; pub(crate) fn generate_init_script(shell_name: String) -> Result { From f84e4ed8b25e50c3da5b9391238e9c13fd48de53 Mon Sep 17 00:00:00 2001 From: Haruaki Tamada Date: Thu, 18 Jul 2024 11:44:02 +0900 Subject: [PATCH 5/5] fix cli parse error on giving no arguments However, this change is quite ugly program, therefore, it should be fix soon. --- src/cli.rs | 6 ++-- src/dirs.rs | 14 +++++++-- src/main.rs | 78 ++++++++++++++++++++++++++++++++++++++++++-------- src/nexter.rs | 9 ++++-- src/printer.rs | 10 +++---- 5 files changed, 91 insertions(+), 26 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 0bdc64f..49ecd98 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -38,14 +38,14 @@ pub struct CliOpts { #[arg(short, long, help = "specify the number of times to execute sibling", value_name = "COUNT", default_value_t = 1)] pub step: i32, - #[arg(short, long, help = "generate the initialize script for the shell", value_name = "SHELL", hide = true)] + #[arg(short, long, help = "generate the initialize script for the shell", value_name = "SHELL", hide = true, default_missing_value = "bash")] pub init: Option, #[arg(short = 't', long = "type", help = "specify the nexter type", value_enum, default_value_t = NexterType::Next, value_name = "TYPE", ignore_case = true)] pub nexter: NexterType, - #[arg(index = 1, help = "the directory for listing the siblings", value_name = "DIR", default_value = ".")] - pub dir: PathBuf, + #[arg(index = 1, help = "the target directory", value_name = "DIR")] + pub dirs: Vec, } #[derive(Debug, Parser)] diff --git a/src/dirs.rs b/src/dirs.rs index 68b1929..4d8f773 100644 --- a/src/dirs.rs +++ b/src/dirs.rs @@ -15,7 +15,7 @@ impl Dirs { if current_dir == PathBuf::from(".") { match std::env::current_dir() { Ok(dir) => - build_dirs(current_dir.parent(), dir), + build_dirs(dir.clone().parent(), dir), Err(e) => Err(SiblingError::Io(e)), } } else if current_dir.exists() { @@ -92,10 +92,18 @@ mod tests { #[test] fn test_dirs_new() { - let dirs = Dirs::new(PathBuf::from("testdata/b")); + let dirs = Dirs::new(PathBuf::from("testdata/d")); assert!(dirs.is_ok()); let dirs = dirs.unwrap(); assert_eq!(dirs.dirs.len(), 26); - assert_eq!(dirs.current, 1); + assert_eq!(dirs.current, 3); + } + + #[test] + fn test_dir_dot() { + let dirs = Dirs::new(PathBuf::from(".")); + assert!(dirs.is_ok()); + let dirs = dirs.unwrap(); + assert_eq!(dirs.current_path().file_name().map(|s| s.to_str()), Some("sibling".into())); } } diff --git a/src/main.rs b/src/main.rs index edecced..01b1e45 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use clap::Parser; use crate::cli::{CliOpts, PrintingOpts, Result, SiblingError}; use crate::nexter::Nexter; @@ -8,20 +10,43 @@ mod init; mod nexter; mod printer; -fn perform_impl(mut dirs: dirs::Dirs, nexter: Box, step: i32, opts: PrintingOpts) -> Result { +fn perform_impl(mut dirs: dirs::Dirs, nexter: &Box, step: i32, opts: &PrintingOpts) -> Result { nexter.next(&mut dirs, step); printer::result_string(&dirs, opts) } -fn perform(opts: CliOpts) -> Result { +fn perform_each(dir: std::path::PathBuf, nexter: &Box, step: i32, opts: &PrintingOpts) -> Result { + match dirs::Dirs::new(dir) { + Err(e) => Err(e), + Ok(dirs) => perform_impl(dirs, nexter, step, opts), + } +} + +fn perform_sibling(opts: CliOpts) -> Vec> { + let nexter = nexter::build_nexter(opts.nexter); + let target_dirs = if opts.dirs.is_empty() { + vec![std::env::current_dir().unwrap()] + } else { + opts.dirs + }; + let mut result = vec![]; + for dir in target_dirs { + let dir = if dir == PathBuf::from(".") { + std::env::current_dir().unwrap() + } else { + std::path::PathBuf::from(dir) + }; + let r = perform_each(dir, &nexter, opts.step, &opts.p_opts); + result.push(r); + } + result +} + +fn perform(opts: CliOpts) -> Vec> { if let Some(shell) = opts.init { - init::generate_init_script(shell) + vec![init::generate_init_script(shell)] } else { - let nexter = nexter::build_nexter(opts.nexter); - match dirs::Dirs::new(opts.dir) { - Err(e) => Err(e), - Ok(dirs) => perform_impl(dirs, nexter, opts.step, opts.p_opts), - } + perform_sibling(opts) } } @@ -39,9 +64,38 @@ fn print_error(e: &SiblingError) { } fn main() { - let opts = cli::CliOpts::parse(); - match perform(opts) { - Ok(result) => println!("{}", result), - Err(e) => print_error(&e), + let mut args = std::env::args(); + let args = if args.len() == 1 { + vec![args.next().unwrap(), ".".into()] + } else { + args.collect() + }; + let opts = cli::CliOpts::parse_from(args); + for item in perform(opts) { + match item { + Ok(result) => println!("{}", result), + Err(e) => print_error(&e), + } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_nexter_example() { + let opts_r = cli::CliOpts::try_parse_from(vec!["sibling", "."]); + + if let Err(e) = &opts_r { + eprintln!("{}", e); + } + assert!(opts_r.is_ok()); + let r = perform(opts_r.unwrap()); + assert_eq!(r.len(), 1); + match r.get(0).unwrap() { + Err(e) => print_error(&e), + Ok(result) => println!("{}", result), + } + } +} \ No newline at end of file diff --git a/src/nexter.rs b/src/nexter.rs index 090fee1..ebd0868 100644 --- a/src/nexter.rs +++ b/src/nexter.rs @@ -83,7 +83,6 @@ fn next_impl(dirs: &mut Dirs, step: i32) -> Option { } else { dirs.dirs.get(dirs.next as usize) }; - dirs.current = dirs.next as usize; r.map(|f| f.to_path_buf()) } @@ -122,7 +121,7 @@ mod tests { Some(p) => assert_eq!(p, PathBuf::from("testdata/d")), None => panic!("unexpected None"), } - match nexter.next(&mut dirs, 1) { + match nexter.next(&mut dirs, 2) { Some(p) => assert_eq!(p, PathBuf::from("testdata/e")), None => panic!("unexpected None"), } @@ -144,8 +143,12 @@ mod tests { Some(p) => assert_eq!(p, PathBuf::from("testdata/j")), None => panic!("unexpected None"), } + match nexter.next(&mut dirs, 1) { + Some(p) => assert_eq!(p, PathBuf::from("testdata/j")), + None => panic!("unexpected None"), + } match nexter.next(&mut dirs, 4) { - Some(p) => assert_eq!(p, PathBuf::from("testdata/f")), + Some(p) => assert_eq!(p, PathBuf::from("testdata/g")), None => panic!("unexpected None"), } match nexter.next(&mut dirs, 26) { diff --git a/src/printer.rs b/src/printer.rs index 33bc7d4..da3c5ad 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -3,7 +3,7 @@ use std::path::PathBuf; use crate::cli::{PrintingOpts, Result}; use crate::dirs::Dirs; -pub(crate) fn result_string(dirs: &Dirs, opts: PrintingOpts) -> Result { +pub(crate) fn result_string(dirs: &Dirs, opts: &PrintingOpts) -> Result { if opts.csv { csv_string(dirs, opts.absolute) } else if dirs.no_more_dir { @@ -22,7 +22,7 @@ fn csv_string(dirs: &Dirs, absolute: bool) -> Result { dirs.current + 1, dirs.next + 1, dirs.dirs.len())) } -fn no_more_dir_string(dirs: &Dirs, opts: PrintingOpts) -> Result { +fn no_more_dir_string(dirs: &Dirs, opts: &PrintingOpts) -> Result { if opts.parent { Ok(pathbuf_to_string(Some(dirs.parent.clone()), opts.absolute)) } else { @@ -30,7 +30,7 @@ fn no_more_dir_string(dirs: &Dirs, opts: PrintingOpts) -> Result { } } -fn list_string(dirs: &Dirs, opts: PrintingOpts) -> Result { +fn list_string(dirs: &Dirs, opts: &PrintingOpts) -> Result { let mut result = vec![]; for (i, dir) in dirs.dirs.iter().enumerate() { let prefix = if i == dirs.next as usize { "> " } @@ -39,12 +39,12 @@ fn list_string(dirs: &Dirs, opts: PrintingOpts) -> Result { } else { " " }; - result.push(format!("{}{}: {}", i + 1, prefix, pathbuf_to_string(Some(dir.to_path_buf()), opts.absolute))); + result.push(format!("{:>4} {}{}", i + 1, prefix, pathbuf_to_string(Some(dir.to_path_buf()), opts.absolute))); } Ok(result.join("\n")) } -fn result_string_impl(dirs: &Dirs, opts: PrintingOpts) -> Result { +fn result_string_impl(dirs: &Dirs, opts: &PrintingOpts) -> Result { let r = if opts.progress { format!("{} ({}/{})", pathbuf_to_string(dirs.next_path(), opts.absolute), dirs.next + 1, dirs.dirs.len()) } else {