From 12395967eac4504ff144d7b4434f0e16371d747b Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Wed, 19 Nov 2025 04:53:25 +0100 Subject: [PATCH] completion: support timestamp --- src/borg/archiver/completion_cmd.py | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/borg/archiver/completion_cmd.py b/src/borg/archiver/completion_cmd.py index c3c2df1d5..d0106d3cc 100644 --- a/src/borg/archiver/completion_cmd.py +++ b/src/borg/archiver/completion_cmd.py @@ -41,6 +41,10 @@ The following argument types have intelligent, context-aware completion: 9. Relative time markers (relative_time_marker_validator): - Suggests common time intervals (60S, 60M, 24H, 7d, 4w, 12m, 1000y) + +10. Timestamps (timestamp): + - Completes file paths when starting with / or . + - Otherwise suggests current timestamp in ISO format """ import argparse @@ -50,6 +54,7 @@ import shtab from ._common import process_epilog from ..constants import * # NOQA from ..helpers import archivename_validator, SortBySpec, FilesCacheMode, PathSpec, ChunkerParams, tag_validator, relative_time_marker_validator +from ..helpers.time import timestamp from ..compress import CompressionSpec from ..helpers.parseformat import partial_format from ..manifest import AI_HUMAN_SORT_KEYS @@ -190,6 +195,19 @@ _borg_complete_relative_time() { compgen -W "${choices}" -- "$1" } +# Complete timestamp (file path or ISO timestamp) +_borg_complete_timestamp() { + local cur="${COMP_WORDS[COMP_CWORD]}" + + # If starts with / or ., complete as file path + if [[ "$cur" == /* || "$cur" == ./* || "$cur" == ../* || "$cur" == . || "$cur" == .. ]]; then + compgen -f -- "$cur" + else + # Suggest current timestamp in ISO format + date +"%Y-%m-%dT%H:%M:%S%z" | sed 's/\([0-9]\{2\}\)$/:\1/' + fi +} + # Complete comma-separated sort keys for any option with type=SortBySpec. # Keys are validated against Borg's AI_HUMAN_SORT_KEYS. _borg_complete_sortby() { @@ -457,6 +475,22 @@ _borg_complete_relative_time() { compadd -V 'relative time' -Q -a choices } +# Complete timestamp (file path or ISO timestamp) +_borg_complete_timestamp() { + local cur + cur="${words[$CURRENT]}" + + # If starts with / or ., complete as file path + if [[ "$cur" == /* || "$cur" == ./* || "$cur" == ../* || "$cur" == . || "$cur" == .. ]]; then + _files + else + # Suggest current timestamp in ISO format + local timestamp + timestamp=$(date +"%Y-%m-%dT%H:%M:%S%z" | sed 's/\([0-9]\{2\}\)$/:\1/') + compadd -Q -- "$timestamp" + fi +} + # Complete comma-separated sort keys for any option with type=SortBySpec. _borg_complete_sortby() { local cur @@ -626,6 +660,9 @@ class CompletionMixIn: relative_time_marker_validator, {"bash": "_borg_complete_relative_time", "zsh": "_borg_complete_relative_time"}, ) + _attach_completion( + parser, timestamp, {"bash": "_borg_complete_timestamp", "zsh": "_borg_complete_timestamp"} + ) # Collect all commands and help topics for "borg help" completion help_choices = list(self.helptext.keys())