v2 / cmd / tools / vpm / search.v
55 lines · 53 sloc · 1.36 KB · 85ff8864c44b48c699ae2d296ffe826cae9696fe
Raw
1module main
2
3import os
4import v.help
5
6fn vpm_search(keywords []string) {
7 search_keys := keywords.map(it.replace('_', '-'))
8 if settings.is_help {
9 help.print_and_exit('search')
10 }
11 if search_keys.len == 0 {
12 vpm_error('specify at least one keyword to search for.')
13 exit(2)
14 }
15 modules := get_all_modules_for_search()
16 installed_modules := get_installed_modules()
17 joined := search_keys.join(', ')
18 mut index := 0
19 for mod in modules {
20 for k in search_keys {
21 if !mod.contains(k) {
22 continue
23 }
24 if index == 0 {
25 println('Search results for `${joined}`:\n')
26 }
27 index++
28 mut parts := mod.split('.')
29 // in case the author isn't present
30 if parts.len == 1 {
31 parts << parts[0]
32 parts[0] = ' '
33 } else {
34 parts[0] = ' by ${parts[0]} '
35 }
36 installed := if mod in installed_modules { ' (installed)' } else { '' }
37 println('${index}. ${parts[1]}${parts[0]}[${mod}]${installed}')
38 break
39 }
40 }
41 if index == 0 {
42 vroot := os.real_path(os.dir(vexe))
43 mut messages := ['No module(s) found for `${joined}` .']
44 for vlibmod in search_keys {
45 if os.is_dir(os.join_path(vroot, 'vlib', vlibmod)) {
46 messages << 'There is already an existing `${vlibmod}` module in vlib, so you can just `import ${vlibmod}` .'
47 }
48 }
49 for m in messages {
50 println(m)
51 }
52 } else {
53 eprintln('\nUse `v install author_name.module_name` to install the module.')
54 }
55}
56