Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasbestle committed Sep 13, 2015
0 parents commit 59c0c95
Show file tree
Hide file tree
Showing 3 changed files with 284 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# The MIT License (MIT)

Copyright (c) 2015 Lukas Bestle <https://lukasbestle.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# welcome

> A more beautiful and informative shell login message
## Introduction

If you use the shell on your computer or server a lot, it makes sense to customize everything to your needs. Still, many people I know get greeted by a dollar sign and a boxy cursor.

I wanted to see at the first glance on which host I'm working and what the current state of the system is. All of that should be beautiful and flexible to configure. So I created welcome, which looks like this on my Uberspace account:

![](https://cdn.codesignd.net/projects/welcome/screenshot.png)

The information below the fancy dynamic ASCII art comes from a modular information provider setup. Each line is its own shell script that can do whatever it wants and output one or more lines of (colored) text. The information then gets layouted automatically by indenting it properly and adding a heading in front of it.

*BTW, before you ask: That is [iTerm 2](http://iterm2.com) with the Solarized Dark color preset and [oh-my-zsh](http://ohmyz.sh) with the agnoster theme.*

## Setup

### Install welcome

The `welcome` script from the `bin` directory can be installed anywhere in your `$PATH`. `~/bin` makes sense, but it can be any directory.

### Install figlet, a dependency of welcome

[figlet](http://www.figlet.org) is a command line tool to generate fancy ASCII art from text and is a required depencency of welcome.

1. Install figlet itself. You can build it from source, but much easier (for example on Uberspace) is the following command to setup everything automatically:
```bash
toast arm ftp://ftp.figlet.org/pub/figlet/program/unix/figlet-2.2.5.tar.gz
```

2. Get colossal, a figlet ASCII font that looks great but is not included by default: Download the file [colossal.flf](http://www.figlet.org/fonts/colossal.flf) from the figlet website and place it in `~/.welcome/colossal.flf`.

### Create your own information providers

The reason welcome is so flexible is the modular information provider system. There are two different kinds of information providers:

#### Summarizers

A summarizer is a function that returns one or more lines of text. Summarizers are expected to always output information and are therefore useful for information like the current date, the system uptime etc.

You can place as many summarizers as you need in `~/.welcome/summarizers`. Each summarizer is a shell script with a specific structure:

```bash
function helloWorldSummarizer() {
echo "World"
}

registerSummarizer "Hello" helloWorldSummarizer
```

Each summarizer must contain at least one function that outputs arbitrary text. You then need to use `registerSummarizer <label> <function>` to register your function.
This summarizer will output a simple `Hello: World`.

The order of the summarizers gets defined by the order in the file system. So to force a specific order, simply prepend numbers, like so: `01-helloworld.sh`.

**There is one thing you need to do before this can work: Your summarizers must actually be executable files to make sure only files you wanted to run are run, so please set `chmod +x ~/.welcome/summarizers/*`**.

#### Warners

Warners are the second kind of information provider. The difference to summarizers is that a warner does not have to output anything. They are useful for information that is only relevant in specific situations (for example when a package update is available like in the screenshot above).

You can place as many warners as you need in `~/.welcome/warners`. Warners are arbitrary binaries that simply output text.

The order of the warners also gets defined by the order in the file system. So to force a specific order, simply prepend numbers, like so: `01-helloworld.sh`.

**Same here: Please set `chmod +x ~/.welcome/warners/*` to make sure only files you wanted to run are run**.

#### Examples

You can find the code for the example from the screenshot in the [my-welcome](https://git.lukasbestle.com/welcome/my-welcome) repository.

### Test and add to your shell startup file

You can now type `welcome` and the output should look quite similar to mine above. The only thing left to do is to add `welcome` to your `.bash_profile` or `.zshrc` file to run `welcome` when logging in.

## Author

- Lukas Bestle <[email protected]>

## License

This project was published under the terms of the MIT license. You can find a copy [over at the repository](https://git.lukasbestle.com/welcome/welcome/blob/master/LICENSE.md).
180 changes: 180 additions & 0 deletions bin/welcome
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
#!/usr/bin/env bash
########################################################################
# 2015-08-02 Lukas Bestle [email protected]
########################################################################
# Prints a login welcome message with status information and ASCII art
# This tool relies on a modular information provider setup in ~/.welcome
# See README for detailed information
#
# Usage: welcome
########################################################################

# Define indentation
indentation=' '

# -----------------------------------------
# Check for availability of files and tools

if ! command -v figlet &> /dev/null; then
echo -e "\033[34mfiglet\033[31m is not available in your \033[34mPATH\033[31m.\033[0m"
echo -e "\033[31mPlease install \033[34mfiglet\033[31m for ASCII art generation before using this tool.\033[0m"
echo
echo -e "\033[31mWelcome anyway. :)\033[0m"
exit 1
fi
if [[ ! -d ~/.welcome ]]; then
echo -e "\033[34m~/.welcome\033[31m does not exist.\033[0m"
echo -e "\033[31mPlease follow the installation instructions in \033[34mREADME\033[31m.\033[0m"
echo
echo -e "\033[31mWelcome anyway. :)\033[0m"
exit 1
fi
if [[ ! -f ~/.welcome/colossal.flf ]]; then
echo -e "\033[34m~/.welcome/colossal.flf\033[31m does not exist.\033[0m"
echo -e "\033[31mPlease follow the installation instructions in \033[34mREADME\033[31m.\033[0m"
echo
echo -e "\033[31mWelcome anyway. :)\033[0m"
exit 1
fi

# --------------------
# Define output buffer

output=""
function echoPrepare() {
if [[ "$1" == "-n" ]]; then
output+="$2"
else
output+="$1\n"
fi
}

# ----------------
# Create ASCII art

# Strip domain from hostname
computerName=${HOSTNAME%%.*}

# Generate ASCII art and indent every line of it
ascii=$(figlet -k -f ~/.welcome/colossal.flf -w 150 "$USER@$computerName")
asciiIndented=$(echo "$ascii" | sed "s/^/$indentation/")

# Generate user and hostname string (colored for output, uncolored for length determination)
userAndHostname="$USER@$computerName ($HOSTNAME)"
userAndHostnameColored="\033[1;32m$USER\033[0m@\033[36m$computerName\033[0m (\033[36m$HOSTNAME\033[0m)"

# Center user and hostname string

# Get the first line of the ASCII art, determine its length, remove 1 (trailing newline)
asciiFirstline=$(echo "$ascii" | head -1)
asciiLength=$(( ${#asciiFirstline} - 1 ))

# Get the length of the unformatted user and hostname string
userAndHostnameLength=${#userAndHostname}

# Calculate the space padding on the left to center the string
userAndHostnameCenterPaddingLength=$(( ($asciiLength - $userAndHostnameLength) / 2 ))

# Put it together
userAndHostnameCentered="\033[${userAndHostnameCenterPaddingLength}C$userAndHostnameColored"

# Build output string
echoPrepare "\033[1;38m"
echoPrepare
echoPrepare "$asciiIndented"
echoPrepare "\033[0m"
echoPrepare "\033[3A" # Go 3 lines up
echoPrepare "$indentation$userAndHostnameCentered"

# --------------------
# Load env summarizers

if ls ~/.welcome/summarizers/*.sh &> /dev/null; then
echoPrepare

# Store for summarizers
maxLength=0
declare -a summarizers
declare -A summarizerFunctions

# Define registration function
function registerSummarizer() {
label="$1"
function="$2"

# Check if all parameters are valid
if [[ -z "$label" ]] || [[ -z "$function" ]] || ! declare -f "$function" &> /dev/null; then
echo -e "\033[31mInvalid summarizer.\033[0m"
return 1
fi

# Store longest label
labelLength=${#label}
if [[ $labelLength -gt $maxLength ]]; then
maxLength=$labelLength
fi

# Store summarizer
summarizers+=("$label")
summarizerFunctions["$label"]="$function"
}

# Load the summarizer files and let them register themselves
for summarizer in ~/.welcome/summarizers/*.sh; do
# Check if the file is executable (security check)
if [[ ! -x "$summarizer" ]]; then
continue
fi

source "$summarizer"
done

# Generate padding after newlines
totalPadding="$(head -c $(( $maxLength + 2 )) < /dev/zero | tr '\0' ' ')"

# Run the summarizers and generate output
for label in "${summarizers[@]}"; do
# Calculate needed padding after the given label
paddingLength=$(( $maxLength - ${#label} + 1 ))
padding="$(head -c $paddingLength < /dev/zero | tr '\0' ' ')"

# Run the summarizer and indent its output
# Only indent the second and every following line
summarizerOutput="$("${summarizerFunctions["$label"]}" | sed "2,\$s/^/$indentation$totalPadding/")"

# Build output string
echoPrepare "$indentation\033[1;35m$label:\033[0m$padding$summarizerOutput\033[0m"
done
fi

# ------------
# Load warners

# Load the warner files
for warner in ~/.welcome/warners/*; do
# Check if the file is executable (security check)
if [[ ! -x "$warner" ]]; then
continue
fi

# Run the warner and indent its output
warnerOutput="$("$warner" | sed "s/^/$indentation/")"

# Output only if the result is not empty
if [[ -n "$warnerOutput" ]]; then
echoPrepare
echoPrepare "$warnerOutput"
fi
done

# -------------
# Create footer

echoPrepare
echoPrepare "$indentation\033[1;38mHave a nice day, \033[32m$USER\033[0;1;38m!\033[0m"
echoPrepare

# --------------------------------
# Finally print everything at once

echo -ne "$output"

0 comments on commit 59c0c95

Please sign in to comment.