mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-20 23:44:59 +02:00
choose files: Add default mappings to choose a file and insert it into the terminal
This commit is contained in:
@@ -772,18 +772,22 @@ func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
m := strings.Join(selections, "\n")
|
||||
if opts.OutputFormat == "json" {
|
||||
payload["paths"] = selections
|
||||
if current_filter != "" {
|
||||
payload["current_filter"] = current_filter
|
||||
}
|
||||
b, _ := json.MarshalIndent(payload, "", " ")
|
||||
m = string(b)
|
||||
payload["paths"] = selections
|
||||
if current_filter != "" {
|
||||
payload["current_filter"] = current_filter
|
||||
}
|
||||
fmt.Print(m)
|
||||
if opts.WriteOutputTo != "" {
|
||||
os.WriteFile(opts.WriteOutputTo, []byte(m), 0600)
|
||||
if tui.RunningAsUI() {
|
||||
fmt.Println(tui.KittenOutputSerializer()(payload))
|
||||
} else {
|
||||
m := strings.Join(selections, "\n")
|
||||
if opts.OutputFormat == "json" {
|
||||
b, _ := json.MarshalIndent(payload, "", " ")
|
||||
m = string(b)
|
||||
}
|
||||
fmt.Print(m)
|
||||
if opts.WriteOutputTo != "" {
|
||||
os.WriteFile(opts.WriteOutputTo, []byte(m), 0600)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,14 @@
|
||||
# License: GPLv3 Copyright: 2025, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
import sys
|
||||
from typing import Any
|
||||
|
||||
from kitty.conf.types import Definition
|
||||
from kitty.constants import appname
|
||||
from kitty.simple_cli_definitions import CONFIG_HELP, CompletionSpec
|
||||
from kitty.typing_compat import BossType
|
||||
|
||||
from ..tui.handler import result_handler
|
||||
|
||||
definition = Definition(
|
||||
'!kittens.choose_files',
|
||||
@@ -123,6 +127,32 @@ egr() # }}}
|
||||
def main(args: list[str]) -> None:
|
||||
raise SystemExit('This must be run as kitten choose-files')
|
||||
|
||||
def relative_path_if_possible(path: str, base: str) -> str:
|
||||
if not base or not path:
|
||||
return path
|
||||
from contextlib import suppress
|
||||
from pathlib import Path
|
||||
b = Path(base)
|
||||
q = Path(path)
|
||||
with suppress(ValueError):
|
||||
return str(q.relative_to(b))
|
||||
return path
|
||||
|
||||
|
||||
@result_handler(has_ready_notification=True)
|
||||
def handle_result(args: list[str], data: dict[str, Any], target_window_id: int, boss: BossType) -> None:
|
||||
paths: list[str] = data.get('paths', [])
|
||||
if not paths:
|
||||
boss.ring_bell_if_allowed()
|
||||
return
|
||||
path = paths[0]
|
||||
w = boss.window_id_map.get(target_window_id)
|
||||
if w is not None:
|
||||
cwd = w.cwd_of_child
|
||||
if cwd:
|
||||
path = relative_path_if_possible(path, cwd)
|
||||
w.paste_text(path)
|
||||
|
||||
|
||||
usage = '[directory to start choosing files in]'
|
||||
|
||||
|
||||
@@ -407,9 +407,7 @@ def handle_result(args: list[str], data: dict[str, Any], target_window_id: int,
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Run with kitty +kitten hints
|
||||
ans = main(sys.argv)
|
||||
if ans:
|
||||
print(ans)
|
||||
main(sys.argv)
|
||||
elif __name__ == '__doc__':
|
||||
cd = sys.cli_docs # type: ignore
|
||||
cd['usage'] = usage
|
||||
|
||||
Reference in New Issue
Block a user