Skip to content

Commit

Permalink
code format
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlivio committed Aug 14, 2023
1 parent 12aea3a commit c585981
Show file tree
Hide file tree
Showing 25 changed files with 555 additions and 555 deletions.
14 changes: 7 additions & 7 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"recommendations": [
"ms-vscode.powershell",
"foxundermoon.shell-format",
"timonwong.shellcheck",
"bierner.markdown-preview-github-styles",
"bierner.markdown-mermaid"
]
"recommendations": [
"ms-vscode.powershell",
"foxundermoon.shell-format",
"timonwong.shellcheck",
"bierner.markdown-preview-github-styles",
"bierner.markdown-mermaid"
]
}
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# bash-helpers

Template to easily create multi-OS bash helpers for Windows (MSYS2/GitBash/WSL), Ubuntu, and Mac. It is useful to let you organize helpers in `OS-dependent` or `command-dependent`. The [init.sh](init.sh) load `OS-dependent` from `os_*.bash` files after testing `$OSTYPE` and load `command-dependent` from `commands/*.bash` after testing `type <command>`.
Template to easily create multi-OS bash helpers for Windows (MSYS2/GitBash/WSL), Ubuntu, and Mac. It is useful to let you organize helpers in `OS-dependent` or `command-dependent`. The [init.sh](init.sh) load `OS-dependent` from `os_*.bash` files after testing `$OSTYPE` and load `command-dependent` from `commands/*.bash` after testing `type <command>`.
The project logo refers to the synthetic chemical element Bohrium, which also has BH's initials.

```mermaid
Expand Down Expand Up @@ -36,10 +36,10 @@ flowchart LR
init --> |"if type COMMAND_NAME then load"| command-dependent
```


## Install

The bash-helpers project has two requirements: a `bash shell` and `git`. So, run on a `bash shell` with `git`:

