Documentation
¶
Overview ¶
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 ( "github.com/noborus/ov/oviewer" ) func main() { ov, err := oviewer.Open("main.go") if err != nil { panic(err) } if err := ov.Run(); err != nil { panic(err) } }
Index ¶
- Constants
- Variables
- func ContentsToStr(lc contents) (string, map[int]int)
- func GetKeyBinds(bind map[string][]string) map[string][]string
- func KeyBindString(k KeyBind) string
- func RangeStyle(lc contents, start int, end int, s OVStyle)
- func StrToContents(str string, tabWidth int) contents
- func ToTcellStyle(s OVStyle) tcell.Style
- type Compressed
- type Config
- type Document
- func (m *Document) BackSearchLine(ctx context.Context, searcher Searcher, num int) (int, error)
- func (m *Document) BufEOF() bool
- func (m *Document) BufEndNum() int
- func (m *Document) ClearCache()
- func (m *Document) ContinueReadAll(ctx context.Context, r io.Reader) error
- func (m *Document) CurrentLN() int
- func (m *Document) Export(w io.Writer, start int, end int)
- func (m *Document) GetLine(n int) string
- func (m *Document) NewCache() error
- func (m *Document) ReadAll(r io.Reader) error
- func (m *Document) ReadFile(fileName string) error
- func (m *Document) ReadReader(r io.Reader) error
- func (m *Document) SearchLine(ctx context.Context, searcher Searcher, num int) (int, error)
- func (m *Document) Write(p []byte) (int, error)
- type EventInput
- type Input
- type InputMode
- type KeyBind
- type OVStyle
- type Root
- func (root *Root) AddDocument(m *Document)
- func (root *Root) BackSearch(str string)
- func (root *Root) Cancel()
- func (root *Root) Close()
- func (root *Root) CloseDocument(m *Document)
- func (root *Root) CopySelect()
- func (root *Root) DocumentLen() int
- func (root *Root) MoveBottom()
- func (root *Root) MoveLine(num int)
- func (root *Root) MoveTop()
- func (root *Root) Paste()
- func (root *Root) Quit()
- func (root *Root) Reload()
- func (root *Root) Run() error
- func (root *Root) Search(str string)
- func (root *Root) SetConfig(config Config)
- func (root *Root) SetDocument(docNum int)
- func (root *Root) SetKeyHandler(name string, keys []string, handler func()) error
- func (root *Root) SetWatcher(watcher *fsnotify.Watcher)
- func (root *Root) Suspend()
- func (root *Root) TailSync()
- func (root *Root) ViewSync()
- func (root *Root) WriteLog()
- func (root *Root) WriteOriginal()
- func (root *Root) WriteQuit()
- type ScreenMode
- type Searcher
Examples ¶
Constants ¶
const FormFeed = "\f"
const MaxWriteLog int = 10
Variables ¶
var ( // OverStrikeStyle represents the overstrike style. OverStrikeStyle tcell.Style // OverLineStyle represents the overline underline style. OverLineStyle tcell.Style )
var ( // Redirect to standard output. // echo "t" | ov> out STDOUTPIPE *os.File // Redirects the error output of ov --exec. // ov --exec -- command 2> out STDERRPIPE *os.File )
ov output destination.
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") // ErrAlreadyClose indicates that it is already closed. ErrAlreadyClose = errors.New("already closed") )
var DefaultContent = content{ // contains filtered or unexported fields }
DefaultContent is a blank Content.
var EOFContent = content{ // contains filtered or unexported fields }
EOFContent is "~" only.
var UpdateInterval = 50 * time.Millisecond
UpdateInterval is the update interval that calls eventUpdate().
Functions ¶
func ContentsToStr ¶ added in v0.9.5
ContentsToStr returns a converted string and byte position, as well as the content position conversion table.
func GetKeyBinds ¶
GetKeyBinds returns the current key mapping.
func KeyBindString ¶
KeyBindString returns keybind as a string for help.
func RangeStyle ¶ added in v0.9.0
RangeStyle applies the style to the specified range. Apply style to contents.
func StrToContents ¶ added in v0.9.5
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.
func ToTcellStyle ¶ added in v0.10.0
func ToTcellStyle(s OVStyle) tcell.Style
Types ¶
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. GZIP // BZIP2 is bzip2 compressed format. BZIP2 // ZSTD is zstd compressed format. ZSTD // LZ4 is lz4 compressed format. LZ4 // XZ is xz compressed format. XZ )
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 // StyleSectionLine is a style that section delimiter line. StyleSectionLine OVStyle // General represents the general behavior. General general // Mode represents the operation of the customized mode. Mode map[string]general // Mouse support disable. DisableMouse bool // IsWriteOriginal is true, write the current screen on quit. IsWriteOriginal bool // BeforeWriteOriginal specifies the number of lines before the current position. // 0 is the top of the current screen BeforeWriteOriginal int // AfterWriteOriginal specifies the number of lines after the current position. // 0 specifies the bottom of the screen. AfterWriteOriginal int // 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 // Deprecated: Alternating background color. ColorAlternate string // Deprecated: Header color. ColorHeader string // Deprecated: OverStrike color. ColorOverStrike string // Deprecated: OverLine color. ColorOverLine string }
Config represents the settings of ov.
type Document ¶
type Document struct { // fileName is the file name to display. FileName string // Caption is an additional caption to display after the file name. Caption string // CFormat is a compressed format. CFormat Compressed // WatchMode is watch mode. WatchMode bool // contains filtered or unexported fields }
The Document structure contains the values for the logical screen.
func NewLogDoc ¶ added in v0.5.0
NewLogDoc generates a document for log. NewLogDoc makes LogDoc the output destination of go's standard logger.
func OpenDocument ¶ added in v0.10.1
OpenDocument opens a file and returns a Document.
func STDINDocument ¶ added in v0.10.1
STDINDocument returns a Document that reads stdin.
func (*Document) BackSearchLine ¶ added in v0.10.1
BackSearchLine does a backward search on the document and returns a matching line.
func (*Document) ContinueReadAll ¶ added in v0.9.0
ContinueReadAll continues to read even if it reaches EOF.
func (*Document) CurrentLN ¶ added in v0.10.0
CurrentLN returns the currently displayed line number.
func (*Document) ReadAll ¶
ReadAll reads all from the reader. And store it in the lines of the Document. ReadAll needs to be notified on eofCh.
func (*Document) ReadReader ¶ added in v0.10.1
ReadReader reads reader. A wrapper for ReadAll, used when eofCh notifications are not needed.
func (*Document) SearchLine ¶ added in v0.10.1
SearchLine searches the document and returns the matching line.
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 WatchCandidate *candidate WriteBACandidate *candidate SectionDelmCandidate *candidate SectionStartCandidate *candidate // contains filtered or unexported fields }
Input represents the status of various inputs. Retain each input list to save the input history.
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. ViewMode // Search is a search input mode. Search // Backsearch is a backward search input mode. Backsearch // Goline is a move input mode. Goline // Header is the number of headers input mode. Header // Delimiter is a delimiter input mode. Delimiter // TabWidth is the tab number input mode. TabWidth // WatchInterval is the watch interval input mode. Watch // SkipLines is the number of lines to skip. SkipLines // WriteBA is the number of ranges to write at quit. WriteBA // SectionDelimiter is a section delimiter input mode. SectionDelimiter // SectionStart is a section start position input mode. SectionStart )
type OVStyle ¶ added in v0.11.0
type OVStyle struct { // Background is a color name string. Background string // Foreground is a color name string. Foreground string // If true, add blink. Blink bool // If true, add bold. Bold bool // If true, add dim. Dim bool // If true, add italic. Italic bool // If true, add reverse. Reverse bool // If true, add underline. Underline bool // If true, add strikethrough. StrikeThrough bool }
OVStyle represents a style in addition to the original style.
type Root ¶
type Root struct { // tcell.Screen is the root screen. tcell.Screen // Config contains settings that determine the behavior of ov. Config // 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
ExecCommand return the structure of oviewer. ExecCommand executes the command and opens stdout/stderr as document.
Example ¶
package main import ( "os/exec" "github.com/noborus/ov/oviewer" ) func main() { command := exec.Command("ls", "-alF") ov, err := oviewer.ExecCommand(command) if err != nil { panic(err) } if err := ov.Run(); err != nil { panic(err) } }
Output:
func NewOviewer ¶
NewOviewer return the structure of oviewer. NewOviewer requires one or more documents.
Example ¶
package main import ( "bytes" "github.com/noborus/ov/oviewer" ) func main() { doc, err := oviewer.NewDocument() if err != nil { panic(err) } s := "Hello, World!" if err := doc.ReadAll(bytes.NewBufferString(s)); err != nil { panic(err) } ov, err := oviewer.NewOviewer(doc) if err != nil { panic(err) } if err := ov.Run(); err != nil { panic(err) } }
Output:
func NewRoot ¶ added in v0.9.0
NewRoot returns the structure of the oviewer. NewRoot is a simplified version that can be used externally.
Example ¶
package main import ( "strings" "github.com/noborus/ov/oviewer" ) func main() { r := strings.NewReader(strings.Repeat("north\n", 99)) ov, err := oviewer.NewRoot(r) if err != nil { panic(err) } if err := ov.Run(); err != nil { panic(err) } }
Output:
func Open ¶
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.
Example ¶
package main import ( "github.com/noborus/ov/oviewer" ) func main() { ov, err := oviewer.Open("example_test.go") if err != nil { panic(err) } if err := ov.Run(); err != nil { panic(err) } }
Output:
func (*Root) AddDocument ¶ added in v0.8.0
AddDocument fires a add document event.
func (*Root) BackSearch ¶
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) CloseDocument ¶ added in v0.8.0
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
DocumentLen returns the number of Docs.
func (*Root) MoveBottom ¶
func (root *Root) MoveBottom()
MoveBottom fires the event of moving to bottom.
func (*Root) Paste ¶ added in v0.6.0
func (root *Root) Paste()
Paste executes the mouse paste event.
func (*Root) Search ¶
Search fires a forward search event. This is for calling Search from the outside. Normally, the event is executed from Confirm.
func (*Root) SetDocument ¶
SetDocument fires a set document event.
func (*Root) SetKeyHandler ¶ added in v0.9.6
SetKeyHandler assigns a new key handler.
func (*Root) SetWatcher ¶ added in v0.8.9
SetWatcher sets file monitoring.
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.
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. Help // LogDoc is Error screen mode. LogDoc )