コード例 #1
0
ファイル: play.go プロジェクト: charpty/ttygif
// Play reads ttyrecord file and play (write to STDOUT)
func Play(filename string, capture func(ttyread.TimeVal) error) (err error) {
	file, err := os.Open(filename)
	if err != nil {
		return
	}
	defer file.Close()
	defer clearScreen()

	var prevTv *ttyread.TimeVal
	reader := ttyread.NewTtyReader(file)
	for {
		// read
		var data *ttyread.TtyData
		data, err = reader.ReadData()
		if err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		}
		// calc delay
		var diff ttyread.TimeVal
		if prevTv == nil {
			err = clearScreen()
			if err != nil {
				return
			}
		} else {
			diff = data.TimeVal.Subtract(*prevTv)
		}
		prevTv = &data.TimeVal
		// capture
		err = capture(diff)
		if err != nil {
			return
		}
		// play
		_, err = os.Stdout.Write(*data.Buffer)
		if err != nil {
			return
		}
	}
	return nil
}
コード例 #2
0
ファイル: player.go プロジェクト: lengshuiyulangcn/ttyrec2gif
// TtyPlay reads ttyrecord file and play
func (g *GifGenerator) TtyPlay(input string, term Terminal, capture func(int32) error) (err error) {
	inFile, err := os.Open(input)
	if err != nil {
		return
	}
	defer inFile.Close()

	reader := ttyread.NewTtyReader(inFile)
	var prevTv *ttyread.TimeVal
	for {
		var data *ttyread.TtyData
		data, err = reader.ReadData()
		if err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		}
		var diff ttyread.TimeVal
		if prevTv != nil {
			diff = data.TimeVal.Subtract(*prevTv)
		}
		prevTv = &data.TimeVal

		err = capture(diff.Sec*1000000 + diff.Usec)
		if err != nil {
			return
		}

		_, err = term.Write(*data.Buffer)
		if err != nil {
			return
		}
	}
	return nil
}