Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Motd updates #8

Merged
merged 3 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@
# along with termux-tools. If not, see
# <https://www.gnu.org/licenses/>.

SUBDIRS = scripts doc mirrors

# We have two variants of our motd message:
# 1. motd.sh: is sourced and echo'es a motd message with something that looks
# like termux's logo.
# 3. motd-playstore: plain text that will only be displayed if the termux-app
# version is very old (like the version found in playstore is)
sysconf_DATA = motd.sh motd-playstore
SUBDIRS = scripts doc mirrors motds

do_subst = sed -e "s%[@]TERMUX_PREFIX[@]%$(termux_prefix)%g" \
-e "s%[@]TERMUX_APP_PACKAGE[@]%${termux_app_package}%g" \
Expand All @@ -41,7 +34,7 @@ $1: $1.in Makefile
endef

# Login script
sysconf_DATA += termux-login.sh
sysconf_DATA = termux-login.sh

# profile.d script
pkgdata_PROFILE = init-termux-properties.sh
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ AC_SUBST(termux_package_manager)
AC_PROG_LN_S

AC_CONFIG_FILES([Makefile scripts/Makefile doc/Makefile
mirrors/Makefile])
mirrors/Makefile motds/Makefile])

AC_OUTPUT
25 changes: 0 additions & 25 deletions motd.sh

This file was deleted.

71 changes: 71 additions & 0 deletions motds/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright (C) 2022 Termux

# This file is part of termux-tools.

# termux-tools is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# termux-tools is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with termux-tools. If not, see
# <https://www.gnu.org/licenses/>.


# We have two variants of our motd message loaded by login script:
# 1. motd.sh: A dynamic motd that is executed with bash and echos a
# motd message with escape sequences, optionally with a terminal logo.
# 2. motd: A static motd that is just read as is with cat and displayed.

# The motd-playstore is an additional motd that will only be displayed
# if the termux-app version is very old (like the version found on playstore)
sysconf_DATA = motd.sh motd motd-playstore

CLEANFILES = motd.sh motd


do_subst = sed -e "s%[@]TERMUX_PREFIX[@]%$(termux_prefix)%g"

define process-rule
$1: $1.in Makefile
@echo "Creating $1"
@$$(do_subst) < $(srcdir)/$1.in > $1
endef

$(eval $(call process-rule,motd.sh))


motd:
@echo "Creating motd file"

@echo "Welcome to Termux!" > motd
@echo "" >> motd
@echo "Docs: https://termux.dev/docs" >> motd
@echo "Donate: https://termux.dev/donate" >> motd
@echo "Community: https://termux.dev/community" >> motd

@echo "" >> motd
@echo "Working with packages:" >> motd
@echo "" >> motd
@echo " - Search: pkg search <query>" >> motd
@echo " - Install: pkg install <package>" >> motd
@echo " - Upgrade: pkg upgrade" >> motd

ifeq ($(TERMUX_PACKAGE_MANAGER),apt)
truboxl marked this conversation as resolved.
Show resolved Hide resolved
@echo "" >> motd
@echo "Subscribing to additional repositories:" >> motd
@echo "" >> motd
@echo " - Root: pkg install root-repo" >> motd
@echo " - X11: pkg install x11-repo" >> motd
@echo "" >> motd
@echo "For fixing any repository issues," >> motd
@echo "try 'termux-change-repo' command." >> motd
endif

@echo "" >> motd
@echo "Report issues at https://termux.dev/issues" >> motd
File renamed without changes.
61 changes: 61 additions & 0 deletions motds/motd.sh.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!@TERMUX_PREFIX@/bin/bash

# Setup TERMUX_APP_PACKAGE_MANAGER
source "@TERMUX_PREFIX@/bin/termux-setup-package-manager" || exit 1

terminal_width="$(stty size | cut -d" " -f2)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terminal width can be determined by simply reading the value of $COLUMNS.
Unless that environment variable is unavailable for some reason in this context, in which case please inform me so I can fix this in my own version.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$COLUMS is not set if bash shell is not interactive, like when motd is executed, unless -i is passed, which creates other problems. It can still be set if checkwinsize is set, but requires starting a sub shell. stty is posix and should be fine to use.

https://stackoverflow.com/a/563592

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah that makes sense, I did have checkwinsize set, but it's probably better to use stty in this context.

if [[ "$terminal_width" =~ ^[0-9]+$ ]] && [ "$terminal_width" -gt 60 ]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a fixed range for the maximum column width of a line on the motd we can evaluate the maximal line length dynamically using something like this.

len_motd=$(echo "$motd_logo" | sed -E "s/\\\e\[[0-9;]+m//g" | wc -L)

if (($COLUMNS > $len_motd))
        then echo -ne "$motd_logo"

We read in the motd, strip out any escape sequences, and pass it to wc -L which returns the column count of the longest line.

With $motd_logo being a Heredoc/variable containing the part of the motd liable to being distorted by wrapping

Copy link
Member Author

