aboutsummaryrefslogtreecommitdiffhomepage
path: root/share/functions/type.fish
blob: 138783f71bd7d5bb3582858969fc13fe9f4c715c (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

function type --description "Print the type of a command"

	# Initialize
	set -l res 1
	set -l mode normal
	set -l multi no
	set -l selection all
	set -l IFS \n\ \t

	# Parse options
	set -l names
	if test (count $argv) -gt 0
		for i in (seq (count $argv))
			set -l arg $argv[$i]
			set -l needbreak 0
			while test -n $arg
				set -l flag $arg
				set arg ''
				switch $flag
					case '--*'
						# do nothing; this just prevents it matching the next case
					case '-??*'
						# combined flags
						set -l IFS
						echo -n $flag | read __ flag arg
						set flag -$flag
						set arg -$arg
				end
				switch $flag
					case -t --type
						if test $mode != quiet
							set mode type
						end

					case -p --path
						if test $mode != quiet
							set mode path
						end

					case -P --force-path
						if test $mode != quiet
							set mode path
						end
						set selection files

					case -a --all
						set multi yes

					case -f --no-functions
						set selection files

					case -q --quiet
						set mode quiet

					case -h --help
						__fish_print_help type
						return 0

					case --
						set names $argv[$i..-1]
						set -e names[1]
						set needbreak 1
						break

					case '*'
						set names $argv[$i..-1]
						set needbreak 1
						break
				end
			end
			if test $needbreak -eq 1
				break
			end
		end
	end

	# Check all possible types for the remaining arguments
	for i in $names
		# Found will be set to 1 if a match is found
		set -l found 0

		if test $selection != files

			if functions -q -- $i
				set res 0
				set found 1
				switch $mode
					case normal
						printf (_ '%s is a function with definition\n') $i
						functions $i

					case type
						echo (_ 'function')
				end
				if test $multi != yes
					continue
				end
			end

			if contains -- $i (builtin -n)

				set res 0
				set found 1
				switch $mode
					case normal
						printf (_ '%s is a builtin\n') $i

					case type
						echo (_ 'builtin')
				end
				if test $multi != yes
					continue
				end
			end

		end

		set -l paths
		if test $multi != yes
			set paths (command -s -- $i)
		else
			set paths (command which -a -- $i ^/dev/null)
		end
		for path in $paths
			set res 0
			set found 1
			switch $mode
				case normal
					printf (_ '%s is %s\n') $i $path

				case type
					echo (_ 'file')

				case path
					echo $path
			end
			if test $multi != yes
				continue
			end
		end

		if begin; test $found = 0; and test $mode != quiet; end
			printf (_ "%s: Could not find '%s'\n") type $i >&2
		end

	end

	return $res
end