Salut à tous !

Si quelqu'un veut discuter du code ci-dessous

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
107
108
109
110
111
112
113
114
115
116
117
 
#-*-shell-script-*-
 
export NOTES_TXT=~/NOTES/TXT/
export NOTES_LOG=~/NOTES/LOG/
 
#        /\          /\          /\
#     /\//\\/\    /\//\\/\    /\//\\/\
#  /\//\\\///\\/\//\\\///\\/\//\\\///\\/\
# //\\\//\/\\///\\\//\/\\///\\\//\/\\///\\
# \\//\/                            \/\\//
#  \/                                  \/
#  /\                                  /\
# //\\      The search function       //\\
# \\//                                \\//
#  \/                                  \/
#  /\                                  /\
# //\\/\                            /\//\\
# \\///\\/\//\\\///\\/\//\\\///\\/\//\\\//
#  \/\\///\\\//\/\\///\\\//\/\\///\\\//\/
#     \/\\//\/    \/\\//\/    \/\\//\/
#        \/          \/          \/
 
function notes.search {
    # examples
    # notes.search --log --newer-than 30 -E "(streaming.*TDA|TDA.*streaming)"
    # notes.search --txt -F --headings "nextcloud"
    # notes.search --all -F "streaming"
    # notes.search --all --newer-than 30 -F "streaming"
    # notes.search --all --recent "streaming"
 
    local search_dirs="$NOTES_TXT"
    local find_recent
    local headings_prefix
    local pattern
    local grep_opts grep_opts_debug
    local ls_opts="-t"
 
    # echo "function args $@"
    TEMP=$(getopt -uo EFr -l all,log,txt,LOG,TXT,newer-than:,headings,recent,all -- $@)
    # echo "TEMP $TEMP"
    set -- $TEMP
    while (($# > 0))
    do
	# echo "looking at $1"
	case "$1" in
	    --all)
		search_dirs="$NOTES_TXT $NOTES_LOG"
		;;
 
	    --log|--LOG)
		search_dirs="$NOTES_LOG"
		;;
 
	    --txt|--TXT)
	        search_dirs="$NOTES_TXT"
		;;
 
	    --newer-than)
		shift
		find_recent="-mtime -$1"
		;;
 
	    --heading?)
		headings_prefix="^\*.* "
		;;
 
	    --recent)
		find_recent="-mtime 30"
		;;
 
	    -E)
		grep_opts="-E"
		;;
 
	    -F)
		grep_opts="-F"
		;;
	    -r)
		ls_opts="-rt"
		;;
	    --)
		# echo "parsing -- : arg before shift $1"
		shift
		# echo "parsing -- : arg after shift $1"
		pattern="$*"
		# echo "parsing -- : pattern is $pattern"
		;;
	esac
	shift 
    done
 
 
    [[ -z $pattern ]] && (echo "no pattern specified"; notes.search.usage; return 1)
    [[ -n $grep_opts ]] && grep_opts_debug="with the $grep_opts option"
    > /tmp/notes.search.results.colors
    find $search_dirs -not -name "*~" -type f $find_recent | xargs ls "$ls_opts" | while read file
    do
	grep --color=always -H -n -i $grep_opts "$headings_prefix$pattern" "$file"  | tee -a /tmp/notes.search.results.colors
    done    
 
    pretty.colors.remove /tmp/notes.search.results.colors > /tmp/notes.search.results
}
 
function notes.search.usage {
    echo "usage : notes.search.new <options> <pattern>"
    echo "options :"
    echo "                  -E : regex search"
    echo "                  -F : fast search"
    echo "                  -r : reverse order (newer first)"
    echo "               --all : search all notes"
    echo "               --txt : search text notes only (default)"
    echo "               --log : search logs only "
    echo " --newer-than <days> : useful with --log; search only in files that were modified in the last <days> days"
    echo "            --recent : useful with --log; search only in files that were modified in the last 30 days"
    echo "          --headings : search only in headings"
}
particulièrement :

- comment gérer les arguments avec espace quand on utilise getopt
- si au contraire je devrais réécrire le traitement des arguments en ligne de commande à la mano.