Skip to content
Permalink
Browse files

eventkit-controller: Add configurable truncation length via env variable

  • Loading branch information...
andrewchidden committed Sep 30, 2018
1 parent cf835b1 commit c21a89e9c3fd192c58de91b921fe609358f27766
Showing with 28 additions and 7 deletions.
  1. +8 −2 app-region/eventkit-controller.sh
  2. +20 −5 tests/eventkit-controller-tests.bats
@@ -16,6 +16,11 @@
sys_root_arg=${1:-${BTT_SYS_ROOT}}
btt_sys_root=${sys_root_arg:-~/.btt}

# Optionally set the `BTT_EVENTKIT_MAXLENGTH` environment variable to control
# the maximum length of the calendar text status.
eventkit_maxlength_arg=${2:-${BTT_EVENTKIT_MAXLENGTH}}
btt_eventkit_maxlength=${eventkit_maxlength_arg:-40}

##### @implementation #####
status_directory="${btt_sys_root}/eventkit-service"
status_filepath="${status_directory}/status"
@@ -24,8 +29,9 @@ current_status="$(cat ${status_filepath})"
if [[ -n "${current_status}" ]]; then
echo "${current_status}" | \
awk "{str = \$0; \
if (length > 33) \
print substr(str, 1, 15) \"\" substr(str, length - 14, 15); \
if (length - 1 > ${btt_eventkit_maxlength}) \
print substr(str, 1, ${btt_eventkit_maxlength} / 2) \"\" \
substr(str, length - ${btt_eventkit_maxlength} / 2 + 1, length); \
else \
print str}"
else
@@ -39,23 +39,38 @@ function tear_down() {
tear_down
}

@test 'message equals long status' {
@test 'truncates middle of long status' {
set_up

# Given the status is too long
local message='Some long meeting name that does not fit in 3.1 hrs'
local expected_result='Some long meeti… fit in 3.1 hrs'
local expected_result='Some long meeting na…s not fit in 3.1 hrs'
echo "${message}" > "${test_status_filepath}"
# When the controller is run
local message
message="$(${eventkit_controller} ${test_status_root_directory})"
# Then it should be the same with three dots in between
# the first and last 15 characters
message="$(${eventkit_controller} ${test_status_root_directory} 40)"
# Then it should be the same message but truncated using an ellipsis
# unicode character in the middle.
[ "${message}" = "${expected_result}" ]

tear_down
}

@test 'does not truncate status equal to max length' {
set_up

# Given the status is equal to the max length specified
local expected_message='Some long meeting name that d in 3.1 hrs'
echo "${expected_message}" > "${test_status_filepath}"
# When the controller is run
local message
message="$(${eventkit_controller} ${test_status_root_directory} 40)"
# Then it should not add the ellipsis unicode character or truncate.
[ "${message}" = "${expected_message}" ]

tear_down
}

@test 'error message for empty status' {
set_up

0 comments on commit c21a89e

Please sign in to comment.
You can’t perform that action at this time.