Last produces only the last n lines of the pipe's contents, or all the lines if there are less than n. If n is zero or negative, there is no output at all.
(n int)
| 763 | // if there are less than n. If n is zero or negative, there is no output at |
| 764 | // all. |
| 765 | func (p *Pipe) Last(n int) *Pipe { |
| 766 | if p.Error() != nil { |
| 767 | return p |
| 768 | } |
| 769 | if n <= 0 { |
| 770 | return NewPipe() |
| 771 | } |
| 772 | return p.Filter(func(r io.Reader, w io.Writer) error { |
| 773 | scanner := newScanner(r) |
| 774 | input := ring.New(n) |
| 775 | for scanner.Scan() { |
| 776 | input.Value = scanner.Text() |
| 777 | input = input.Next() |
| 778 | } |
| 779 | input.Do(func(p interface{}) { |
| 780 | if p != nil { |
| 781 | fmt.Fprintln(w, p) |
| 782 | } |
| 783 | }) |
| 784 | return scanner.Err() |
| 785 | }) |
| 786 | } |
| 787 | |
| 788 | // Match produces only the input lines that contain the string s. |
| 789 | func (p *Pipe) Match(s string) *Pipe { |