Skip to content

Commit 0145149

Browse files
author
Bhavi Dhingra
committed
refactor(gstashapply): code reuse
1 parent 9811d86 commit 0145149

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

cmd/gstashapply

+4-10
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,21 @@
22

33
#shellcheck disable=SC1091 # Not following: ./__assertgitrepo was not specified as input
44
#shellcheck disable=SC2155 # Declare and assign separately to avoid masking return values
5-
#shellcheck disable=SC2001 # See if you can use ${variable//search/replace}
65

76
source "${__CUSTOM_GIT_UTIL}"/fzf_headers/__gstash_header
87
source "${__CUSTOM_GIT_UTIL}"/__assertgitrepo
8+
source "${__CUSTOM_GIT_UTIL}"/__select_stash_id
99

1010
__assertgitrepo
1111

1212
function main() {
1313

14-
local GSTASH_HEADER="$(__gstash_header)"
14+
local GSTASH_HEADER="$(__gstash_header "apply")"
1515

16-
local selectedStr="$(git stash list | \
17-
fzf --bind '?:toggle-preview'\
18-
--header "${GSTASH_HEADER}"\
19-
--preview "source ${__CUSTOM_GIT_UTIL}/fzf_previews/__gstash_preview; __gstash_preview {}")"
20-
[ -z "${selectedStr}" ] && return
16+
local stashId="$(__select_stash_id "${GSTASH_HEADER}")"
17+
[ -z "${stashId}" ] && return
2118

22-
local stashId="$(echo "${selectedStr}" | sed 's/\:.*//g')"
2319
git stash apply "${stashId}"
24-
25-
git status
2620
}
2721

2822
main

util/__select_stash_id

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
#shellcheck disable=SC2155 # Declare and assign separately to avoid masking return values
4+
#shellcheck disable=SC2001 # See if you can use ${variable//search/replace}
5+
6+
# Usage: __select_stash_id "${HEADER}"
7+
function __select_stash_id() {
8+
9+
local GSTASH_HEADER="${1}"
10+
local selectedStr="$(git stash list | \
11+
fzf --bind '?:toggle-preview'\
12+
--header "${GSTASH_HEADER}"\
13+
--preview "source ${__CUSTOM_GIT_UTIL}/fzf_previews/__gstash_preview; __gstash_preview {}")"
14+
15+
echo "${selectedStr}" | sed 's/\:.*//g'
16+
}

util/fzf_headers/__gstash_header

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ source "$__CUSTOM_CONSOLE_UTIL"/__common
88

99
function __gstash_header() {
1010

11-
local msg="gstashapply | enter: apply stash | ?: toggle preview"
12-
local colorMsgL1="${UNDERLINE}${MAGENTA}gstashapply${RESET} | \
13-
${BLUE}enter:${RESET} apply stash | \
11+
local op="${1}"
12+
local cmd="gstash${1}"
13+
local msg="${cmd} | enter: ${op} stash | ?: toggle preview"
14+
local colorMsgL1="${UNDERLINE}${MAGENTA}gstash${op}${RESET} | \
15+
${BLUE}enter:${RESET} ${op} stash | \
1416
${BLUE}?:${RESET} toggle preview"
15-
local colorMsgL2=" | ${BLUE}esc:${RESET} quit"
17+
local spaces="$(echo "${cmd}" | sed -e 's/./ /g')"
18+
local colorMsgL2="${spaces} | ${BLUE}esc:${RESET} quit"
1619

1720
local edge="$(echo "${msg}" | sed -e 's/./-/g')"
1821
local GSTASH_HEADER="${edge}"'

0 commit comments

Comments
 (0)