Example #1
0
func (c *Commands) executeFile(t *Term, name string) error {
	fh, err := os.Open(name)
	if err != nil {
		return err
	}
	defer fh.Close()

	scanner := bufio.NewScanner(fh)
	lineno := 0
	for scanner.Scan() {
		line := strings.TrimSpace(scanner.Text())
		lineno++

		if line == "" || line[0] == '#' {
			continue
		}

		cmdstr, args := parseCommand(line)

		if err := c.Call(cmdstr, args, t); err != nil {
			fmt.Printf("%s:%d: %v\n", name, lineno, err)
		}
	}

	return scanner.Err()
}
Example #2
0
func formatFile(src *os.File) ([]byte, error) {
	scanner := bufio.NewScanner(src)
	var b bytes.Buffer

	lineNum := 0

	for scanner.Scan() {
		line := []byte(scanner.Text())

		if *nonPrintEnd {
			line = appendEnd(line)
		}

		if *nonPrintTab {
			line = appendTab(line)
		}

		if *numLines || *nonBlank {
			lineNum++
			line = numberLine(line, lineNum)
		}

		line = append(line, '\n')
		b.Write(line)
	}
	return b.Bytes(), nil
}