From 346a651a48da46c84f687752345ff5611e26cebc Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 20 Jul 2023 21:54:39 +0530 Subject: [PATCH] Add option to control compression modes --- kittens/transfer/main.py | 8 ++++++++ kittens/transfer/send.go | 10 +++++----- kittens/transfer/utils.go | 8 +++++++- kitty_tests/file_transmission.py | 2 ++ 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/kittens/transfer/main.py b/kittens/transfer/main.py index c5467c80d..e0fa5e962 100644 --- a/kittens/transfer/main.py +++ b/kittens/transfer/main.py @@ -26,6 +26,14 @@ receiving computer. In :code:`normal` mode the last argument is assumed to be a destination path on the receiving computer. +--compress +default=auto +choices=auto,never,always +Whether to compress data being sent. By default compression is enabled based on the +type of file being sent. For files recognized as being already compressed, compression +is turned off as it just wastes CPU cycles. + + --permissions-bypass -p The password to use to skip the transfer confirmation popup in kitty. Must match the password set for the :opt:`file_transfer_confirmation_bypass` option in diff --git a/kittens/transfer/send.go b/kittens/transfer/send.go index 98fa112ab..64c2342d8 100644 --- a/kittens/transfer/send.go +++ b/kittens/transfer/send.go @@ -117,7 +117,7 @@ func get_remote_path(local_path string, remote_base string) string { return remote_base } -func NewFile(local_path, expanded_local_path string, file_id int, stat_result fs.FileInfo, remote_base string, file_type FileType) *File { +func NewFile(opts *Options, local_path, expanded_local_path string, file_id int, stat_result fs.FileInfo, remote_base string, file_type FileType) *File { stat, ok := stat_result.Sys().(*syscall.Stat_t) if !ok { panic("This platform does not support getting file identities from stat results") @@ -129,7 +129,7 @@ func NewFile(local_path, expanded_local_path string, file_id int, stat_result fs file_size: stat_result.Size(), bytes_to_transmit: stat_result.Size(), permissions: stat_result.Mode().Perm(), remote_path: filepath.ToSlash(get_remote_path(local_path, remote_base)), rsync_capable: file_type == FileType_regular && stat_result.Size() > 4096, - compression_capable: file_type == FileType_regular && stat_result.Size() > 4096 && should_be_compressed(expanded_local_path), + compression_capable: file_type == FileType_regular && stat_result.Size() > 4096 && should_be_compressed(expanded_local_path, opts.Compress), remote_initial_size: -1, } return &ans @@ -144,7 +144,7 @@ func process(opts *Options, paths []string, remote_base string, counter *int) (a } if s.IsDir() { *counter += 1 - ans = append(ans, NewFile(x, expanded, *counter, s, remote_base, FileType_directory)) + ans = append(ans, NewFile(opts, x, expanded, *counter, s, remote_base, FileType_directory)) new_remote_base := remote_base if new_remote_base != "" { new_remote_base = strings.TrimRight(new_remote_base, "/") + "/" + filepath.Base(x) + "/" @@ -166,10 +166,10 @@ func process(opts *Options, paths []string, remote_base string, counter *int) (a ans = append(ans, new_ans...) } else if s.Mode()&fs.ModeSymlink == fs.ModeSymlink { *counter += 1 - ans = append(ans, NewFile(x, expanded, *counter, s, remote_base, FileType_symlink)) + ans = append(ans, NewFile(opts, x, expanded, *counter, s, remote_base, FileType_symlink)) } else if s.Mode().IsRegular() { *counter += 1 - ans = append(ans, NewFile(x, expanded, *counter, s, remote_base, FileType_regular)) + ans = append(ans, NewFile(opts, x, expanded, *counter, s, remote_base, FileType_regular)) } } return diff --git a/kittens/transfer/utils.go b/kittens/transfer/utils.go index 36261f088..2cf81c488 100644 --- a/kittens/transfer/utils.go +++ b/kittens/transfer/utils.go @@ -84,7 +84,13 @@ func run_with_paths(cwd, home string, f func()) { f() } -func should_be_compressed(path string) bool { +func should_be_compressed(path, strategy string) bool { + if strategy == "always" { + return true + } + if strategy == "never" { + return false + } ext := strings.ToLower(filepath.Ext(path)) if ext != "" { switch ext[1:] { diff --git a/kitty_tests/file_transmission.py b/kitty_tests/file_transmission.py index 1a3b4bdca..2c75be1b1 100644 --- a/kitty_tests/file_transmission.py +++ b/kitty_tests/file_transmission.py @@ -508,3 +508,5 @@ class TestFileTransmission(BaseTest): single_file('--transmit-deltas') os.remove(dest) single_file('--transmit-deltas') + single_file('--compress=never') + single_file('--compress=always')