|
| 1 | +module Bashly |
| 2 | + module Commands |
| 3 | + class Doc < Base |
| 4 | + summary 'Show bashly reference documentation' |
| 5 | + help 'This command displays bite-sized help for all the bashly configuration options in the terminal.' |
| 6 | + |
| 7 | + usage 'bashly doc [SEARCH] [--index]' |
| 8 | + usage 'bashly doc (-h|--help)' |
| 9 | + |
| 10 | + option '-i --index', 'Show option keys only' |
| 11 | + param 'SEARCH', 'Search for options that match this text' |
| 12 | + |
| 13 | + example 'bashly doc command' |
| 14 | + example 'bashly doc command.flags' |
| 15 | + example 'bashly doc flag. -i' |
| 16 | + example 'bashly doc catch_all' |
| 17 | + |
| 18 | + def run |
| 19 | + if args['--index'] |
| 20 | + puts data.keys |
| 21 | + else |
| 22 | + show |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + private |
| 27 | + |
| 28 | + def show |
| 29 | + data.each do |key, info| |
| 30 | + show_key key |
| 31 | + show_help info['help'] |
| 32 | + show_example info['example'] if info['example'] |
| 33 | + show_url info['url'] if info['url'] |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + def show_key(key) |
| 38 | + say "!txtgrn!#{key}" |
| 39 | + say '' |
| 40 | + end |
| 41 | + |
| 42 | + def show_url(url) |
| 43 | + say " See !undblu!#{url}" |
| 44 | + say '' |
| 45 | + end |
| 46 | + |
| 47 | + def show_example(example) |
| 48 | + example = word_wrap " #{example}" |
| 49 | + example.gsub!(/^(\s*- )?(\s*\w+):/, '\1!txtblu!\2!txtrst!:') |
| 50 | + example.gsub!(/^(\s*- )/, '!txtylw!\1!txtrst!') |
| 51 | + example.gsub!(/^(\s*#.+)/, '!txtpur!\1!txtrst!') |
| 52 | + say example |
| 53 | + say '' |
| 54 | + end |
| 55 | + |
| 56 | + def show_help(help) |
| 57 | + help = word_wrap " #{help}" |
| 58 | + help.gsub!(/`([^`]+)`/, '!txtgrn!\1!txtrst!') |
| 59 | + say help |
| 60 | + say '' |
| 61 | + end |
| 62 | + |
| 63 | + def data |
| 64 | + return raw_data unless args['SEARCH'] |
| 65 | + |
| 66 | + result = raw_data.select { |k, _v| k.== args['SEARCH'] } |
| 67 | + return result if result.any? |
| 68 | + |
| 69 | + result = raw_data.select { |k, _v| k.include? args['SEARCH'] } |
| 70 | + return result if result.any? |
| 71 | + |
| 72 | + raise Error, "No match" |
| 73 | + end |
| 74 | + |
| 75 | + def raw_data |
| 76 | + @raw_data ||= begin |
| 77 | + result = {} |
| 78 | + Dir["#{docs_dir}/*.yml"].sort.each do |path| |
| 79 | + result.merge! YAML.load_file(path) |
| 80 | + end |
| 81 | + result |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + def docs_dir |
| 86 | + @docs_dir ||= File.expand_path '../docs', __dir__ |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | +end |
0 commit comments