Просмотр исходного кода

~/.zshrc,~/.bin/up: Fast hg branch detection.

It's quite interesting that even `ruby -e ''` and `python -c ''` take
about 0.03s with >90% system load whereas `git branch` is usually at
0.005s and 0% system load.

And `up some-file` is even faster (0.001s w/ 0% system load), which is
understandable as it mostly does just a few directory hops. But still
quite cool that this now works reliably and fast.

I don't know if it's the gc kicking in or if it just takes that long to
load the standard includes/library, but there's definitively room for
mini-optimization because on slower machines it's too much of an
overhead to use in places like the prompt which has to be responsive.
Lucas Stadler лет назад: 12
Родитель
Сommit
44f062700d
3 измененных файлов с 32 добавлено и 3 удалено
  1. 2 0
      .bin/Makefile
  2. 25 0
      .bin/up.c
  3. 5 3
      .zshrc

+ 2 - 0
.bin/Makefile

@ -0,0 +1,2 @@
1
up: up.c
2
	clang -o up up.c

+ 25 - 0
.bin/up.c

@ -0,0 +1,25 @@
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <unistd.h>
5
6
int main(int argc, char **argv) {
7
	if (argc < 2) {
8
		printf("Usage: %s file-or-dir\n", argv[0]);
9
		return -1;
10
	}
11
12
	int max_path = 2 << 8;
13
	char *path = malloc(sizeof(char) * max_path);
14
	do {
15
		getcwd(path, max_path);
16
		if (access(argv[1], F_OK) == 0) {
17
			printf("%s\n", path);
18
			return 0;
19
		} else {
20
			chdir("..");
21
		}
22
	} while (strcmp("/", path) != 0);
23
24
	return 1;
25
}

+ 5 - 3
.zshrc

@ -16,13 +16,15 @@ setopt autocd extendedglob
16 16
# End of lines configured by zsh-newuser-install
17 17
18 18
function repo_char {
19
	#hg root >/dev/null 2> /dev/null && echo '☿' && return
20
	git branch > /dev/null 2> /dev/null && echo "±" && return
19
	$HOME/.bin/up .hg >/dev/null 2> /dev/null && echo '☿' && return
20
	$HOME/.bin/up .git > /dev/null 2> /dev/null && echo "±" && return
21 21
	echo "∅"
22 22
}
23 23
24 24
function repo_branch {
25
	if git branch > /dev/null 2> /dev/null; then
25
	if $HOME/.bin/up .hg > /dev/null 2> /dev/null; then
26
		echo "%{$fg[green]%}$(cat `$HOME/.bin/up .hg`/.hg/branch)%{$reset_color%}"
27
	elif git branch > /dev/null 2> /dev/null; then
26 28
		echo "%{$fg[green]%}$(git branch --no-color | sed -En 's/^\* (.*)/\1/p')%{$reset_color%}"
27 29
	fi
28 30
}