```bash
git clone https://github.com/alanlivio/bash-helpers ~/.bh &&\
echo "source ~/.bh/init.sh" >> ~/.bashrc &&\
Expand Down Expand Up @@ -67,22 +67,22 @@ See more OS-independent helpers [os_any.bash](os_any.bash) folder.

### os_ubu

* `ubu_update`: update os and apt packages. If defined BH_PKGS_APT, install them.
* `ubu_update`: update os and apt packages. If defined BH_PKGS_APT, install them.
* `gnome_sanity`: enable dark mode, disable animations, clean taskbar (e.g., small icons), uninstall pre-installed and not used apps (e.g., weather, news, calendar, solitaire).
* `deb_install_url`: fetch and install a deb package.

See more Ubuntu helpers in [os_ubu.bash](os_ubu.bash).

### os_mac

* `mac_update`: update os and brew packages. If defined BH_PKGS_BREW, install them.
* `mac_update`: update os and brew packages. If defined BH_PKGS_BREW, install them.
* `mac_install_brew`: install brew package manager

See more Mac helpers in [os_mac.bash](os_mac.bash).

### os_win

* `win_update`: update os and winget packages. If defined BH_PKGS_WINGET, install them.
* `win_update`: update os and winget packages. If defined BH_PKGS_WINGET, install them.
* `win_ssh_add_identity`: set ssh-agent to automatically startup and add $HOME/.ssh/id_rsa as Identity.
* `winpath`: get current path in Windows format. It uses `cygpath -m` in GitBash/MSYS2, while uses `wslpath -m` in WSL.
* `win_policy_reset`: reset group policy.
Expand Down Expand Up @@ -135,6 +135,7 @@ See more helpers in [commands/docker.bash](commands/docker.bash).
### others

See other commands at:

* [commands/adb.bash](commands/adb.bash).
* [commands/cmake.bash](commands/cmake.bash).
* [commands/ffmpeg.bash](commands/ffmpeg.bash).
Expand Down
90 changes: 45 additions & 45 deletions commands/cmake.bash
Original file line number Diff line number Diff line change
@@ -1,77 +1,77 @@
function cmake_configure_debug() {
if test -e CMakeLists.txt; then
cmake -B _build-Debug-$WSL_DISTRO_NAME$OS -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DSTATIC_LINKING=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Debug "$@"
else
cmake .. -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DSTATIC_LINKING=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Debug "$@"
fi
if test -e CMakeLists.txt; then
cmake -B _build-Debug-$WSL_DISTRO_NAME$OS -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DSTATIC_LINKING=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Debug "$@"
else
cmake .. -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DSTATIC_LINKING=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Debug "$@"
fi
}

function cmake_configure_release() {
if test -e CMakeLists.txt; then
cmake -B _build-Release-$WSL_DISTRO_NAME$OS -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DSTATIC_LINKING=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release "$@"
else
cmake .. -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DSTATIC_LINKING=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release "$@"
fi
if test -e CMakeLists.txt; then
cmake -B _build-Release-$WSL_DISTRO_NAME$OS -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DSTATIC_LINKING=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release "$@"
else
cmake .. -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DSTATIC_LINKING=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release "$@"
fi
}

function cmake_build() {
cmake --build . --target all
cmake --build . --target all
}

function cmake_clean() {
cmake --build . --target clean
cmake --build . --target clean
}

function cmake_build_target() {
cmake --build . --target $1
cmake --build . --target $1
}

function cmake_check() {
cmake --build . --target check
cmake --build . --target check
}

function cmake_install() {
case $OSTYPE in
linux*)
sudo cmake --install . --prefix /usr
;;
msys*)
cmake --install .
;;
*)
sudo cmake --install .
;;
esac
case $OSTYPE in
linux*)
sudo cmake --install . --prefix /usr
;;
msys*)
cmake --install .
;;
*)
sudo cmake --install .
;;
esac
}

function cmake_uninstall() {
local manifest="install_manifest.txt"
if test -e $manifest; then
while IFS= read -r i; do
local file=${i%$'\r'}
if test -e "$file"; then
echo "uninstall $file"
sudo rm "$file"
fi
done <$manifest
else
log_error "$manifest does not exist" && return 1
fi
local manifest="install_manifest.txt"
if test -e $manifest; then
while IFS= read -r i; do
local file=${i%$'\r'}
if test -e "$file"; then
echo "uninstall $file"
sudo rm "$file"
fi
done <$manifest
else
log_error "$manifest does not exist" && return 1
fi
}

function cmake_clean_retain_objs() {
if test -d CMakeFiles; then
find . -maxdepth 1 -not -name '.' -not -name CMakeFiles -exec rm -rf {} \;
else
log_error "there is no CMakeFiles dir" && return 1
fi
if test -d CMakeFiles; then
find . -maxdepth 1 -not -name '.' -not -name CMakeFiles -exec rm -rf {} \;
else
log_error "there is no CMakeFiles dir" && return 1
fi
}

function cmake_test_all() {
ctest
ctest
}

function cmake_test_target() {
cmake --build . --target $1
ctest -R $1
cmake --build . --target $1
ctest -R $1
}
42 changes: 21 additions & 21 deletions commands/convert.bash
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
function convert_heic_to_jpg_at_dir() {
if test -z "*.heic"; then
for file in *.heic; do convert $file ${file/%.heic/.jpg}; done
else
log_msg "no .heic file at dir"
fi
if test -z "*.heic"; then
for file in *.heic; do convert $file ${file/%.heic/.jpg}; done
else
log_msg "no .heic file at dir"
fi
}

function convert_pptx_compress_images() {
: ${1?"Usage: ${FUNCNAME[0]} <pptx_file>"}
# https://dev.to/feldroy/til-strategies-for-compressing-jpg-files-with-imagemagick-5fn9
[[ -d /tmp/pptx_extracted ]] && rm -rf /tmp/pptx_extracted/
[[ -d ${1%.*}-compressed.pptx ]] && rm -rf ${1%.*}-compressed.pptx
unzip -q "$1" -d /tmp/pptx_extracted
local large_images=$(find /tmp/pptx_extracted/ppt/media -type f -size +500k -name *.jpg -o -name *.png -o -name *.jpeg -print)
local mogrigfy_params="-sampling-factor 4:2:0 -quality 85 -strip"
[[ -z $large_images ]] && log_msg "no large images" && return
for image in $large_images; do
log_msg "compressing $(basename $image)"
mogrify $mogrigfy_params $image
done
# create file
local cwd=$(pwd)
(cd /tmp/pptx_extracted/ &&
zip -9 -q -r "$cwd/${1%.*}-compressed.pptx" *)
: ${1?"Usage: ${FUNCNAME[0]} <pptx_file>"}
# https://dev.to/feldroy/til-strategies-for-compressing-jpg-files-with-imagemagick-5fn9
[[ -d /tmp/pptx_extracted ]] && rm -rf /tmp/pptx_extracted/
[[ -d ${1%.*}-compressed.pptx ]] && rm -rf ${1%.*}-compressed.pptx
unzip -q "$1" -d /tmp/pptx_extracted
local large_images=$(find /tmp/pptx_extracted/ppt/media -type f -size +500k -name *.jpg -o -name *.png -o -name *.jpeg -print)
local mogrigfy_params="-sampling-factor 4:2:0 -quality 85 -strip"
[[ -z $large_images ]] && log_msg "no large images" && return
for image in $large_images; do
log_msg "compressing $(basename $image)"
mogrify $mogrigfy_params $image
done
# create file
local cwd=$(pwd)
(cd /tmp/pptx_extracted/ &&
zip -9 -q -r "$cwd/${1%.*}-compressed.pptx" *)
}
14 changes: 7 additions & 7 deletions commands/docker.bash
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
alias docker_prune="docker container prune; docker image prune -a"
function docker_run_at_same_folder(){
: ${2?"Usage: ${FUNCNAME[0]} <image> <command and args>"}
local image=$1
shift 1
local cmd="$*"
docker run -v $(pwd):$(pwd) -w $(pwd) -it $image $cmd
}
function docker_run_at_same_folder() {
: ${2?"Usage: ${FUNCNAME[0]} <image> <command and args>"}
local image=$1
shift 1
local cmd="$*"
docker run -v $(pwd):$(pwd) -w $(pwd) -it $image $cmd
}
16 changes: 8 additions & 8 deletions commands/ffmpeg.bash
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
function ffmpeg_cut_mp4() {
: ${3?"Usage: ${FUNCNAME[0]} <video> <begin_time_in_format_00:00:00> <end_time_in_format_00:00:00>"}
ffmpeg -i $1 -vcodec copy -acodec copy -ss $2 -t $3 -f mp4 cuted-$1
: ${3?"Usage: ${FUNCNAME[0]} <video> <begin_time_in_format_00:00:00> <end_time_in_format_00:00:00>"}
ffmpeg -i $1 -vcodec copy -acodec copy -ss $2 -t $3 -f mp4 cuted-$1
}

function ffmpeg_show_motion_vectors() {
: ${1?"Usage: ${FUNCNAME[0]} <video>"}
ffplay -flags2 +export_mvs -vf codecview=mv=pf+bf+bb $1
: ${1?"Usage: ${FUNCNAME[0]} <video>"}
ffplay -flags2 +export_mvs -vf codecview=mv=pf+bf+bb $1
}

function ffmpeg_images_merge_to_mp4() {
: ${1?"Usage: ${FUNCNAME[0]} <image>"}
ffmpeg -loop_input -i "$1".png -t 5 "$1".mp4
: ${1?"Usage: ${FUNCNAME[0]} <image>"}
ffmpeg -loop_input -i "$1".png -t 5 "$1".mp4
}

function ffmpeg_mp4_files_merge() {
: ${1?"Usage: ${FUNCNAME[0]} <file1> ... "}
ffmpeg -f concat -safe 0 -i <(for f in "$@"; do echo "file '$PWD/$f'"; done) -c copy output.mp4
: ${1?"Usage: ${FUNCNAME[0]} <file1> ... "}
ffmpeg -f concat -safe 0 -i <(for f in "$@"; do echo "file '$PWD/$f'"; done) -c copy output.mp4
}
Loading

0 comments on commit c585981

Please sign in to comment.