@agnostic-apollo agnostic-apollo Aug 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am aware of that, but no need to do such processing and calling additional external binaries like sed and wc. If motd is updated, the commiter can adjust size. Moreover, 60 is a good default for column width for motd anyways.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's a reasonable concern.


motd="
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned above utilizing a Heredoc may be desirable in the interest of cleanliness and maintainability.

# Store $IFS for safekeeping
OLD_IFS=$IFS
# Logo portion of the MOTD.
IFS='' read -rd '' motd_logo << LOGO_EOF
\e[47m                \e[0m  \e[1mWelcome to Termux!\e[0m
\e[47m  \e[0m            \e[47m\e[47m  \e[0m
\e[47m  \e[0m  \e[47m  \e[0m        \e[47m  \e[0m  \e[1mDocs:\e[0m   \e[4mtermux.dev/docs\e[0m
\e[47m  \e[0m  \e[47m  \e[0m        \e[47m  \e[0m  \e[1mGitter:\e[0m \e[4mgitter.im/termux/termux\e[0m
\e[47m  \e[0m            \e[47m  \e[0m  \e[1mCommunity:\e[0m \e[4mtermux.dev/community\e[0m
\e[47m  \e[0m            \e[47m\e[47m  \e[0m
\e[47m                \e[0m  \e[1mTermux version:\e[0m ${TERMUX_VERSION-Unknown}
LOGO_EOF

Storing the initial $IFS may be necessary if a user has set a custom Input Field Separator.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for heredoc, since it is slower since it requires creation of temp file and also additional utils like read or cat. We are also appending motd variable, so current way is better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm didn't think of that.

\e[47m \e[0m \e[1mWelcome to Termux!\e[0m
\e[47m \e[0m \e[0;37m\e[47m .\e[0m
\e[47m \e[0m \e[47m \e[0m \e[47m \e[0m \e[1mDocs:\e[0m \e[4mhttps://termux.dev/docs\e[0m
\e[47m \e[0m \e[47m \e[0m \e[47m \e[0m \e[1mDonate:\e[0m \e[4mhttps://termux.dev/donate\e[0m
\e[47m \e[0m \e[47m \e[0m \e[1mCommunity:\e[0m \e[4mhttps://termux.dev/community\e[0m
\e[47m \e[0m \e[0;37m\e[47m .\e[0m
\e[47m \e[0m \e[1mTermux version:\e[0m ${TERMUX_VERSION-Unknown}
"

motd_indent=" "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Storing the indent of the motd as a separate variable is a great idea and I will be adopting it into my version as it simplifies handling the indentation significantly.


else

motd="
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same rationale as above regarding Heredocs for cleanliness and maintainability applies here, and to subsequent motd parts.

\e[1mWelcome to Termux!\e[0m

\e[1mDocs:\e[0m \e[4mhttps://termux.dev/docs\e[0m
\e[1mDonate:\e[0m \e[4mhttps://termux.dev/donate\e[0m
\e[1mCommunity:\e[0m \e[4mhttps://termux.dev/community\e[0m

\e[1mTermux version:\e[0m ${TERMUX_VERSION-Unknown}
"

motd_indent=""
fi

motd+="
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concatenating the motd variable with applicable sections is a significantly more elegant approach than my current solution, I will will be adopting it.

${motd_indent}\e[1mWorking with packages:\e[0m
${motd_indent}\e[1mSearch:\e[0m pkg search <query>
${motd_indent}\e[1mInstall:\e[0m pkg install <package>
${motd_indent}\e[1mUpgrade:\e[0m pkg upgrade
"


if [ "$TERMUX_APP_PACKAGE_MANAGER" = "apt" ]; then

motd+="
${motd_indent}\e[1mSubscribing to additional repos:\e[0m
${motd_indent}\e[1mRoot:\e[0m pkg install root-repo
${motd_indent}\e[1mX11:\e[0m pkg install x11-repo

${motd_indent}For fixing any repository issues,
${motd_indent}try 'termux-change-repo' command.
"

fi

motd+="
${motd_indent}Report issues at \e[4mhttps://termux.dev/issues\e[0m
"

echo -e "$motd"
8 changes: 5 additions & 3 deletions scripts/login.in
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/bin/sh

if tty >/dev/null 2>&1 && [ $# = 0 ] && [ ! -f ~/.hushlogin ] && [ -z "$TERMUX_HUSHLOGIN" ]; then
if [ -f @TERMUX_PREFIX@/etc/motd.sh ]; then
# Dynamic motd is preferred over plain-text.
bash @TERMUX_PREFIX@/etc/motd.sh
# Use user defined dynamic motd file if it exists
if [ -f ~/.termux/motd.sh ]; then
[ ! -x ~/.termux/motd.sh ] && chmod u+x ~/.termux/motd.sh
~/.termux/motd.sh
# Default to termux-tools package provided static motd file if it exists
elif [ -f @TERMUX_PREFIX@/etc/motd ]; then
cat @TERMUX_PREFIX@/etc/motd
fi
Expand Down