Version: v0.9.6 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2022 License: MIT Imports: 33 Imported by: 1



Package oviewer provides a pager for terminals.

oviewer displays the contents of the file or reader etc... on the current terminal screen. After running a Run, oveiwer does not return until the user finishes running. So if you want to do something in concurrent, you need to use goroutine.

There is also a simple usage example below: https://github.com/noborus/mdviewer/

package main

import (

func main() {
    ov, err := oviewer.Open("main.go")
    if err != nil {
    if err := ov.Run(); err != nil {




This section is empty.


View Source
var (
	// OverStrikeStyle represents the overstrike style.
	OverStrikeStyle tcell.Style
	// OverLineStyle represents the overline underline style.
	OverLineStyle tcell.Style
View Source
var (
	// Redirect to standard output.
	//     echo "t" | ov> out
	// Redirects the error output of ov --exec.
	//     ov --exec -- command 2> out

ov output destination.

View Source
var (
	// ErrOutOfRange indicates that value is out of range.
	ErrOutOfRange = errors.New("out of range")
	// ErrFatalCache indicates that the cache value had a fatal error.
	ErrFatalCache = errors.New("fatal error in cache value")
	// ErrMissingFile indicates that the file does not exist.
	ErrMissingFile = errors.New("missing filename")
	// ErrIsDirectory indicates that specify a directory instead of a file.
	ErrIsDirectory = errors.New("is a directory")
	// ErrNotFound indicates not found.
	ErrNotFound = errors.New("not found")
	// ErrCancel indicates cancel.
	ErrCancel = errors.New("cancel")
	// ErrInvalidNumber indicates an invalid number.
	ErrInvalidNumber = errors.New("invalid number")
	// ErrFailedKeyBind indicates keybinding failed.
	ErrFailedKeyBind = errors.New("failed to set keybind")
	// ErrSignalCatch indicates that the signal has been caught.
	ErrSignalCatch = errors.New("signal catch")
View Source
var DefaultContent = content{
	// contains filtered or unexported fields

DefaultContent is a blank Content.

View Source
var EOFContent = content{
	// contains filtered or unexported fields

EOFContent is "~" only.


func ContentsToStr added in v0.9.5

func ContentsToStr(lc lineContents) (string, map[int]int)

ContentsToStr returns a converted string and byte position, as well as the content position conversion table.

func GetKeyBinds

func GetKeyBinds(bind map[string][]string) map[string][]string

GetKeyBinds returns the current key mapping.

func KeyBindString

func KeyBindString(k KeyBind) string

KeyBindString returns keybind as a string for help.

func RangeStyle added in v0.9.0

func RangeStyle(lc lineContents, start int, end int, style ovStyle)

RangeStyle applies the style to the specified range.

func StrToContents added in v0.9.5

func StrToContents(str string, tabWidth int) lineContents

StrToContents converts a single-line string into a one line of contents. Parse escape sequences, etc. 1 Content matches the characters displayed on the screen.


type Compressed added in v0.8.9

type Compressed int

Compressed represents the type of compression.

const (
	// UNCOMPRESSED is an uncompressed format.
	UNCOMPRESSED Compressed = iota
	// GZIP is gzip compressed format.
	// BZIP2 is bzip2 compressed format.
	// ZSTD is zstd compressed format.
	// LZ4 is lz4 compressed format.
	// XZ is xz compressed format.

func (Compressed) String added in v0.8.9

func (c Compressed) String() string

type Config

type Config struct {
	// StyleAlternate is a style that applies line by line.
	StyleAlternate ovStyle
	// StyleHeader is the style that applies to the header.
	StyleHeader ovStyle
	// StyleHeader is the style that applies to the header.
	StyleBody ovStyle
	// StyleOverStrike is a style that applies to overstrikes.
	StyleOverStrike ovStyle
	// OverLineS is a style that applies to overstrike underlines.
	StyleOverLine ovStyle
	// StyleLineNumber is a style that applies line number.
	StyleLineNumber ovStyle
	// StyleSearchHighlight is the style that applies to the search highlight.
	StyleSearchHighlight ovStyle
	// StyleColumnHighlight is the style that applies to the column highlight.
	StyleColumnHighlight ovStyle
	// StyleMarkLine is a style that marked line.
	StyleMarkLine ovStyle

	// Old setting method.
	// Alternating background color.
	ColorAlternate string
	// Header color.
	ColorHeader string
	// OverStrike color.
	ColorOverStrike string
	// OverLine color.
	ColorOverLine string

	// General represents the general behavior.
	General general
	// Mode represents the operation of the customized mode.
	Mode map[string]general

	// Mouse support disable.
	DisableMouse bool
	// AfterWrite writes the current screen on exit.
	AfterWrite bool
	// QuiteSmall Quit if the output fits on one screen.
	QuitSmall bool
	// CaseSensitive is case-sensitive if true.
	CaseSensitive bool
	// RegexpSearch is Regular expression search if true.
	RegexpSearch bool
	// Incsearch is incremental server if true.
	Incsearch bool
	// Debug represents whether to enable the debug output.
	Debug bool

	// KeyBinding
	Keybind map[string][]string

Config represents the settings of ov.

func NewConfig added in v0.5.0

func NewConfig() Config

NewConfig return the structure of Config with default values.

type Document

type Document struct {
	// fileName is the file name to display.
	FileName string

	// CFormat is a compressed format.
	CFormat Compressed
	// contains filtered or unexported fields

The Document structure contains the values for the logical screen.

func NewDocument

func NewDocument() (*Document, error)

NewDocument returns Document.

func NewHelp

func NewHelp(k KeyBind) (*Document, error)

NewHelp generates a document for help.

func NewLogDoc added in v0.5.0

func NewLogDoc() (*Document, error)

NewLogDoc generates a document for log. NewLogDoc makes LogDoc the output destination of go's standard logger.

func (*Document) BufEOF

func (m *Document) BufEOF() bool

BufEOF return true if EOF is reached.

func (*Document) BufEndNum

func (m *Document) BufEndNum() int

BufEndNum return last line number.

func (*Document) ClearCache

func (m *Document) ClearCache()

ClearCache clears the cache.

func (*Document) ContinueReadAll added in v0.9.0

func (m *Document) ContinueReadAll(r io.Reader) error

ContinueReadAll continues to read even if it reaches EOF.

func (*Document) GetLine

func (m *Document) GetLine(n int) string

GetLine returns one line from buffer.

func (*Document) NewCache

func (m *Document) NewCache() error

NewCache creates a new cache.

func (*Document) ReadAll

func (m *Document) ReadAll(r io.Reader) error

ReadAll reads all from the reader to the buffer. It returns if beforeSize is accumulated in buffer before the end of read.

func (*Document) ReadFile

func (m *Document) ReadFile(fileName string) error

ReadFile reads file.

func (*Document) Write added in v0.5.0

func (m *Document) Write(p []byte) (int, error)

Write matches the interface of io.Writer(so package log is possible). Therefore, the log.Print output is displayed by logDoc.

type EventInput

type EventInput interface {
	// Prompt returns the prompt string in the input field.
	Prompt() string
	// Confirm returns the event when the input is confirmed.
	Confirm(i string) tcell.Event
	// Up returns strings when the up key is pressed during input.
	Up(i string) string
	// Down returns strings when the down key is pressed during input.
	Down(i string) string

EventInput is a generic interface for inputs.

type Input

type Input struct {
	EventInput EventInput

	ModeCandidate      *candidate
	SearchCandidate    *candidate
	GoCandidate        *candidate
	DelimiterCandidate *candidate
	TabWidthCandidate  *candidate
	// contains filtered or unexported fields

Input represents the status of various inputs. Retain each input list to save the input history.

func NewInput

func NewInput() *Input

NewInput returns all the various inputs.

type InputMode

type InputMode int

InputMode represents the state of the input.

const (
	// Normal is normal mode.
	Normal InputMode = iota
	// ViewMode is a view selection input mode.
	// Search is a search input mode.
	// Backsearch is a backward search input mode.
	// Goline is a move input mode.
	// Header is the number of headers input mode.
	// Delimiter is a delimiter input mode.
	// TabWidth is the tab number input mode.
	// SkipLines is the number of lines to skip.

type KeyBind

type KeyBind map[string][]string

KeyBind is the mapping of action and key.

type Root

type Root struct {
	// tcell.Screen is the root screen.
	// Config contains settings that determine the behavior of ov.

	// Doc contains the model of ov
	Doc *Document

	// DocList
	DocList    []*Document
	CurrentDoc int

	// Original position at the start of search.
	OriginPos int
	// Original string.
	OriginStr string
	// contains filtered or unexported fields

Root structure contains information about the drawing.

func ExecCommand added in v0.8.9

func ExecCommand(command *exec.Cmd) (*Root, error)

ExecCommand return the structure of oviewer. ExecCommand executes the command and opens stdout/stderr as document.

package main

import (


func main() {
	command := exec.Command("ls", "-alF")
	ov, err := oviewer.ExecCommand(command)
	if err != nil {
	if err := ov.Run(); err != nil {

func NewOviewer

func NewOviewer(docs ...*Document) (*Root, error)

NewOviewer return the structure of oviewer. NewOviewer requires one or more documents.

package main

import (


func main() {
	doc, err := oviewer.NewDocument()
	if err != nil {
	s := "Hello, World!"
	if err := doc.ReadAll(bytes.NewBufferString(s)); err != nil {

	ov, err := oviewer.NewOviewer(doc)
	if err != nil {
	if err := ov.Run(); err != nil {

func NewRoot added in v0.9.0

func NewRoot(read io.Reader) (*Root, error)

NewRoot returns the structure of the oviewer. NewRoot is a simplified version that can be used externally.

package main

import (


func main() {
	r := strings.NewReader(strings.Repeat("north\n", 99))
	ov, err := oviewer.NewRoot(r)
	if err != nil {
	if err := ov.Run(); err != nil {

func Open

func Open(fileNames ...string) (*Root, error)

Open reads the file named of the argument and return the structure of oviewer. If there is no file name, create Root from standard input. If there is only one file name, create Root from that file, but return an error if the open is an error. If there is more than one file name, create Root from multiple files.

package main

import (

func main() {
	ov, err := oviewer.Open("example_test.go")
	if err != nil {
	if err := ov.Run(); err != nil {

func (*Root) AddDocument added in v0.8.0

func (root *Root) AddDocument(m *Document)

AddDocument fires a add document event.

func (*Root) BackSearch

func (root *Root) BackSearch(str string)

BackSearch fires a backward search event. This is for calling Search from the outside. Normally, the event is executed from Confirm.

func (*Root) Cancel added in v0.6.2

func (root *Root) Cancel()

Cancel follow mode and follow all mode.

func (*Root) Close added in v0.9.0

func (root *Root) Close()

Close closes the oviewer.

func (*Root) CloseDocument added in v0.8.0

func (root *Root) CloseDocument(m *Document)

CloseDocument fires a del document event.

func (*Root) CopySelect added in v0.6.0

func (root *Root) CopySelect()

CopySelect executes a copy select event.

func (*Root) DocumentLen added in v0.9.0

func (root *Root) DocumentLen() int

DocumentLen returns the number of Docs.

func (*Root) MoveBottom

func (root *Root) MoveBottom()

MoveBottom fires the event of moving to bottom.

func (*Root) MoveLine

func (root *Root) MoveLine(num int)

MoveLine fires an event that moves to the specified line.

func (*Root) MoveTop

func (root *Root) MoveTop()

MoveTop fires the event of moving to top.

func (*Root) Paste added in v0.6.0

func (root *Root) Paste()

Paste executes the mouse paste event.

func (*Root) Quit

func (root *Root) Quit()

Quit executes a quit event.

func (*Root) Run

func (root *Root) Run() error

Run starts the terminal pager.

func (*Root) Search

func (root *Root) Search(str string)

Search fires a forward search event. This is for calling Search from the outside. Normally, the event is executed from Confirm.

func (*Root) SetConfig

func (root *Root) SetConfig(config Config)

SetConfig sets config.

func (*Root) SetDocument

func (root *Root) SetDocument(docNum int)

SetDocument fires a set document event.

func (*Root) SetKeyHandler added in v0.9.6

func (root *Root) SetKeyHandler(name string, keys []string, handler func()) error

func (*Root) SetWatcher added in v0.8.9

func (root *Root) SetWatcher(watcher *fsnotify.Watcher)

SetWatcher sets file monitoring.

func (*Root) Suspend added in v0.9.5

func (root *Root) Suspend()

Quit executes a quit event.

func (*Root) TailSync added in v0.8.0

func (root *Root) TailSync()

TailSync move to tail and sync.

func (*Root) ViewSync added in v0.8.0

func (root *Root) ViewSync()

ViewSync redraws the whole thing.

func (*Root) WriteLog added in v0.7.1

func (root *Root) WriteLog()

WriteLog write to the log terminal.

func (*Root) WriteOriginal

func (root *Root) WriteOriginal()

WriteOriginal writes to the original terminal.

func (*Root) WriteQuit

func (root *Root) WriteQuit()

WriteQuit sets the write flag and executes a quit event.

type ScreenMode added in v0.9.0

type ScreenMode int

ScreenMode represents the state of the screen.

const (
	// Docs is a normal document screen mode.
	Docs ScreenMode = iota
	// Help is Help screen mode.
	// LogDoc is Error screen mode.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL