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:
1134 files changed
+433629
No files matched your search
@@ -0,0 +1,194 @@
|
||||
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package tool
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"github.com/kovidgoyal/kitty/kittens/ask"
|
||||
"github.com/kovidgoyal/kitty/tools/cli"
|
||||
"github.com/kovidgoyal/kitty/tools/cli/markup"
|
||||
"github.com/kovidgoyal/kitty/tools/tty"
|
||||
"github.com/kovidgoyal/kitty/tools/utils"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
type ConfirmPolicy uint8
|
||||
|
||||
const (
|
||||
ConfirmAlways = iota
|
||||
ConfirmNever
|
||||
ConfirmIfNeeded
|
||||
)
|
||||
|
||||
func ask_for_permission(script_path string) (response string, err error) {
|
||||
opts := &ask.Options{Type: "choices", Default: "n", Choices: []string{"y;green:Yes", "n;red:No", "v;yellow:View", "e;magenta:Edit"}}
|
||||
|
||||
ctx := markup.New(true)
|
||||
opts.Message = ctx.Prettify(fmt.Sprintf(
|
||||
"Attempting to execute the script: :yellow:`%s`\nExecuting untrusted scripts can be dangerous. Proceed anyway?", script_path))
|
||||
response, err = ask.GetChoices(opts)
|
||||
return response, err
|
||||
}
|
||||
|
||||
func ask_if_exe_allowed(exe_path string) (ok bool, err error) {
|
||||
opts := &ask.Options{Type: "yesno", Default: "n"}
|
||||
ctx := markup.New(true)
|
||||
opts.Message = ctx.Prettify(fmt.Sprintf(
|
||||
"Attempting to execute the program: :yellow:`%s`\nExecuting untrusted programs can be dangerous. Proceed anyway?", exe_path))
|
||||
response, err := ask.GetChoices(opts)
|
||||
return response == "y", err
|
||||
}
|
||||
|
||||
func permission_denied(script_path string) error {
|
||||
return fmt.Errorf("Execution of %s was denied by user", script_path)
|
||||
}
|
||||
|
||||
func confirm_and_run_exe(args []string) (rc int, err error) {
|
||||
exe := args[len(args)-1]
|
||||
ok, err := ask_if_exe_allowed(exe)
|
||||
if err != nil {
|
||||
return 1, err
|
||||
}
|
||||
if ok {
|
||||
exe = utils.FindExe(args[0])
|
||||
if exe == "" {
|
||||
return 1, fmt.Errorf("Failed to find the script interpreter: %s", args[0])
|
||||
}
|
||||
if err = unix.Exec(exe, []string{exe}, os.Environ()); err != nil {
|
||||
rc = 1
|
||||
}
|
||||
} else {
|
||||
return 1, permission_denied(exe)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func confirm_and_run_shebang(args []string, confirm_policy ConfirmPolicy) (rc int, err error) {
|
||||
script_path := args[len(args)-1]
|
||||
do_confirm := true
|
||||
switch confirm_policy {
|
||||
case ConfirmNever:
|
||||
do_confirm = false
|
||||
case ConfirmAlways:
|
||||
do_confirm = true
|
||||
case ConfirmIfNeeded:
|
||||
do_confirm = unix.Access(script_path, unix.X_OK) != nil
|
||||
}
|
||||
if do_confirm {
|
||||
response, err := ask_for_permission(script_path)
|
||||
if err != nil {
|
||||
return 1, err
|
||||
}
|
||||
switch response {
|
||||
default:
|
||||
return 1, permission_denied(script_path)
|
||||
case "v":
|
||||
raw, err := os.ReadFile(script_path)
|
||||
if err != nil {
|
||||
return 1, err
|
||||
}
|
||||
cli.ShowHelpInPager(utils.UnsafeBytesToString(raw))
|
||||
// The pager might have exited automatically if there is less than
|
||||
// one screen of text, so confirm manually, here, where output from
|
||||
// pager will still be visible.
|
||||
fmt.Print("Execute the script? (y/n): ")
|
||||
q, err := tty.ReadSingleByteFromTerminal()
|
||||
if err != nil {
|
||||
return 1, err
|
||||
}
|
||||
if q != 'y' && q != 'Y' {
|
||||
fmt.Println()
|
||||
return 1, permission_denied(script_path)
|
||||
}
|
||||
fmt.Print("\x1b[H\x1b[2J") // clear screen
|
||||
case "e":
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return 1, err
|
||||
}
|
||||
editor := exec.Command(exe, "edit-in-kitty", script_path)
|
||||
editor.Stdin = os.Stdin
|
||||
editor.Stdout = os.Stdout
|
||||
editor.Stderr = os.Stderr
|
||||
if err = editor.Run(); err != nil {
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(err, &exitErr) {
|
||||
return exitErr.ExitCode(), nil
|
||||
}
|
||||
return 1, err
|
||||
}
|
||||
return confirm_and_run_shebang(args, ConfirmIfNeeded)
|
||||
case "y":
|
||||
}
|
||||
}
|
||||
exe := utils.FindExe(args[0])
|
||||
if exe == "" {
|
||||
return 1, fmt.Errorf("Failed to find the script interpreter: %s", args[0])
|
||||
}
|
||||
err = unix.Exec(exe, args, os.Environ())
|
||||
if err != nil {
|
||||
rc = 1
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func run_shebang(args []string) (rc int, err error) {
|
||||
if len(args) < 3 {
|
||||
return 1, fmt.Errorf("Usage: kitten __shebang__ confirm-exe path_to_script cmd...")
|
||||
}
|
||||
var confirm_policy ConfirmPolicy
|
||||
switch args[0] {
|
||||
case "confirm-always":
|
||||
confirm_policy = ConfirmAlways
|
||||
case "confirm-never":
|
||||
confirm_policy = ConfirmNever
|
||||
case "confirm-if-needed":
|
||||
confirm_policy = ConfirmIfNeeded
|
||||
default:
|
||||
return 1, fmt.Errorf("Unknown confirmation policy: %s", args[1])
|
||||
}
|
||||
script_path := args[1]
|
||||
cmd := args[2:]
|
||||
if len(cmd) == 1 && cmd[0] == "__ext__" {
|
||||
ext := filepath.Ext(script_path)
|
||||
if ext == "" || ext == "." {
|
||||
return 1, fmt.Errorf("%s has no file extension so cannot be used in __ext__ mode", script_path)
|
||||
}
|
||||
cmd = []string{ext[1:]}
|
||||
}
|
||||
f, err := os.Open(script_path)
|
||||
if err != nil {
|
||||
return 1, err
|
||||
}
|
||||
scanner := bufio.NewScanner(f)
|
||||
first_line := ""
|
||||
if scanner.Scan() {
|
||||
first_line = scanner.Text()
|
||||
} else if err = scanner.Err(); err != nil {
|
||||
f.Close()
|
||||
return 1, fmt.Errorf("Failed to read from %s with error: %w", script_path, err)
|
||||
}
|
||||
f.Close()
|
||||
if strings.HasPrefix(first_line, "#!") {
|
||||
first_line = strings.TrimSpace(first_line[2:])
|
||||
switch runtime.GOOS {
|
||||
case "darwin":
|
||||
cmd = strings.Split(first_line, " ")
|
||||
default:
|
||||
cmd = strings.SplitN(first_line, " ", 2)
|
||||
}
|
||||
}
|
||||
cmd = append(cmd, script_path)
|
||||
return confirm_and_run_shebang(cmd, confirm_policy)
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package tool
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/kovidgoyal/kitty/kittens/ask"
|
||||
"github.com/kovidgoyal/kitty/kittens/broadcast"
|
||||
"github.com/kovidgoyal/kitty/kittens/choose_files"
|
||||
"github.com/kovidgoyal/kitty/kittens/choose_fonts"
|
||||
"github.com/kovidgoyal/kitty/kittens/clipboard"
|
||||
"github.com/kovidgoyal/kitty/kittens/command_palette"
|
||||
"github.com/kovidgoyal/kitty/kittens/desktop_ui"
|
||||
"github.com/kovidgoyal/kitty/kittens/diff"
|
||||
"github.com/kovidgoyal/kitty/kittens/dnd"
|
||||
"github.com/kovidgoyal/kitty/kittens/hints"
|
||||
"github.com/kovidgoyal/kitty/kittens/hyperlinked_grep"
|
||||
"github.com/kovidgoyal/kitty/kittens/icat"
|
||||
"github.com/kovidgoyal/kitty/kittens/notify"
|
||||
"github.com/kovidgoyal/kitty/kittens/panel"
|
||||
"github.com/kovidgoyal/kitty/kittens/query_terminal"
|
||||
"github.com/kovidgoyal/kitty/kittens/quick_access_terminal"
|
||||
"github.com/kovidgoyal/kitty/kittens/remote_file"
|
||||
"github.com/kovidgoyal/kitty/kittens/resize_window"
|
||||
"github.com/kovidgoyal/kitty/kittens/show_key"
|
||||
"github.com/kovidgoyal/kitty/kittens/ssh"
|
||||
"github.com/kovidgoyal/kitty/kittens/themes"
|
||||
"github.com/kovidgoyal/kitty/kittens/transfer"
|
||||
"github.com/kovidgoyal/kitty/kittens/unicode_input"
|
||||
"github.com/kovidgoyal/kitty/tools/cli"
|
||||
"github.com/kovidgoyal/kitty/tools/cmd/at"
|
||||
"github.com/kovidgoyal/kitty/tools/cmd/atexit"
|
||||
"github.com/kovidgoyal/kitty/tools/cmd/benchmark"
|
||||
"github.com/kovidgoyal/kitty/tools/cmd/edit_in_kitty"
|
||||
"github.com/kovidgoyal/kitty/tools/cmd/mouse_demo"
|
||||
"github.com/kovidgoyal/kitty/tools/cmd/pytest"
|
||||
"github.com/kovidgoyal/kitty/tools/cmd/run_shell"
|
||||
"github.com/kovidgoyal/kitty/tools/cmd/show_error"
|
||||
"github.com/kovidgoyal/kitty/tools/cmd/update_self"
|
||||
"github.com/kovidgoyal/kitty/tools/tui"
|
||||
"github.com/kovidgoyal/kitty/tools/utils/images"
|
||||
"github.com/kovidgoyal/kitty/tools/watch"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
func KittyToolEntryPoints(root *cli.Command) {
|
||||
root.Add(cli.OptionSpec{
|
||||
Name: "--version", Type: "bool-set", Help: "The current kitten version."})
|
||||
tui.PrepareRootCmd(root)
|
||||
// @
|
||||
at.EntryPoint(root)
|
||||
// update-self
|
||||
update_self.EntryPoint(root)
|
||||
// edit-in-kitty
|
||||
edit_in_kitty.EntryPoint(root)
|
||||
// clipboard
|
||||
clipboard.EntryPoint(root)
|
||||
// dnd
|
||||
dnd.EntryPoint(root)
|
||||
// icat
|
||||
icat.EntryPoint(root)
|
||||
// ssh
|
||||
ssh.EntryPoint(root)
|
||||
// transfer
|
||||
transfer.EntryPoint(root)
|
||||
// panel
|
||||
panel.EntryPoint(root)
|
||||
// quick_access_terminal
|
||||
quick_access_terminal.EntryPoint(root)
|
||||
// resize_window
|
||||
resize_window.EntryPoint(root)
|
||||
// broadcast
|
||||
broadcast.EntryPoint(root)
|
||||
// remote_file
|
||||
remote_file.EntryPoint(root)
|
||||
// unicode_input
|
||||
unicode_input.EntryPoint(root)
|
||||
// show_key
|
||||
show_key.EntryPoint(root)
|
||||
// desktop_ui
|
||||
desktop_ui.EntryPoint(root)
|
||||
// mouse_demo
|
||||
root.AddSubCommand(&cli.Command{
|
||||
Name: "mouse-demo",
|
||||
ShortDescription: "Demo the mouse handling kitty implements for terminal programs",
|
||||
OnlyArgsAllowed: true,
|
||||
Run: func(cmd *cli.Command, args []string) (rc int, err error) {
|
||||
return mouse_demo.Run(args)
|
||||
},
|
||||
})
|
||||
// hyperlinked_grep
|
||||
hyperlinked_grep.EntryPoint(root)
|
||||
// ask
|
||||
ask.EntryPoint(root)
|
||||
// hints
|
||||
hints.EntryPoint(root)
|
||||
// diff
|
||||
diff.EntryPoint(root)
|
||||
// notify
|
||||
notify.EntryPoint(root)
|
||||
// themes
|
||||
themes.EntryPoint(root)
|
||||
themes.ParseEntryPoint(root)
|
||||
// run-shell
|
||||
run_shell.EntryPoint(root)
|
||||
// show_error
|
||||
show_error.EntryPoint(root)
|
||||
// choose-fonts
|
||||
choose_fonts.EntryPoint(root)
|
||||
// choose-files
|
||||
choose_files.EntryPoint(root)
|
||||
// command-palette
|
||||
command_palette.EntryPoint(root)
|
||||
// query-terminal
|
||||
query_terminal.EntryPoint(root)
|
||||
// __pytest__
|
||||
pytest.EntryPoint(root)
|
||||
// __hold_till_enter__
|
||||
root.AddSubCommand(&cli.Command{
|
||||
Name: "__hold_till_enter__",
|
||||
Hidden: true,
|
||||
OnlyArgsAllowed: true,
|
||||
Run: func(cmd *cli.Command, args []string) (rc int, err error) {
|
||||
return tui.ExecAndHoldTillEnter(args)
|
||||
},
|
||||
})
|
||||
// __shebang__
|
||||
root.AddSubCommand(&cli.Command{
|
||||
Name: "__shebang__",
|
||||
Hidden: true,
|
||||
OnlyArgsAllowed: true,
|
||||
Run: func(cmd *cli.Command, args []string) (rc int, err error) {
|
||||
return run_shebang(args)
|
||||
},
|
||||
})
|
||||
// __confirm_and_run_exe__
|
||||
root.AddSubCommand(&cli.Command{
|
||||
Name: "__confirm_and_run_exe__",
|
||||
Hidden: true,
|
||||
OnlyArgsAllowed: true,
|
||||
Run: func(cmd *cli.Command, args []string) (rc int, err error) {
|
||||
return confirm_and_run_exe(args)
|
||||
},
|
||||
})
|
||||
// __watch_conf__
|
||||
watch.EntryPoint(root)
|
||||
// __convert_image__
|
||||
images.ConvertEntryPoint(root)
|
||||
// __atexit__
|
||||
atexit.EntryPoint(root)
|
||||
// __width_test__
|
||||
cli.WcswidthKittenEntryPoint(root)
|
||||
// __generate_man_pages__
|
||||
root.AddSubCommand(&cli.Command{
|
||||
Name: "__generate_man_pages__",
|
||||
Hidden: true,
|
||||
OnlyArgsAllowed: true,
|
||||
Run: func(cmd *cli.Command, args []string) (rc int, err error) {
|
||||
q := root
|
||||
if len(args) > 0 {
|
||||
for _, scname := range args {
|
||||
sc := q.FindSubCommand(scname)
|
||||
if sc == nil {
|
||||
return 1, fmt.Errorf("No sub command named: %s found", scname)
|
||||
}
|
||||
if err = sc.GenerateManPages(1, true); err != nil {
|
||||
return 1, err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err = q.GenerateManPages(1, false); err != nil {
|
||||
rc = 1
|
||||
}
|
||||
}
|
||||
return
|
||||
},
|
||||
})
|
||||
benchmark.EntryPoint(root)
|
||||
}
|
||||
Reference in new issue
Block a user