Skip to content

Commit

Permalink
Adding actual screen dimension functions (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
kigster authored Oct 3, 2022
1 parent 26af14c commit 7c1cc89
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.3
3.0.4
24 changes: 10 additions & 14 deletions doc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,18 @@ For each passed argument checks if it's installed.



* [output.screen-width.actual()](#outputscreen-widthactual)
* [output.screen-height.actual()](#outputscreen-heightactual)
* [section()](#section)

### `output.screen-width.actual()`

OS-independent way to determine screen width.

### `output.screen-height.actual()`

OS-independent way to determine screen height.

### `section()`

Prints a "arrow-like" line using powerline characters
Expand Down Expand Up @@ -1839,20 +1849,6 @@ Prints values of all variables starting with prefixes in args
---
## File `lib/background.sh`
# lib/background.sh
Run a bunch of jobs on the background and wait for their completion
---
Expand Down
Binary file modified doc/USAGE.pdf
Binary file not shown.
66 changes: 40 additions & 26 deletions lib/output.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ function output.reset-min-max-width() {

output.reset-min-max-width

# @description OS-independent way to determine screen width.
output.screen-width.actual() {
local w
util.os
if [[ ${AppCurrentOS} =~ darwin ]]; then
w="$(.output.stty.field columns)"
elif [[ ${AppCurrentOS} =~ linux ]]; then
w="$(stty -a 2>/dev/null | grep columns | awk '{print $7}' | sedx 's/;//g')"
fi
printf -- "%d" "$w"
}

# @description OS-independent way to determine screen height.
output.screen-height.actual() {
local h
util.os
if [[ ${AppCurrentOS} =~ darwin ]]; then
h="$(.output.stty.field rows)"
elif [[ ${AppCurrentOS} =~ linux ]]; then
h="$(stty -a 2>/dev/null | grep rows | awk '{print $5}' | sedx 's/;//g')"
fi
printf -- "%d" "$h"
}

function output.constrain-screen-width() {
export LibOutput__WidthDetectionStrategy="constrained"
[[ $1 -gt 0 ]] && output.set-max-width "$1"
Expand Down Expand Up @@ -105,7 +129,6 @@ cursor.restore() {
printf "\e[u"
}


output.print-at-x-y() {
local x=$1
shift
Expand Down Expand Up @@ -136,29 +159,26 @@ output.color.off() {

.output.stty.field() {
local field="$1"
stty -a 2>/dev/null| grep "${field}" | tr -d ';' | tr ' ' '\n' | grep -B 1 "${field}" | head -1
stty -a 2>/dev/null | grep "${field}" | tr -d ';' | tr ' ' '\n' | grep -B 1 "${field}" | head -1
}


.output.current-screen-width.unconstrained() {
local w
util.os
if output.is-pipe; then
printf -- '%d' "${LibOutput__MaxWidth:-80}"
else
if [[ ${AppCurrentOS} =~ darwin ]]; then
w="$(.output.stty.field columns)"
elif [[ ${AppCurrentOS} =~ linux ]]; then
w="$(stty -a 2>/dev/null | grep columns | awk '{print $7}' | sedx 's/;//g')"
fi
printf -- "%d" "$w"
output.screen-width.actual
fi

}

screen.width.actual() {
.output.current-screen-width.unconstrained
}

screen.height.actual() {
.output.screen-height
}

.output.current-screen-width.constrained() {
local w=$(.output.current-screen-width.unconstrained)

Expand Down Expand Up @@ -207,14 +227,7 @@ screen.width.actual() {
}

.output.screen-height() {
util.os
local h
if [[ ${AppCurrentOS} =~ darwin ]]; then
h="$(.output.stty.field rows)"
elif [[ ${AppCurrentOS} =~ linux ]]; then
h="$(stty -a 2>/dev/null | grep rows | awk '{print $5}' | sedx 's/;//g')"
fi

local h=$(output.screen-height.actual)
[[ -z ${h} ]] && h=${LibOutput__MinHeight}
[[ ${h} -lt ${LibOutput__MinHeight} ]] && h=${LibOutput__MinHeight}
printf -- $((h - 2))
Expand Down Expand Up @@ -397,21 +410,23 @@ ascii-clean() {

.output.width() {
local w=$(screen.width)
printf "%d" $((w - 4))
printf "%d" $((w - 4))
}

.output.left-as-is() {
.output.left-as-is.black-text "$@"
}

.output.left-as-is.black-text() {
local bg="${1}"; shift # bar background
local tfg="${1}"; shift # text foreground
local bg="${1}"
shift # bar background
local tfg="${1}"
shift # text foreground
local text="$*"

local len=${#text}
len=$(( len + 5 ))
local tlen=$(( len - 5 ))
len=$((len + 5))
local tlen=$((len - 5))
text="$(printf -- "%${tlen}.${tlen}s" "${text}")"

local fg="${txtblk}"
Expand Down Expand Up @@ -440,7 +455,7 @@ ascii-clean() {
local text="$*"
printf "\n${color}"
if output.is-terminal; then
local width=$(( $(.output.width) - 4 ))
local width=$(($(.output.width) - 4))
printf -- " %-${width}.${width}s${clr}\n\n" "${text}"
else
printf -- " ❯❯ %-${width}.${width}s${clr}\n\n" "${text}"
Expand Down Expand Up @@ -650,4 +665,3 @@ ui.closer.kind-of-ok:() {
ui.closer.kind-of-ok $@
echo
}

14 changes: 13 additions & 1 deletion test/output_test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ source lib/output.sh
source lib/color.sh
source lib/util.sh

set +e
set -e

@test "ascii-pipe() should remove color and other escape sequences from STDIN" {
set -e
Expand All @@ -31,3 +31,15 @@ set +e
[[ ${is_pipe} -eq 1 ]]
}

@test "output.screen-width.actual()" {
export COLUMNS=120
local w=$(output.screen-width.actual)
[[ $w -eq 0 ]]
}

@test "output.screen.height.actual()" {
export ROWS=20
local h=$(output.screen-width.actual)
[[ $h -eq 0 ]]
}

0 comments on commit 7c1cc89

Please sign in to comment.