aboutsummaryrefslogtreecommitdiff
path: root/wrapper.go
blob: 45c04f42aa3b5c39fd8b2b27c354c6f5ffa691bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main

const shellWrapper=`
# Declare constant for the executable

# Print error message
__ws_err() {
	if [[ $# -eq 0 ]]; then
		echo -n "ws: " 1>&2
		cat <&0 1>&2
	elif [[ $# -eq 1 ]]; then
		echo "ws: $1" 1>&2
	fi
}

# Execute internal workspace command
__ws_internal() {
	__WS_INTERNAL_EXE="ws_internal"
	${__WS_INTERNAL_EXE} "$@"
	return $?
}

# Record the current working directory before changing to workspace
declare WS_PREVIOUS_LOC=""
__ws_push_wd() {
	WS_PREVIOUS_LOC="$(pwd)"
}

# Return to the previously recorded directory
__ws_pop_wd() {
	if [[ -z "${WS_PREVIOUS_LOC}" ]]; then
		ws_err "No working directory was recorded prior to entering the workspace"
		return 1
	fi
	cd "${WS_PREVIOUS_LOC}"
	WS_PREVIOUS_LOC=""
	return 0
}

# Workspace usage instruction
__ws_usage() {
    __ws_err <<_EOF
Usage: ws [command]

ws is a program for managing temporary workspaces.

Commands:
  list|ls          List all available workspaces
  new              Create a new workspace and change into it
  print_current_workspace|pcw
                   Print the current workspace path
  recent|rec       Change to the most recently used workspace
  next|n           Change to the next workspace in the history
  prev|p           Change to the previous workspace in the history
  remove|rm        Remove the current workspace and return to the previous one
  return|ret       Return to the previous workspace
  usage            Show this usage information
_EOF
}

# Workspace function
ws() {
	local cmd="${1:-recent}"

	case "$cmd" in
		"list"|"ls")
			__ws_internal list_workspaces
			;;
		"new")
			__ws_push_wd
			local ws_path="$(__ws_internal create_new_workspace)"
			echo "${ws_path}"
			[[ ! $? -eq 0 ]] && return $?
			cd "${ws_path}"
			;;
		"print_current_workspace"|"pcw")
			__ws_internal print_current_workspace
			[[ ! $? -eq 0 ]] && return $?
			;;
		"recent"|"rec"|"next"|"n"|"prev"|"p")

			local internal_cmd=""
			if [[ "$cmd" == "rec" || "$cmd" == "recent" ]]; then
				__ws_push_wd
				internal_cmd="recent"
			elif [[ "$cmd" == "next" || "$cmd" == "n" ]]; then
				internal_cmd="next"
			elif [[ "$cmd" == "prev" || "$cmd" == "p" ]]; then
				internal_cmd="prev"
			fi

			local ws_path="$(__ws_internal "print_${internal_cmd}_workspace")"
			[[ ! $? -eq 0 ]] && return $?
			cd "${ws_path}"

			;;
		"remove"|"rm")
			local ws_path="$(__ws_internal print_recent_workspace)"
			rm -rf "${ws_path}"
			if [[ -n "${WS_PREVIOUS_LOC}" ]]; then
				__ws_pop_wd || return $?
			else
				local ws_path="$(__ws_internal print_workspace_root)"
				[[ ! $? -eq 0 ]] && return $?
				cd "${ws_path}"
			fi
			;;
		"return"|"ret")
			__ws_pop_wd
			;;
		"usage")
			__ws_usage
			;;
		*)
			__ws_usage
			return 1
			;;
	esac
}
`