1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
pub fn complete_zsh(bin: &str, usage_cmd: &str) -> String {
    // let cmds = vec![&spec.cmd];
    // let args = render_args(&cmds);
    format!(
        r#"
#compdef {bin}
local curcontext="$curcontext"

# caching config
_usage_{bin}_cache_policy() {{
  if [[ -z "${{lifetime}}" ]]; then
    lifetime=$((60*60*4)) # 4 hours
  fi
  local -a oldp
  oldp=( "$1"(Nms+${{lifetime}}) )
  (( $#oldp ))
}}

_{bin}() {{
  typeset -A opt_args
  local curcontext="$curcontext" spec cache_policy

  zstyle -s ":completion:${{curcontext}}:" cache-policy cache_policy
  if [[ -z $cache_policy ]]; then
    zstyle ":completion:${{curcontext}}:" cache-policy _usage_{bin}_cache_policy
  fi

  if ( [[ -z "${{_usage_{bin}_spec:-}}" ]] || _cache_invalid _usage_{bin}_spec ) \
      && ! _retrieve_cache _usage_{bin}_spec;
  then
    spec="$({usage_cmd})"
    _store_cache _usage_{bin}_spec spec
  fi

  _arguments '*: :( $(usage complete-word -s "$spec" -- "${{words[@]}}" ) )'
  return 0
}}

if [ "$funcstack[1]" = "_{bin}" ]; then
    _{bin} "$@"
else
    compdef _{bin} {bin}
fi

# vim: noet ci pi sts=0 sw=4 ts=4
"#
    )
}

// fn render_args(cmds: &[&SchemaCmd]) -> String {
//     format!("XX")
// }

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_complete_zsh() {
        // let spec = r#"
        // "#;
        // let spec = Spec::parse(&Default::default(), spec).unwrap();
        assert_snapshot!(complete_zsh("mycli", "mycli complete --usage").trim(), @r###"
        #compdef mycli
        local curcontext="$curcontext"

        # caching config
        _usage_mycli_cache_policy() {
          if [[ -z "${lifetime}" ]]; then
            lifetime=$((60*60*4)) # 4 hours
          fi
          local -a oldp
          oldp=( "$1"(Nms+${lifetime}) )
          (( $#oldp ))
        }

        _mycli() {
          typeset -A opt_args
          local curcontext="$curcontext" spec cache_policy

          zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
          if [[ -z $cache_policy ]]; then
            zstyle ":completion:${curcontext}:" cache-policy _usage_mycli_cache_policy
          fi

          if ( [[ -z "${_usage_mycli_spec:-}" ]] || _cache_invalid _usage_mycli_spec ) \
              && ! _retrieve_cache _usage_mycli_spec;
          then
            spec="$(mycli complete --usage)"
            _store_cache _usage_mycli_spec spec
          fi

          _arguments '*: :( $(usage complete-word -s "$spec" -- "${words[@]}" ) )'
          return 0
        }

        if [ "$funcstack[1]" = "_mycli" ]; then
            _mycli "$@"
        else
            compdef _mycli mycli
        fi

        # vim: noet ci pi sts=0 sw=4 ts=4
        "###);
    }
}