├── LICENSE ├── readme.md ├── sll.go └── sll_test.go /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Kevin Burke 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # sll 2 | 3 | Tired of grepping for files and getting really long lines and/or minified junk 4 | in the output? 5 | 6 | Yeah, the second result is not what you are looking for. 7 | 8 | `sll` makes it easy to strip those lines; just pipe the result of `grep` or 9 | `ag` through `sll`. 10 | 11 | ```bash 12 | ag check | sll 13 | ``` 14 | 15 | By default, any line longer than 1024 lines is not shown. You can change this 16 | by passing an integer to `sll`: 17 | 18 | ```bash 19 | ag check | sll 80 20 | ``` 21 | 22 | will filter any lines longer than 80 characters from the output. 23 | 24 | ## Installation 25 | 26 | Run `go install github.com/kevinburke/sll` and add `"$GOPATH/bin"` to your 27 | `$PATH` variable. 28 | 29 | ## Other Ways to Implement this 30 | 31 | There are many other ways to write this and some of them are listed here. Feel 32 | free to add your own with a pull request. Be sure to mention how this project 33 | is "pointless" and a "waste of time". Before merging, I will berate myself for 34 | having foisted this waste on the unsuspecting Internet population, retreat to 35 | my parents basement, and promise to never open source a thing I write ever 36 | again. 37 | 38 | - `egrep -v .{1024}` 39 | - `cut -c 1-1024` 40 | - `less -S` 41 | - `sed -E 's/^(.{0,80}).*/\1/'` 42 | 43 | ## Errata 44 | 45 | Currently `ag` has a `--print-long-lines` flag, which [has not been 46 | implemented][ag]. If it does, you should probably use that instead. 47 | 48 | [ag]: https://github.com/ggreer/the_silver_searcher/issues/189 49 | -------------------------------------------------------------------------------- /sll.go: -------------------------------------------------------------------------------- 1 | // sll ("strip long line") will remove long lines from a grep output. 2 | package main 3 | 4 | import ( 5 | "bufio" 6 | "errors" 7 | "flag" 8 | "fmt" 9 | "io" 10 | "log" 11 | "os" 12 | "strconv" 13 | ) 14 | 15 | const DEFAULT_STRIP_SIZE = 1024 16 | 17 | var help = `Usage: sll [size] 18 | 19 | size: Strip lines longer than this from the command output. Defaults to 1024 20 | ` 21 | 22 | func getLength(args []string) (int, error) { 23 | argLen := len(args) 24 | if argLen > 0 { 25 | if argLen >= 2 { 26 | return 0, errors.New("too many arguments provided") 27 | } 28 | length, err := strconv.ParseUint(args[0], 10, 16) 29 | if err != nil { 30 | return 0, err 31 | } 32 | if length <= 0 { 33 | return 0, fmt.Errorf("invalid line length: %d", length) 34 | } 35 | return int(length), nil 36 | } else { 37 | return 1024, nil 38 | } 39 | return 0, nil 40 | } 41 | 42 | func init() { 43 | flag.Usage = func() { 44 | os.Stderr.Write([]byte(help)) 45 | } 46 | } 47 | 48 | func stripLongLines(length int, in io.Reader, out io.Writer) error { 49 | buf := bufio.NewReaderSize(in, length) 50 | for { 51 | line, isPrefix, err := buf.ReadLine() 52 | if err != nil { 53 | if err == io.EOF { 54 | break 55 | } 56 | log.Fatal(err) 57 | } 58 | if isPrefix { 59 | // line was too long. ignore it 60 | for isPrefix { 61 | _, isPrefix, err = buf.ReadLine() 62 | if err != nil { 63 | if err == io.EOF { 64 | break 65 | } 66 | log.Fatal(err) 67 | } 68 | } 69 | } else { 70 | out.Write(line) 71 | out.Write([]byte("\n")) 72 | } 73 | } 74 | return nil 75 | } 76 | 77 | func main() { 78 | flag.Parse() 79 | args := flag.Args() 80 | length, err := getLength(args) 81 | if err != nil { 82 | log.Fatal(err) 83 | } 84 | err = stripLongLines(length, os.Stdin, os.Stdout) 85 | if err != nil { 86 | log.Fatal(err) 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /sll_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | ) 7 | 8 | func TestLinesStripped(t *testing.T) { 9 | b := new(bytes.Buffer) 10 | // Need to have at least 16 in the buffer to trigger NewBufferSize. 11 | input := `foo 12 | 012345678901234567890123 13 | barbazbang 14 | 012345678901234567890123 15 | 012345678901234567890123 16 | 012345678901234567890123 17 | blah` 18 | expected := `foo 19 | barbazbang 20 | blah 21 | ` 22 | r := bytes.NewReader([]byte(input)) 23 | err := stripLongLines(16, r, b) 24 | if err != nil { 25 | t.Fatal(err) 26 | } 27 | s := b.String() 28 | if s != expected { 29 | t.Errorf("expected strip(input) to be %s, was %s", expected, s) 30 | } 31 | } 32 | --------------------------------------------------------------------------------