Documentation
¶
Overview ¶
Package termcast streams the screen of a chromedp page to a terminal that has graphics.
It uses the screencast of the Chrome DevTools Protocol to get the frames of the page, and the package rasterm to draw them. Start asks the terminal which graphics it has, with rasterm.Detect. The terminal must support Kitty, iTerm2 or Sixel graphics. Start returns ErrNoGraphics when it does not.
The browser sends a frame only when the page changes. The stream keeps the latest frame and draws it again only when it is new, so a page that stops changing stays on the screen. On a Kitty terminal that plays animations, the stream draws each frame into one Kitty image, so the picture changes with no flicker. See WithAnimation. On other terminals, the stream clears the screen at each redraw and draws the latest frame. It uses no alternate screen and does not hide the cursor. A program that ends with log.Fatal skips its deferred calls, so the stream must never leave the terminal in a changed state.
The log lines of the program draw over the frames. Stream.LogWriter holds the lines while the stream runs and prints them when the stream stops, after a final frame. Other output that draws over the frames, such as the protocol messages of chromedp.WithDebugf, does not work together with the stream. The program decides which one it shows.
A nil *Stream is a stream that is off, and its methods are safe. So a program starts the stream only when its own flag asks for it, and writes defer s.Stop() and log.SetOutput(s.LogWriter()) in either case. See the example.
Example ¶
A program has its own flag for the stream, and starts the stream only when the flag is on. A nil stream is a stream that is off, so the program calls Stop and LogWriter in either case.
package main
import (
"context"
"flag"
"log"
"time"
"github.com/chromedp/chromedp"
"github.com/chromedp/termcast"
)
func main() {
onTerminal := flag.Bool("visible-on-terminal", false, "draw the page in the terminal")
fps := flag.Float64("terminal-fps", termcast.DefaultFPS, "frames per second of the stream")
flag.Parse()
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
// Load the page. This call starts the browser.
if err := chromedp.Do(ctx, chromedp.Navigate("https://example.com")); err != nil {
log.Fatal(err)
}
// Start the stream. Start returns termcast.ErrNoGraphics for a terminal
// that has no graphics.
var s *termcast.Stream
if *onTerminal {
var err error
if s, err = termcast.Start(ctx, termcast.WithFPS(*fps)); err != nil {
log.Fatal(err)
}
}
defer s.Stop()
// The log lines wait until the stream stops, so that they do not draw over
// the frames.
log.SetOutput(s.LogWriter())
log.Println("the page is on the screen")
if err := chromedp.Do(ctx, chromedp.Sleep(5*time.Second)); err != nil {
// log.Fatal skips the deferred calls. Stop first, so that the final
// frame and the held log lines reach the terminal.
s.Stop()
log.Fatal(err)
}
}
Output:
Index ¶
- Constants
- type Error
- type Option
- func WithAnimation(on bool) Option
- func WithDetectOptions(opts ...rasterm.DetectOption) Option
- func WithElement[S chromedp.Selectable](sel S) Option
- func WithFPS(fps float64) Option
- func WithLogOutput(w io.Writer) Option
- func WithMaxSize(width, height int) Option
- func WithOutput(w io.Writer) Option
- func WithQuality(quality int) Option
- func WithTerminal(t rasterm.Terminal) Option
- type Stream
Examples ¶
Constants ¶
const ( // DefaultFPS is the number of frames per second that the stream draws. DefaultFPS = 4 // DefaultQuality is the JPEG quality that the screencast asks for. DefaultQuality = 80 // DefaultWidth is the largest width of a frame in pixels. DefaultWidth = 1024 // DefaultHeight is the largest height of a frame in pixels. DefaultHeight = 768 )
Default values of the options.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Error ¶
type Error string
Error is the type of the constant errors of this package.
const ( // ErrNoGraphics is the error that [Start] returns when the terminal has no // graphics. [rasterm.Detect] finds Kitty, iTerm2 and Sixel from a query to // the terminal and from the environment. The variable TERM_GRAPHICS can force // the type to kitty, iterm or sixel, and the value none turns graphics // off. ErrNoGraphics Error = "" /* 131-byte string literal not displayed */ )
type Option ¶
type Option func(*config)
Option changes how Start makes a stream.
func WithAnimation ¶ added in v0.3.0
WithAnimation turns the Kitty stream on or off. The Kitty stream draws each frame into one Kitty image with rasterm.KittyStream, so it does not clear the screen and does not flicker. It works only with the Kitty protocol.
By default, the stream uses it when the terminal reports that it plays a Kitty animation, or when it names itself kitty or Ghostty. A terminal that draws Kitty images but has no animation shows only the first frame of a Kitty stream, so the default does not guess.
func WithDetectOptions ¶ added in v0.3.0
func WithDetectOptions(opts ...rasterm.DetectOption) Option
WithDetectOptions gives options to rasterm.Detect, such as rasterm.WithTimeout. WithTerminal turns detection off, and then the options do nothing.
func WithElement ¶ added in v0.2.0
func WithElement[S chromedp.Selectable](sel S) Option
WithElement makes the stream draw only the element that sel selects, and not the whole page. The element is the first one that the selector matches, for example chromedp.CSS("#chart"). The stream looks the element up again at each frame, so an element that moves or changes its size stays in view.
The browser sends the viewport, so the stream shows the part of the element that is in the viewport. Scroll the element into view first, for example with chromedp.ScrollIntoView. While the page has no such element, the stream keeps what is on the screen.
func WithFPS ¶
WithFPS sets the number of frames per second that the stream draws at most. The default is DefaultFPS.
func WithLogOutput ¶
WithLogOutput sets the writer that receives the held log lines when the stream stops. The default is os.Stderr.
func WithMaxSize ¶
WithMaxSize sets the largest width and height of a frame in pixels. The browser scales a larger page down. The default is DefaultWidth by DefaultHeight.
func WithOutput ¶
WithOutput sets the writer that receives the frames. The default is os.Stdout. Detection asks the terminal on os.Stdout and os.Stdin, so give WithTerminal too when w is not that terminal.
func WithQuality ¶
WithQuality sets the JPEG quality of the frames that the browser sends, from 0 to 100. The default is DefaultQuality.
func WithTerminal ¶ added in v0.3.0
WithTerminal makes Start use t, and not detect the terminal. Use it when the program detected the terminal itself, or when the output is not the terminal, for example rasterm.Terminal{Protocol: rasterm.Kitty} for a file. Start returns ErrNoGraphics when t.Protocol is rasterm.None.
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream draws the screen of a page in the terminal. Start makes it. A nil stream is a stream that is off. Every method is safe on a nil pointer, and safe to call from several goroutines.
func Start ¶
Start starts the screencast of the page of ctx and draws it in the terminal. The context is a chromedp context. Start starts the browser and opens the target when the context has none yet, as chromedp.Run does.
Start first detects the terminal with rasterm.Detect, unless WithTerminal gives it. It returns ErrNoGraphics when the terminal has no graphics. The stream ends when the context ends, and then it acts as Stream.Stop.
func (*Stream) Keys ¶ added in v0.3.0
Keys returns the keys that the user typed while Start detected the terminal, in the order of the typing. Detection reads the input to get the answers of the terminal, so a program that reads its input must use these keys as its first input. It returns nil on a nil stream, and when WithTerminal turned detection off.
func (*Stream) LogWriter ¶
LogWriter returns a writer for the log lines of the program, for example for log.SetOutput. While the stream runs, the writer holds the lines in memory. Stream.Stop prints them to the log output after the final frame. After Stream.Stop, the writer passes each line on at once. On a nil stream, it returns os.Stderr.
func (*Stream) Stop ¶
Stop stops the screencast and the goroutines of the stream, draws the final frame once, and then prints the held log lines. When no frame arrived, it takes one screenshot as the final frame. It returns the first error that the stream met, such as an error of the encoder or of the output.
Stop is safe to call more than once and from several goroutines. Each call returns the same error. A call that arrives while another one runs waits until that one is done. The stream also stops by itself when the context of Start ends. On a nil stream, Stop does nothing and returns nil.
func (*Stream) Terminal ¶ added in v0.3.0
Terminal returns the terminal that the stream draws on, as rasterm.Detect found it or as WithTerminal gave it. On a nil stream, it returns the zero rasterm.Terminal, which has no graphics.