chore: import official kitty sources from upstream
Bring the kovidgoyal/kitty codebase in as the base of FWS-Kitty, vendored at upstream commit 91fb38a without its git history.
This commit is contained in:
commit
7e7d249099
1134 files changed
+433629
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,130 @@
|
||||
// License: GPLv3 Copyright: 2024, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package pager
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"github.com/kovidgoyal/kitty/tools/simdstring"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
func wait_for_file_to_grow(file_name string, limit int64) (err error) {
|
||||
// TODO: Use the fsnotify package to avoid this poll
|
||||
for {
|
||||
time.Sleep(time.Second)
|
||||
s, err := os.Stat(file_name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if s.Size() > limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func read_input(input_file *os.File, input_file_name string, input_channel chan<- input_line_struct, follow_file bool, count_carriage_returns bool) {
|
||||
const buf_capacity = 8192
|
||||
buf := make([]byte, buf_capacity)
|
||||
output_buf := strings.Builder{}
|
||||
output_buf.Grow(buf_capacity)
|
||||
var err error
|
||||
var n int
|
||||
var total_read int64
|
||||
var num_carriage_returns int
|
||||
|
||||
defer func() {
|
||||
_ = input_file.Close()
|
||||
last := input_line_struct{line: output_buf.String(), err: err, num_carriage_returns: num_carriage_returns}
|
||||
if errors.Is(err, io.EOF) {
|
||||
last.err = nil
|
||||
}
|
||||
if len(last.line) > 0 || last.err != nil {
|
||||
input_channel <- last
|
||||
}
|
||||
close(input_channel)
|
||||
}()
|
||||
|
||||
var process_chunk func([]byte)
|
||||
|
||||
if count_carriage_returns {
|
||||
process_chunk = func(chunk []byte) {
|
||||
for len(chunk) > 0 {
|
||||
idx := simdstring.IndexByte2(chunk, '\n', '\r')
|
||||
if idx == -1 {
|
||||
_, _ = output_buf.Write(chunk)
|
||||
chunk = nil
|
||||
}
|
||||
switch chunk[idx] {
|
||||
case '\r':
|
||||
num_carriage_returns += 1
|
||||
default:
|
||||
input_channel <- input_line_struct{line: output_buf.String(), num_carriage_returns: num_carriage_returns, is_a_complete_line: true}
|
||||
num_carriage_returns = 0
|
||||
output_buf.Reset()
|
||||
output_buf.Grow(buf_capacity)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
process_chunk = func(chunk []byte) {
|
||||
for len(chunk) > 0 {
|
||||
idx := bytes.IndexByte(chunk, '\n')
|
||||
switch idx {
|
||||
case -1:
|
||||
_, _ = output_buf.Write(chunk)
|
||||
chunk = nil
|
||||
default:
|
||||
_, _ = output_buf.Write(chunk[idx:])
|
||||
chunk = chunk[idx+1:]
|
||||
input_channel <- input_line_struct{line: output_buf.String(), is_a_complete_line: true}
|
||||
output_buf.Reset()
|
||||
output_buf.Grow(buf_capacity)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for {
|
||||
for err != nil {
|
||||
n, err = input_file.Read(buf)
|
||||
if n > 0 {
|
||||
total_read += int64(n)
|
||||
process_chunk(buf)
|
||||
}
|
||||
if err == unix.EAGAIN || err == unix.EINTR {
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
if !follow_file {
|
||||
break
|
||||
}
|
||||
if errors.Is(err, io.EOF) {
|
||||
input_file.Close()
|
||||
if err = wait_for_file_to_grow(input_file_name, total_read); err != nil {
|
||||
break
|
||||
}
|
||||
if input_file, err = os.Open(input_file_name); err != nil {
|
||||
break
|
||||
}
|
||||
var off int64
|
||||
if off, err = input_file.Seek(total_read, io.SeekStart); err != nil {
|
||||
break
|
||||
}
|
||||
if off != total_read {
|
||||
err = fmt.Errorf("Failed to seek in %s to: %d", input_file_name, off)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// License: GPLv3 Copyright: 2024, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package pager
|
||||
|
||||
// TODO:
|
||||
// Scroll to line when starting
|
||||
// Visual mode elect with copy/paste and copy-on-select
|
||||
// Mouse based wheel scroll, drag to select, drag scroll, double click to select
|
||||
// Hyperlinks: Clicking should delegate to terminal and also allow user to specify action
|
||||
// Keyboard hints mode for clicking hyperlinks
|
||||
// Display images when used as scrollback pager
|
||||
// automatic follow when input is a pipe/tty and on last line like tail -f
|
||||
// syntax highlighting using chroma
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/kovidgoyal/kitty/tools/cli"
|
||||
"github.com/kovidgoyal/kitty/tools/tty"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
var debugprintln = tty.DebugPrintln
|
||||
var _ = debugprintln
|
||||
|
||||
type input_line_struct struct {
|
||||
line string
|
||||
num_carriage_returns int
|
||||
is_a_complete_line bool
|
||||
err error
|
||||
}
|
||||
|
||||
type global_state_struct struct {
|
||||
input_file_name string
|
||||
opts *Options
|
||||
}
|
||||
|
||||
var global_state global_state_struct
|
||||
|
||||
func main(_ *cli.Command, opts_ *Options, args []string) (rc int, err error) {
|
||||
global_state.opts = opts_
|
||||
input_channel := make(chan input_line_struct, 4096)
|
||||
var input_file *os.File
|
||||
if len(args) > 1 {
|
||||
return 1, fmt.Errorf("Only a single file can be viewed at a time")
|
||||
}
|
||||
if len(args) == 0 {
|
||||
if tty.IsTerminal(os.Stdin.Fd()) {
|
||||
return 1, fmt.Errorf("STDIN is a terminal and no filename specified. See --help")
|
||||
}
|
||||
input_file = os.Stdin
|
||||
global_state.input_file_name = "/dev/stdin"
|
||||
} else {
|
||||
input_file, err = os.Open(args[0])
|
||||
if err != nil {
|
||||
return 1, err
|
||||
}
|
||||
if tty.IsTerminal(input_file.Fd()) {
|
||||
return 1, fmt.Errorf("%s is a terminal not paging it", args[0])
|
||||
}
|
||||
global_state.input_file_name = args[0]
|
||||
}
|
||||
follow := global_state.opts.Follow
|
||||
if follow && global_state.input_file_name == "/dev/stdin" {
|
||||
follow = false
|
||||
}
|
||||
go read_input(input_file, global_state.input_file_name, input_channel, follow, global_state.opts.Role == "scrollback")
|
||||
return
|
||||
}
|
||||
|
||||
func EntryPoint(parent *cli.Command) {
|
||||
create_cmd(parent, main)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env python
|
||||
# License: GPLv3 Copyright: 2024, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
|
||||
import sys
|
||||
|
||||
from kitty.simple_cli_definitions import CompletionSpec
|
||||
|
||||
OPTIONS = """
|
||||
--role
|
||||
default=pager
|
||||
choices=pager,scrollback
|
||||
The role the pager is used for. The default is a standard less like pager.
|
||||
|
||||
|
||||
--follow
|
||||
type=bool-set
|
||||
Follow changes in the specified file, automatically scrolling if currently on the last line.
|
||||
""".format
|
||||
|
||||
help_text = """\
|
||||
Display text in a pager with various features such as searching, copy/paste, etc.
|
||||
Text can some from the specified file or from STDIN. If no filename is specified
|
||||
and STDIN is not a TTY, it is used.
|
||||
"""
|
||||
usage = '[filename]'
|
||||
|
||||
|
||||
def main(args: list[str]) -> None:
|
||||
raise SystemExit('Must be run as kitten pager')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv)
|
||||
elif __name__ == '__doc__':
|
||||
cd = sys.cli_docs # type: ignore
|
||||
cd['usage'] = usage
|
||||
cd['options'] = OPTIONS
|
||||
cd['help_text'] = help_text
|
||||
cd['short_desc'] = 'Pretty, side-by-side diffing of files and images'
|
||||
cd['args_completion'] = CompletionSpec.from_string('type:file mime:text/* group:"Text files"')
|
||||
Reference in new issue
Block a user