tigrawap / slit

slit - a modern PAGER for viewing logs, get more than most in less time
Other
469 stars 18 forks source link

Refactor for library use #51

Closed bcicen closed 6 years ago

bcicen commented 6 years ago

This PR refactors main slit components into a core subpackage suitable for import and use by external programs. Also introduced is a new Slit struct and some "entrypoint" methods for instantiation; e.g:

opening a viewer for a given filepath:

package main

import (
    slit "github.com/tigrawap/slit/core"
)

func main() {
    s, err := slit.NewFromFilepath("/tmp/output.log")
    if err != nil {
        panic(err)
    }
    s.Display()
    s.Shutdown()
}

streaming from a channel to the viewer:

package main

import (
    "fmt"
    "time"
    slit "github.com/tigrawap/slit/core"
)

func main() {
    ch := make(chan string)

    s, err := slit.NewFromStream(ch)
    if err != nil {
        panic(err)
    }

    go func() {
        for i := 0; i < 100; i++ {
            ch <- fmt.Sprintf("line %d", i)
            time.Sleep(10 * time.Millisecond)
        }
        close(ch)
    }()

    s.Display()
    s.Shutdown()
}

mostly closes https://github.com/tigrawap/slit/issues/45

tigrawap commented 6 years ago

Thanks, I'm on vacation atm, will take a look in a few days

bcicen commented 6 years ago

@tigrawap great; I've submitted #52 as well, which fixes some blocking conditions and shutdown behavior that could cause bugs when used as a library.

tigrawap commented 6 years ago

(Merged manually)