added syntax highlighting
This commit is contained in:
		
							parent
							
								
									a61ae5ab77
								
							
						
					
					
						commit
						a5075bc9cd
					
				
							
								
								
									
										55
									
								
								en
									
									
									
									
									
								
							
							
						
						
									
										55
									
								
								en
									
									
									
									
									
								
							@ -10,7 +10,14 @@
 | 
				
			|||||||
argv0="$0"
 | 
					argv0="$0"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if test -z "$1"; then
 | 
					if test -z "$1"; then
 | 
				
			||||||
	printf "Usage: %s file...\n" "$argv0"
 | 
						printf "Usage: %s file...\n" "$argv0" 1>&2
 | 
				
			||||||
 | 
						exit 1
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if ! command -v mktemp >/dev/null 2>&1
 | 
				
			||||||
 | 
					then
 | 
				
			||||||
 | 
						printf "%s: mktemp(1): Missing dependency." "$argv0"
 | 
				
			||||||
 | 
						exit 1
 | 
				
			||||||
fi
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
help="Commands:\n\
 | 
					help="Commands:\n\
 | 
				
			||||||
@ -30,7 +37,18 @@ To learn more about a command verb, type \`h verb'.
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
while test -n "$1"; do
 | 
					while test -n "$1"; do
 | 
				
			||||||
	content="$(cat "$1")"
 | 
						content="$(cat "$1")"
 | 
				
			||||||
	cat "$1"
 | 
						file="$(printf "%s\n" "$1" | tr -d '/')"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						# use bat for syntax highlighting if it’s available, but remove the frills
 | 
				
			||||||
 | 
						if command -v bat >/dev/null
 | 
				
			||||||
 | 
						then
 | 
				
			||||||
 | 
							# there has to be a temp file for writing the highlighted text to
 | 
				
			||||||
 | 
							syntax_h() {
 | 
				
			||||||
 | 
								temp_highlight="$(mktemp -t "XXXXX$file")"
 | 
				
			||||||
 | 
								printf "%s\n" "$1" >"$temp_highlight"
 | 
				
			||||||
 | 
								bat -pp --color always "$temp_highlight"
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	while true; do
 | 
						while true; do
 | 
				
			||||||
		printf ">> "
 | 
							printf ">> "
 | 
				
			||||||
@ -45,44 +63,25 @@ while test -n "$1"; do
 | 
				
			|||||||
				clear
 | 
									clear
 | 
				
			||||||
				;;
 | 
									;;
 | 
				
			||||||
			"d")
 | 
								"d")
 | 
				
			||||||
				printf "%s: d: Command not implemented.\n" "$argv0"
 | 
									printf "%s: d: Command not implemented.\n" "$argv0" 1>&2
 | 
				
			||||||
				;;
 | 
									;;
 | 
				
			||||||
			"e")
 | 
								"e")
 | 
				
			||||||
				eval "$args"
 | 
									eval "$args"
 | 
				
			||||||
				;;
 | 
									;;
 | 
				
			||||||
			"h")
 | 
								"h")
 | 
				
			||||||
				if test -z "$args"; then
 | 
									if test -z "$args"; then
 | 
				
			||||||
					printf "$help\n"
 | 
										printf "%s: %s\n" "$argv0" "$help"
 | 
				
			||||||
				else
 | 
									else
 | 
				
			||||||
					printf "%s: Command-specific help not implemented.\n" "$argv0"
 | 
										printf "%s: Command-specific help not implemented.\n" "$argv0" 1>&2
 | 
				
			||||||
					continue
 | 
										continue
 | 
				
			||||||
					case $args in
 | 
					 | 
				
			||||||
						"c")
 | 
					 | 
				
			||||||
							;;
 | 
					 | 
				
			||||||
						"d")
 | 
					 | 
				
			||||||
							;;
 | 
					 | 
				
			||||||
						"e")
 | 
					 | 
				
			||||||
							;;
 | 
					 | 
				
			||||||
						"h")
 | 
					 | 
				
			||||||
							;;
 | 
					 | 
				
			||||||
						"i")
 | 
					 | 
				
			||||||
							;;
 | 
					 | 
				
			||||||
						"p")
 | 
					 | 
				
			||||||
							;;
 | 
					 | 
				
			||||||
						"q")
 | 
					 | 
				
			||||||
							;;
 | 
					 | 
				
			||||||
						"r")
 | 
					 | 
				
			||||||
							;;
 | 
					 | 
				
			||||||
						"w")
 | 
					 | 
				
			||||||
							;;
 | 
					 | 
				
			||||||
					esac
 | 
					 | 
				
			||||||
				fi
 | 
									fi
 | 
				
			||||||
				;;
 | 
									;;
 | 
				
			||||||
			"i")
 | 
								"i")
 | 
				
			||||||
				printf "%s: i: Command not implemented.\n" "$argv0"
 | 
									printf "%s: i: Command not implemented.\n" "$argv0" 1>&2
 | 
				
			||||||
				;;
 | 
									;;
 | 
				
			||||||
			"p")
 | 
								"p")
 | 
				
			||||||
				printf "%s\n" "$content"
 | 
									out="$(syntax_h "$content" || printf "%s\n" "$content")"
 | 
				
			||||||
 | 
									printf "%s" "$out" | nl -ba
 | 
				
			||||||
				;;
 | 
									;;
 | 
				
			||||||
			"q")
 | 
								"q")
 | 
				
			||||||
				if test -n "$(printf "%s\n" "$content" | diff "$1" -)"
 | 
									if test -n "$(printf "%s\n" "$content" | diff "$1" -)"
 | 
				
			||||||
@ -90,7 +89,7 @@ while test -n "$1"; do
 | 
				
			|||||||
					printf "%s: %s: Unsaved changes in the buffer. Quit anyway? [y/N] " \
 | 
										printf "%s: %s: Unsaved changes in the buffer. Quit anyway? [y/N] " \
 | 
				
			||||||
						"$argv0" \
 | 
											"$argv0" \
 | 
				
			||||||
						"$1"
 | 
											"$1"
 | 
				
			||||||
					read quit
 | 
										read -r quit
 | 
				
			||||||
					if [ "$quit" = "y" ]; then
 | 
										if [ "$quit" = "y" ]; then
 | 
				
			||||||
						break
 | 
											break
 | 
				
			||||||
					else
 | 
										else
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user