Example #1
0
func runGitLog(debug *bool) *bufio.Scanner {
	gitargs := setupGitLogArgs()
	if *debug {
		fmt.Println("git", strings.Join(gitargs, " "))
	}

	_, stdout, stderr := piper.MustPipe("git", gitargs...)

	go func() {
		for stderr.Scan() {
			fmt.Fprintln(os.Stderr, stderr.Text())
		}
	}()

	return stdout
}
Example #2
0
func main() {
	chdir := flag.String(
		"C", "", docStr(
			"Change to given directory before doing anything else"))
	clean := flag.Bool(
		"c", false, docStr(
			"Clear output when done. Leave no fan trace."))
	duration := flag.Int64(
		"d", 0, docStr(
			"Show estimated time remaining based on N seconds total",
			"runtime. Setting N to 0 turns off the remaining time",
			"estimation. If runtime exceeds N, ?s is shown."))
	freq := flag.Int(
		"e", 1, docStr("Fan speed. Lower is faster."))
	echo := flag.Bool(
		"P", false, docStr(
			"Echo whatever was read from stdin to stdout."))
	quiet := flag.Bool(
		"q", false, docStr(
			"Do not show fan."))
	record := flag.String(
		"R", "", docStr(
			"If file does not exist, record the duration and number of",
			"lines read from stdin into <file>. If file exist, use the",
			"values in <file> for the -t and -d arguments."))
	saveNewRecord := flag.Bool(
		"r", false, docStr(
			"When done, record the duration and number of",
			"lines read from stdin into <file> given by -R."))
	tLines := flag.Int(
		"t", 0, docStr(
			"Show estimated completion percentage based on N lines of max",
			"input. Setting N to 0 turns off the percentage estimation. If",
			"input lines is more than N, ?% will be shown instead."))
	title := flag.String(
		"T", "", docStr(
			"Print given title before the fan. If title is '-', and <args>",
			"is supplied, then use <args> as the title"))

	flag.Usage = func() {
		fmt.Println(usage)
		flag.PrintDefaults()
	}

	flag.Parse()
	if *chdir != "" {
		err := os.Chdir(*chdir)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			return
		}
	}

	lastLen := 0
	nLines := 0

	cmd := ""
	var proc *exec.Cmd
	var in, stderr *bufio.Scanner
	if flag.NArg() > 0 {
		cmd = strings.Join(flag.Args(), " ")
		if *record == "" {
			*record = safeFileName(cmd)
		}
		proc, in, stderr = piper.MustPipe("/bin/sh", "-c", cmd)
		go func() {
			for stderr.Scan() {
				fmt.Println(stderr.Text())
			}
		}()
	} else {
		in = bufio.NewScanner(os.Stdin)
	}

	if *record != "" {
		content := readFile(*record)
		*duration, *tLines = readRecord(&content)
	}

	startTime = time.Now().Unix()
	if *title != "" {
		*title = chooseAndFormatTitle(*title, cmd)
		fanOut(*title)
	}
	var buf string
	for in.Scan() {
		buf = in.Text()
		nLines++
		if *echo {
			fanOut(buf + "\n")
		}
		if *quiet {
			continue
		}
		if (nLines-1)%*freq != 0 {
			continue
		}

		for i := 0; i < lastLen; i++ {
			fanOut("\b")
		}

		str := getFanText(*duration, nLines, *tLines)
		fanOut(str)
		newLen := len(str)
		if newLen < lastLen {
			// Clear trailing garbage from previous output
			for i := newLen; i < lastLen; i++ {
				fanOut(" ")
			}
			for i := newLen; i < lastLen; i++ {
				fanOut("\b")
			}
		}

		lastLen = newLen
		if *echo {
			fanOut("\n")
		}
	}
	timeTaken := time.Now().Unix() - startTime

	if !*quiet {
		if *clean {
			cleanFan(lastLen + len(*title))
		} else {
			fanOut("\n")
		}
	}

	if *record != "" {
		_, err := os.Stat(*record)
		if *saveNewRecord || os.IsNotExist(err) {
			createFanRecord(*record, timeTaken, nLines)
		}
	}

	ret := 0
	if proc != nil {
		if proc.Wait() != nil {
			ret = 1
		}
	}
	os.Exit(ret)
}