summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2025-02-09 11:53:35 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2025-02-09 11:53:35 +0900
commit2b584586ed1caf15429625da981575ee35d407b8 (patch)
tree6a4458be6e685f944214dc4c243234b7b8065cd7 /src/util
parenta1994ff0abb48dfe4c6951ad67e837f4c767cc39 (diff)
downloadfzf-2b584586ed1caf15429625da981575ee35d407b8.tar.gz
Add --accept-nth option to transform the output
This option can be used to replace a sed or awk in the post-processing step. ps -ef | fzf --multi --header-lines 1 | awk '{print $2}' ps -ef | fzf --multi --header-lines 1 --accept-nth 2 This may not be a very "Unix-y" thing to do, so I've always felt that fzf shouldn't have such an option, but I've finally changed my mind because: * fzf can be configured with a custom delimiter that is a fixed string or a regular expression. * In such cases, you'd need to repeat the delimiter again in the post-processing step. * Also, tools like awk or sed may interpret a regular expression differently, causing mismatches. You can still use sed, cut, or awk if you prefer. Close #3987 Close #1323
Diffstat (limited to 'src/util')
-rw-r--r--src/util/chars.go21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/util/chars.go b/src/util/chars.go
index 4b9cca01..dd037caa 100644
--- a/src/util/chars.go
+++ b/src/util/chars.go
@@ -189,6 +189,27 @@ func (chars *Chars) TrimTrailingWhitespaces() {
chars.slice = chars.slice[0 : len(chars.slice)-whitespaces]
}
+func (chars *Chars) TrimSuffix(runes []rune) {
+ lastIdx := len(chars.slice)
+ firstIdx := lastIdx - len(runes)
+ if firstIdx < 0 {
+ return
+ }
+
+ for i := firstIdx; i < lastIdx; i++ {
+ char := chars.Get(i)
+ if char != runes[i-firstIdx] {
+ return
+ }
+ }
+
+ chars.slice = chars.slice[0:firstIdx]
+}
+
+func (chars *Chars) SliceRight(last int) {
+ chars.slice = chars.slice[:last]
+}
+
func (chars *Chars) ToString() string {
if runes := chars.optionalRunes(); runes != nil {
return string(runes)