From 5c2cee5b3beb6ab959906c2827b906e204dca953 Mon Sep 17 00:00:00 2001 From: dylan araps Date: Sun, 3 Jan 2016 17:54:16 +1100 Subject: [PATCH] Finished rewrite and added OS X support (experimental), it's now way faster! --- Readme.md | 60 ++- fetch.sh | 1149 ++++++++++++++++++++++++++++++----------------------- 2 files changed, 679 insertions(+), 530 deletions(-) diff --git a/Readme.md b/Readme.md index ab7c9f6c..bc6d8511 100644 --- a/Readme.md +++ b/Readme.md @@ -1,13 +1,8 @@ # fetch.sh -### I'm currently rewriting the script from scratch with portability in mind. - This is the home of my fetch script! This script gathers info about your system and prints it to the terminal. -I've spent the past few days rewriting this and it now supports -other distros as well as a ton of new features and bug fixes. - If you're having any issues or have any ideas, please open an issue! I can't test on many other distros and I want this to work for as many people as possible. @@ -41,18 +36,15 @@ These are the script's optional dependencies: ## Usage The script now supports dynamic image sizing and padding, - they're enabled by default and there's a variable you + it's enabled by default and there's a variable you need to set for it to work correctly. You can either change the variable $fontwidth inside of the - script or launch it with "--fontwidth num". + script or launch it with ```--font_width num```. Once you set the var the script will scale the image and padding to fit your terminal window. -You can disable this by changing the var "$img_auto" or by launching - the script with "--size px". - Please report any bugs or issues you're having with this as I can't test with many configurations. @@ -70,53 +62,52 @@ Info: --winman string/cmd Manually set the window manager --cpu string/cmd Manually set the cpu name --memory string/cmd Manually set the memory ---speed string/cmd Manually set the cpu speed --speed_type Change the type of cpu speed to get Possible values: current, min, max --song string/cmd Manually set the current song Text Colors: ---colors 1 2 3 4 Change the color of text +--colors 1 2 3 4 5 Change the color of text (title, subtitle, colon, info) ---titlecol num Change the color of the title ---subtitlecol num Change the color of the subtitle ---coloncol num Change the color of the colons ---infocol num Change the color of the info +--title_color num Change the color of the title +--subtitle_color num Change the color of the subtitle +--colon_color num Change the color of the colons +--underline_color num Change the color of the underline +--info_color num Change the color of the info Text Formatting: --underline on/off Enable/Disable title underline --underline_char char Character to use when underlineing title ---linewrap on/off Enable/Disable line wrapping +--line_wrap on/off Enable/Disable line wrapping --bold on/off Enable/Disable bold text Color Blocks: ---printcols start end Range of colors to print as blocks ---blockwidth num Width of color blocks --color_blocks on/off Enable/Disable the color blocks +--block_range start end --v + Range of colors to print as blocks +--block_width num Width of color blocks Image: --image Image to display with the script The image gets priority over other images: (wallpaper, \$img) ---fontwidth Used to automatically size the image ---size px Change the size of the image ---smart_crop on/off Smart crop images with plain color backgrounds ---smart_crop_mode Which mode to use with smart crop - Takes the values: fit, fill ---img_auto on/off Enable/Disable automatic i mage sizing ---cropoffset value Change the crop offset. Possible values: - northwest, north, northeast, west, center - east, southwest, south, southeast ---padding num How many spaces to pad the text - to the right +--font_width px Used to automatically size the image +--split_size num Width of img/text splits + A value of 2 makes each split half the terminal + width and etc +--crop_mode Which crop mode to use + Takes the values: normal, fit, fill +--crop_offset value Change the crop offset for crop_mode normal. + Possible values: northwest, north, northeast, + west, center, east, southwest, south, southeast --xoffset px How close the image will be to the left edge of the window --yoffset px How close the image will be to the top edge of the window --gap num Gap between image and text right side ---images on/off Enable/Disable all images" +--images on/off Enable/Disable all images --wall on/off Enable/Disable the wallpaper function and fallback to \$img --clean Remove all cropped images @@ -136,17 +127,14 @@ Other: Here's what's on my todo list -- Add support for Max OS X -- Add an easy way to define info prefixes at launch. -- Cleanup +- Add uptime support for OS X - Add options to bold other text in the script (info, underline, colons) -- Fix issues with multiline prompts -### Smart Crop mode comparison +### Crop mode comparison #### Fit ![fit](https://imgur.com/MI0UtSV.png) diff --git a/fetch.sh b/fetch.sh index f1406067..f80c0031 100755 --- a/fetch.sh +++ b/fetch.sh @@ -1,31 +1,459 @@ -#!/usr/bin/env bash +#!/bin/bash # Fetch info about your system # # Optional Dependencies: (You'll lose these features without them) # Displaying Images: w3m # Image Cropping: ImageMagick -# Wallpaper Display: feh Window Manager Detection: wmctrl +# Wallpaper Display: feh # Current Song: mpc # Text formatting, dynamic image size and padding: tput # # Created by Dylan Araps # https://github.com/dylanaraps/dotfiles - -# Info Prefixes {{{ -# The titles that come before the info (Ram:, Cpu:, Uptime:) -# TODO: Add an easy way to specify these at launch. +# Speed up script by not using unicode +export LC_ALL=C -title_os="OS" -title_kernel="Kernel" -title_uptime="Uptime" -title_packages="Packages" -title_shell="Shell" -title_windowmanager="Window Manager" -title_cpu="CPU" -title_memory="Memory" -title_song="Song" +# Config Options {{{ + + +# Info Options {{{ + +# Info +# What to display and in what order. +# Format is: "Subtitle: function name" +# Additional lines you can use include: +# "underline" "linebreak" "echo: msg here" "title: title here" +# You can also include your own lines by using: +# "echo: subtitlehere: $(custom cmd here)" +info=( + "gettitle" + "underline" + "OS: getos" + "Kernel: getkernel" + "Uptime: getuptime" + "Packages: getpackages" + "Shell: getshell" + "Window Manager: getwindowmanager" + "CPU: getcpu" + "Memory: getmemory" + "Song: getsong" + "linebreak" + "getcols" + "linebreak" +) + +# CPU + +# CPU speed type +# --speed_type current/min/max +speed_type="max" + + +# Color Blocks + +# Color block range +# --block_range start end +start=0 +end=7 + +# Toggle color blocks +# --color_blocks on/off +color_blocks="on" + +# Color block width +# --color_block_width num +blockwidth=3 + + +# }}} + + +# Text Colors {{{ +# --colors 1 2 3 4 5 + + +# --title_color num +title_color=4 + +# --subtitle_color num +subtitle_color=1 + +# --colon_color num +colon_color=8 + +# --underline_color num +underline_color=8 + +# --info_color num +info_color=6 + + +# }}} + + +# Text Options {{{ + + +# Toggle line wrapping +# --line_wrap on/off +line_wrap="on" + +# Toggle bold text +# --bold on/off +bold="on" + +# Toggle title underline +# --underline on/off +underline="on" + +# Underline character +# --underline_char char +underline_char="-" + + +# }}} + + +# Image Options {{{ + + +# Toggle all images +# --images on/off +images="on" + +# Thumbnail directory +imgtempdir="$HOME/.fetchimages" + +# Split Size +# Sizing for the img and text splits +# The larger the value the less space fetch will take up. +# The default value of 2 splits the image and text at +# half terminal width each. +# A value of 3 splits them at a third width each and etc. +# --split_size num +split_size=2 + +# Use current wallpaper as the image +# --wall on/off +wall="on" + +# Default image to use if wallpaper use is disabled +# --image img +img="$HOME/Pictures/avatars/gon.png" + +# Crop mode +# --crop_mode normal/fit/fill +crop_mode="normal" + +# Crop offset +# Only affects normal mode. +# --crop_offset northwest/north/northeast/west/center +# east/southwest/south/southeast +crop_offset="center" + +# Font width +# Used when calculating dynamic image size +font_width=5 + +# Right gap between image and text +# --gap num +gap=4 + +# Image offsets +# --xoffset px +# --yoffset px +yoffset=0 +xoffset=0 + + +# }}} + + +# }}} + + +# Gather Info {{{ + + +# Get Operating System +case "$(uname)" in + "Linux") + if type -p crux >/dev/null 2>&1; then + os="CRUX" + else + os="$(awk -F'=' '/^NAME=/ {printf $2; exit}' /etc/*ease)" + os=${os#\"*} + os=${os%*\"} + fi + ;; + + "Darwin") + os="Mac OS X" + ;; +esac + +# Get Title +gettitle () { + title="${USER}@$(hostname)" +} + +# Get kernel version +getkernel() { + kernel="$(uname -r)" +} + +# Get uptime +getuptime () { + case "$os" in + "Mac OS X") + # TODO: Fix uptime for OS X + uptime="Unknown" + ;; + + *) uptime="$(uptime -p)" ;; + esac +} + +# Get package count +getpackages () { + case "$os" in + "Arch Linux"|"Parabola GNU/Linux-libre"|"Manjaro"|"Antergos") + packages="$(pacman -Q | wc -l)" + ;; + + "void") + packages="$(xbps-query -l | wc -l)" + ;; + + "Ubuntu"|"Mint"|"Debian"|"Kali Linux"|"Deepin Linux") + packages="$(dpkg --get-selections | grep -v deinstall$ | wc -l)" + ;; + + "Slackware") + packages="$(ls -1 /var/log/packages | wc -l)" + ;; + + "Gentoo"|"Funtoo") + packages="$(ls -d /var/db/pkg/*/* | wc -l)" + ;; + + "Fedora"|"openSUSE"|"Red Hat Enterprise Linux"|"CentOS") + packages="$(rpm -qa | wc -l)" + ;; + + "CRUX") + packages="$(pkginfo -i | wc -l)" + ;; + + "Mac OS X") + packages="$(pkgutil --pkgs | wc -l)" + packages=${packages//[[:blank:]]/} + ;; + + *) packages="Unknown" ;; + esac +} + +# Get shell +getshell () { + shell="$SHELL" +} + +# Get window manager +getwindowmanager () { + if [ -e "$HOME/.xinitrc" ]; then + xinitrc=$(awk '/^[^#]*exec/ {print $2}' "${HOME}/.xinitrc") + windowmanager="${xinitrc/-session/}" + else + case "$os" in + "Mac OS X") + windowmanager="Quartz Compositor" + ;; + + *) + windowmanager="Unknown" + ;; + + esac + fi +} + +# Get cpu +getcpu () { + case $os in + "Mac OS X") + cpu="$(sysctl -n machdep.cpu.brand_string)" + ;; + + *) + cpu="$(awk -F ': ' '/model name/ {printf $2; exit}' /proc/cpuinfo)" + + case $speed_type in + current) speed="$(lscpu | awk '/CPU MHz:/ {printf $3}')" ;; + min) speed="$(lscpu | awk '/CPU min MHz:/ {printf $4}')" ;; + max) speed="$(lscpu | awk '/CPU max MHz:/ {printf $4}')" ;; + esac + + # Convert mhz to ghz without bc + speed=$((${speed/.*/} / 100)) + speed=${speed:0:1}.${speed:1} + cpu="$cpu @ ${speed}GHz" + ;; + esac + + # Remove uneeded patterns from cpu output + # This is faster than sed/gsub + cpu=${cpu//(tm)/} + cpu=${cpu//(TM)/} + cpu=${cpu//(r)/} + cpu=${cpu//(R)/} + cpu=${cpu// CPU/} + cpu=${cpu// Processor/} + cpu=${cpu// Six-Core/} +} + +# Get memory +getmemory () { + case $os in + "Mac OS X") + memtotal=$(printf "$(sysctl -n hw.memsize)"/1024^2 | bc) + memwired=$(vm_stat | awk '/wired/ { print $4 }') + memactive=$(vm_stat | awk '/active / { print $3 }') + memcompressed=$(vm_stat | awk '/occupied/ { print $5 }') + memused=$(((${memwired//.} + ${memactive//.} + ${memcompressed//.}) * 4 / 1024)) + memory="${memused}MB / ${memtotal}MB" + ;; + + *) + mem="$(awk 'NR < 4 {printf $2 " "}' /proc/meminfo)" + + # Split the string above into 3 vars + # This is faster than using an array. + set $mem + + memtotal=$1 + memfree=$2 + memavail=$3 + memused="$((memtotal - memavail))" + memory="$(( ${memused%% *} / 1024))MB / $(( ${memtotal%% *} / 1024))MB" + ;; + esac +} + +# Get song +getsong () { + song=$(mpc current 2>/dev/null || printf "%s" "Unknown") +} + +getcols () { + if [ "$color_blocks" == "on" ]; then + printf "%s" "${padding}" + while [ $start -le $end ]; do + printf "%s%${blockwidth}s" "$(tput setab $start)" + start=$((start + 1)) + + # Split the blocks at 8 colors + [ $end -ge 9 ] && [ $start -eq 8 ] && \ + printf "\n%s" "${clear}${padding}" + done + + # Clear formatting + printf "%s" "$clear" + fi +} + + +# }}} + + +# Images {{{ + + +getimage () { + # Check if the directory exists + [ ! -d "$imgtempdir" ] && (mkdir "$imgtempdir" || exit) + + # Get columns + columns=$(tput cols) + + # Image size is half of the terminal + imgsize=$((columns * font_width / split_size)) + + # Padding is half the terminal width + gap + padding="$(tput cuf $((columns / split_size + gap)))" + + # If wall=on, Get image to display from current wallpaper. + if [ "$wall" == "on" ]; then + img=$(awk '/feh/ {printf $3}' "$HOME/.fehbg") + img=${img#\'*} + img=${img%*\'} + fi + + # Get name of image and prefix it with it's crop mode and offset + imgname="$crop_mode-$crop_offset-${img##*/}" + + # This check allows you to resize the image at launch + if [ -f "$imgtempdir/$imgname" ]; then + imgheight=$(identify -format "%h" "$imgtempdir/$imgname") + [ $imgheight != $imgsize ] && rm "$imgtempdir/$imgname" + fi + + # Check to see if the thumbnail exists before we do any cropping. + if [ ! -f "$imgtempdir/$imgname" ]; then + # Get image size so that we can do a better crop + size=$(identify -format "%w %h" $img) + width=${size%% *} + height=${size##* } + + # This checks to see if height is geater than width + # so we can do a better crop of portrait images. + if [ $height -gt $width ]; then + size=$width + else + size=$height + fi + + case "$crop_mode" in + fit) + c=$(convert "$img" -colorspace srgb -format "%[pixel:p{0,0}]" info:) + convert \ + "$img" \ + -trim +repage \ + -gravity south \ + -background "$c" \ + -extent "$size"x"$size" \ + -scale "$imgsize"x"$imgsize" \ + "$imgtempdir/$imgname" + ;; + + fill) + c=$(convert "$img" -colorspace srgb -format "%[pixel:p{0,0}]" info:) + convert \ + "$img" \ + -trim +repage \ + -scale "$imgsize"x"$imgsize"^ \ + -background "$c" \ + -extent "$imgsize"x"$imgsize" \ + "$imgtempdir/$imgname" + ;; + + *) + convert \ + "$img" \ + -gravity $crop_offset \ + -crop "$size"x"$size"+0+0 \ + -scale "$imgsize"x"$imgsize" \ + "$imgtempdir/$imgname" + ;; + + esac + fi + + # The final image + img="$imgtempdir/$imgname" +} # }}} @@ -34,317 +462,28 @@ title_song="Song" # Text Formatting {{{ -# Line wrap -# Set this to 0 or use the flag "--wrap on/off" to disable -# line wrapping. Really useful for small terminal windows -# and long lines. -linewrap="on" - -# Set to "", comment this line or use the flag "--bold on/off" -# to disable all bold text. -bold="on" - -# Whether or not to underline the title. -# Configurable at launch with "--underline on/off" -underline="on" - -# Character to underline title with -# Configurable at launch with "--underline_char char" -underline_char="-" - -# Default colors -# Colors can be defined at launch with: -# "--titlecol 1, --subtitlecol 2, --coloncol 3, --infocol 4" -# Or the shorthand "-c/--color 1 2 3 4" -# Or by editing them below. -title_color=$(tput setaf 7) -subtitle_color=$(tput setaf 1) -colon_color=$(tput setaf 7) # Also changes underline color -info_color=$(tput setaf 7) - -# Reset formatting -# Removing this line will fuck up the text formatting -clear=$(tput sgr0) - -# Amount of left padding to use when images are disabled. -# The variable takes a count of spaces. So a value of 10 -# will pad the text to the right 10 spaces. -padding=0 - - -# }}} - - -# Custom Image {{{ - - -# Enable or disable the use of images (Disable images at launch with "--images on/off") -images="on" - -# If "on", fetch will use a cropped version of your wallpaper as the image -# (Disable this at launch with "--wall off") -# NOTE: This is only compatible with feh, I can add support for more -# wallpaper setters but you'll need to show me a way to get the current -# wallpaper from the commandline. -wall="on" - -# The image to use if wall="off". There's also the launch flag "--image" -# to set a custom image at launch. -img="$HOME/Pictures/avatars/gon.png" - -# Image size is based on terminal size -# Using the flag "--size" sets this to off. -img_auto="on" - -# This is experimental and needs testing. ( Off by default ) -# Smart crop images with plain color backgrounds. -# What this means is that your solid bg waifu wallpaper will be cropped around -# your waifu no matter where she is in the image. -smart_crop="off" - -# Smart crop has two modes, fit and fill. -# Fit: Fit the whole character into the crop. -# Fill: Fit a portrait of the character into the crop. -# Fit gives the best results when the character isn't cut off at the sides. -smart_crop_mode="fit" - -# Image size to use if img_auto="off" -# Also configureable at launch with "--size" -size=128 - -# Font width is needed to properly calulate the image size -# If there's a gap on the right try increasing the value by 1 -# If there's an overlap try decreasing the value by 1 -fontwidth=5 - -# Gap is the amount of space between the image and the text on the right -gap=4 - -# Image size/offset -# (Customizable at launch with these flags: --xoffset 0 --yoffset 0") -yoffset=0 -xoffset=0 - -# Default crop offset (Customizable at launch with --cropoffset) -# Possible values: -# northwest, north, northeast, west, center, east, southwest, south, southeast -crop_offset="center" - -# Directory to store cropped images -imgtempdir="$HOME/.fetchimages" - - -# }}} - - -# Get Info {{{ -# Commands to use when gathering info - - -# Title (Configurable with "-t" and "--title" at launch) -# To use the usual "user@hostname" change the line below to: -title="$(whoami)@$(hostname)" - -# Operating System (Configurable with "-O" and "--distro" at launch) -# You can manually set this if the command below doesn't work for you. -if type -p crux >/dev/null 2>&1; then - os="CRUX" -else - os=$(awk '/^NAME=/' /etc/*ease | sed -n 's/^NAME=//p' | tr -d '"') -fi - -# Linux kernel name/version (Configurable with "-K" and "--kernel" at launch) -kernel=$(uname -r) - -# System Uptime (Configurable with "-U" and "--uptime" at launch) -uptime=$(uptime -p) - -# Total number of packages (Configurable with "-P" and "--packages" at launch) -# If your package manager can't be found open an issue on my github repo. -# (Link is at the top) -getpackages () { - case $os in - 'void') \ - packages=$(xbps-query -l | wc -l) ;; - 'Arch Linux'|'Parabola GNU/Linux-libre'|'Manjaro'|'Antergos') \ - packages=$(pacman -Q | wc -l) ;; - 'Ubuntu'|'Mint'|'Debian'|'Kali Linux'|'Deepin Linux') \ - packages=$(dpkg --get-selections | grep -v deinstall$ | wc -l) ;; - 'Slackware') \ - packages=$(ls -1 /var/log/packages | wc -l) ;; - 'Gentoo'|'Funtoo') \ - packages=$(ls -d /var/db/pkg/*/* | wc -l) ;; - 'Fedora'|'openSUSE'|'Red Hat Enterprise Linux'|'CentOS') \ - packages=$(rpm -qa | wc -l) ;; - 'CRUX') \ - packages=$(pkginfo -i | wc -l) ;; - *) packages="unknown" ;; - esac +underline () { + uline=$(printf %"$length"s) + uline=${uline// /$underline_char} } +colors () { + title_color="$(tput setaf $title_color)" + subtitle_color="$(tput setaf $subtitle_color)" + colon_color="$(tput setaf $colon_color)" + underline_color="$(tput setaf $underline_color)" + info_color="$(tput setaf $info_color)" +} -# Shell (Configurable with "-s" and "--shell" at launch) -shell="$SHELL" - -# Window manager (Configurable with "-W" and "--windowmanager" at launch) -# (depends on wmctrl) -# This can be detected without wmctrl by using an array of window manager -# process names and pgrep but it's really slow. -# (Doubles script startup time in some cases). -# If you don't want to install wmctrl you can either edit the var below, -# export the "windowmanager" variable in your shell's configuration file, -# or run the script with: --windowmanager wmname -# windowmanager="openbox" -getwindowmanager () { - if type -p wmctrl >/dev/null 2>&1; then - windowmanager=$(wmctrl -m | awk '/Name:/ {printf $2}') - elif [ -e ~/.xinitrc ]; then - windowmanager=$(grep -v "^#" "${HOME}/.xinitrc" | tail -n 1 | cut -d " " -f2) +bold () { + if [ "$bold" == "on" ]; then + bold="$(tput bold)" else - windowmanager="Unknown" + bold="" fi } -# Processor (Configurable with "-C", "-S" and "--cpu", "--speed" at launch) -cpu="$(awk 'BEGIN{FS=":"} /model name/ {print $2; exit}' /proc/cpuinfo |\ - awk 'BEGIN{FS="@"; OFS="\n"} { print $1; exit }' |\ - sed -e 's/\((tm)\|(TM)\)//' -e 's/\((R)\|(r)\)//' -e 's/^\ //')" - -# Get current/min/max cpu speed -speed_type="max" -cpuspeed () { - case $speed_type in - current) speed="$(lscpu | awk '/CPU MHz:/ {printf "scale=1; " $3 " / 1000 \n"}' | bc -l)" ;; - min) speed="$(lscpu | awk '/CPU min MHz:/ {printf "scale=1; " $4 " / 1000 \n"}' | bc -l)" ;; - max) speed="$(lscpu | awk '/CPU max MHz:/ {printf "scale=1; " $4 " / 1000 \n"}' | bc -l)" ;; - esac -} - -# Memory (Configurable with "--memory" at launch) -# Print used ram / total ram -# This function checks the "free" command's version as -# version 3.3.0 changed the layout of columns. -getmemory () { - freeversion=$(free --version | awk '{printf $4}') - - if [ ${freeversion//.} -gt 330 ] || [${freeversion//.} -gt 3300 ]; then - memory=$(free -m | awk '/Mem:/ {printf $3 "MB / " $2 "MB"}') - else - usedmem=$(free -m | awk '/cache:/ {print $3}') - totalmem=$(free -m | awk '/Mem:/ {print $2}') - memory="${usedmem}MB / ${totalmem}MB" - fi -} - -# Currently playing song/artist (Configurable with "-m" and "--song" at launch) -if type -p mpc >/dev/null 2>&1; then - song=$(mpc current) -else - song="Unknown" -fi - -# Print terminal colors in a line -# (Configurable with "--printcols start end" at launch) -# Start/End are vars for the range of colors to print -# The default values below print 8 colors in total. -start=0 -end=7 - -# Print the color blocks by default. -color_blocks="on" - -# Widh of the color blocks -blockwidth=3 - -printcols () { - while [ "$start" -le "$end" ]; do - printf "%s%${blockwidth}s" "$(tput setab $start)" - start=$((start + 1)) - - # Split the blocks at 8 colors - [ $end -ge 9 ] && [ $start -eq 8 ] && printf "\n%${pad}s" "$clear$pad" - done - - # Clear formatting - printf %s "$clear" -} - - -# }}} - - -# Usage {{{ - - -usage () { - printf '%s\n' - printf '%s\n' "usage: ${0##*/} [--colors 1 2 4 5] [--kernel \"\$(uname -rs)\"]" - printf '%s\n' - printf '%s\n' " Info:" - printf '%s\n' " --title string Change the title at the top" - printf '%s\n' " --distro string/cmd Manually set the distro" - printf '%s\n' " --kernel string/cmd Manually set the kernel" - printf '%s\n' " --uptime string/cmd Manually set the uptime" - printf '%s\n' " --packages string/cmd Manually set the package count" - printf '%s\n' " --shell string/cmd Manually set the shell" - printf '%s\n' " --winman string/cmd Manually set the window manager" - printf '%s\n' " --cpu string/cmd Manually set the cpu name" - printf '%s\n' " --memory string/cmd Manually set the memory" - printf '%s\n' " --speed string/cmd Manually set the cpu speed" - printf '%s\n' " --speed_type Change the type of cpu speed to get" - printf '%s\n' " Possible values: current, min, max" - printf '%s\n' " --song string/cmd Manually set the current song" - printf '%s\n' - printf '%s\n' " Text Colors:" - printf '%s\n' " --colors 1 2 3 4 Change the color of text" - printf '%s\n' " (title, subtitle, colon, info)" - printf '%s\n' " --titlecol num Change the color of the title" - printf '%s\n' " --subtitlecol num Change the color of the subtitle" - printf '%s\n' " --coloncol num Change the color of the colons" - printf '%s\n' " --infocol num Change the color of the info" - printf '%s\n' - printf '%s\n' " Text Formatting:" - printf '%s\n' " --underline on/off Enable/Disable title underline" - printf '%s\n' " --underline_char char Character to use when underlineing title" - printf '%s\n' " --linewrap on/off Enable/Disable line wrapping" - printf '%s\n' " --bold on/off Enable/Disable bold text" - printf '%s\n' - printf '%s\n' " Color Blocks:" - printf '%s\n' " --printcols start end Range of colors to print as blocks" - printf '%s\n' " --blockwidth num Width of color blocks" - printf '%s\n' " --color_blocks on/off Enable/Disable the color blocks" - printf '%s\n' - printf '%s\n' " Image:" - printf '%s\n' " --image Image to display with the script" - printf '%s\n' " The image gets priority over other" - printf '%s\n' " images: (wallpaper, \$img)" - printf '%s\n' " --fontwidth px Used to automatically size the image" - printf '%s\n' " --size px Change the size of the image" - printf '%s\n' " --smart_crop on/off Smart crop images with plain color backgrounds" - printf '%s\n' " --smart_crop_mode Which mode to use with smart crop" - printf '%s\n' " Takes the values: fit, fill" - printf '%s\n' " --img_auto on/off Enable/Disable automatic i mage sizing" - printf '%s\n' " --cropoffset value Change the crop offset. Possible values:" - printf '%s\n' " northwest, north, northeast, west, center" - printf '%s\n' " east, southwest, south, southeast" - printf '%s\n' - printf '%s\n' " --padding num How many spaces to pad the text" - printf '%s\n' " to the right" - printf '%s\n' " --xoffset px How close the image will be " - printf '%s\n' " to the left edge of the window" - printf '%s\n' " --yoffset px How close the image will be " - printf '%s\n' " --gap num Gap between image and text right side" - printf '%s\n' " to the top edge of the window" - printf '%s\n' " --images on/off Enable/Disable all images" - printf '%s\n' " --wall on/off Enable/Disable the wallpaper function" - printf '%s\n' " and fallback to \$img" - printf '%s\n' " --clean Remove all cropped images" - printf '%s\n' - printf '%s\n' " Other:" - printf '%s\n' " --help Print this text and exit" - printf '%s\n' - exit 1 -} +clear="$(tput sgr0)" # }}} @@ -353,51 +492,127 @@ usage () { # Args {{{ -for argument in "$@"; do +# Usage {{{ + + +usage () { + printf "%s\n" + printf "%s\n" "usage: ${0##*/} [--colors 1 2 4 5] [--kernel \"\$(uname -rs)\"]" + printf "%s\n" + printf "%s\n" " Info:" + printf "%s\n" " --title string Change the title at the top" + printf "%s\n" " --distro string/cmd Manually set the distro" + printf "%s\n" " --kernel string/cmd Manually set the kernel" + printf "%s\n" " --uptime string/cmd Manually set the uptime" + printf "%s\n" " --packages string/cmd Manually set the package count" + printf "%s\n" " --shell string/cmd Manually set the shell" + printf "%s\n" " --winman string/cmd Manually set the window manager" + printf "%s\n" " --cpu string/cmd Manually set the cpu name" + printf "%s\n" " --memory string/cmd Manually set the memory" + printf "%s\n" " --speed_type Change the type of cpu speed to get" + printf "%s\n" " Possible values: current, min, max" + printf "%s\n" " --song string/cmd Manually set the current song" + printf "%s\n" + printf "%s\n" " Text Colors:" + printf "%s\n" " --colors 1 2 3 4 5 Change the color of text" + printf "%s\n" " (title, subtitle, colon, underline, info)" + printf "%s\n" " --title_color num Change the color of the title" + printf "%s\n" " --subtitle_color num Change the color of the subtitle" + printf "%s\n" " --colon_color num Change the color of the colons" + printf "%s\n" " --underline_color num Change the color of the underlines" + printf "%s\n" " --info_color num Change the color of the info" + printf "%s\n" + printf "%s\n" " Text Formatting:" + printf "%s\n" " --underline on/off Enable/Disable title underline" + printf "%s\n" " --underline_char char Character to use when underlineing title" + printf "%s\n" " --line_wrap on/off Enable/Disable line wrapping" + printf "%s\n" " --bold on/off Enable/Disable bold text" + printf "%s\n" + printf "%s\n" " Color Blocks:" + printf "%s\n" " --color_blocks on/off Enable/Disable the color blocks" + printf "%s\n" " --block_width num Width of color blocks" + printf "%s\n" " --block_range start end --v " + printf "%s\n" " Range of colors to print as blocks" + printf "%s\n" + printf "%s\n" " Image:" + printf "%s\n" " --image Image to display with the script" + printf "%s\n" " The image gets priority over other" + printf "%s\n" " images: (wallpaper, \$img)" + printf "%s\n" " --font_width px Used to automatically size the image" + printf "%s\n" " --split_size num Width of img/text splits" + printf "%s\n" " A value of 2 makes each split half the terminal" + printf "%s\n" " width and etc" + printf "%s\n" " --crop_mode Which crop mode to use" + printf "%s\n" " Takes the values: normal, fit, fill" + printf "%s\n" " --crop_offset value Change the crop offset for normal mode." + printf "%s\n" " Possible values: northwest, north, northeast," + printf "%s\n" " west, center, east, southwest, south, southeast" + printf "%s\n" + printf "%s\n" " --xoffset px How close the image will be " + printf "%s\n" " to the left edge of the window" + printf "%s\n" " --yoffset px How close the image will be " + printf "%s\n" " to the top edge of the window" + printf "%s\n" " --gap num Gap between image and text right side" + printf "%s\n" " to the top edge of the window" + printf "%s\n" " --images on/off Enable/Disable all images" + printf "%s\n" " --wall on/off Enable/Disable the wallpaper function" + printf "%s\n" " and fallback to \$img" + printf "%s\n" " --clean Remove all cropped images" + printf "%s\n" + printf "%s\n" " Other:" + printf "%s\n" " --help Print this text and exit" + printf "%s\n" + exit 1 +} + + +# }}} + + +while [ ! -z $1 ]; do case $1 in # Info --title) title="$2" ;; - --distro) os="$2" ;; + --os) os="$2" ;; --kernel) kernel="$2" ;; --uptime) uptime="$2" ;; --packages) packages="$2" ;; --shell) shell="$2" ;; --winman) windowmanager="$2" ;; --cpu) cpu="$2" ;; - --speed) speed="$2" ;; --speed_type) speed_type="$2" ;; --memory) memory="$2" ;; --song) song="$2" ;; # Text Colors - --colors) title_color="$(tput setaf $2)"; \ - [ ! -z $3 ] && subtitle_color="$(tput setaf $3)"; \ - [ ! -z $4 ] && colon_color="$(tput setaf $4)"; \ - [ ! -z $5 ] && info_color="$(tput setaf $5)" ;; - --titlecol) title_color="$(tput setaf $2)" ;; - --subtitlecol) subtitle_color="$(tput setaf $2)" ;; - --coloncol) colon_color="$(tput setaf $2)" ;; - --infocol) info_color="$(tput setaf $2)" ;; + --colors) title_color=$2; \ + [ ! -z $3 ] && subtitle_color=$3; \ + [ ! -z $4 ] && colon_color=$4; \ + [ ! -z $4 ] && underline_color=$5; \ + [ ! -z $5 ] && info_color=$6 ;; + --title_color) title_color=$2 ;; + --subtitle_color) subtitle_color=$2 ;; + --colon_color) colon_color=$2 ;; + --underline_color) underline_color=$2 ;; + --info_color) info_color=$2 ;; # Text Formatting --underline) underline="$2" ;; --underline_char) underline_char="$2" ;; - --linewrap) linewrap="$2" ;; + --line_wrap) line_wrap="$2" ;; --bold) bold="$2" ;; # Color Blocks - --printcols) start=$2; end=$3 ;; --color_blocks) color_blocks="$2" ;; + --block_range) start=$2; end=$3 ;; + --block_width) blockwidth="$2" ;; # Image --image) wall="off"; img="$2" ;; - --fontwidth) fontwidth="$2" ;; - --size) img_auto="off" imgsize="$2" ;; - --smart_crop) smart_crop="$2" ;; - --smart_crop_mode) smart_crop_mode="$2" ;; - --img_auto) img_auto="off" ;; - --cropoffset) crop_offset="$2" ;; - --padding) padding="$2" ;; + --font_width) font_width="$2" ;; + --split_size) split_size="$2" ;; + --crop_mode) crop_mode="$2" ;; + --crop_offset) crop_offset="$2" ;; --xoffset) xoffset="$2" ;; --yoffset) yoffset="$2" ;; --gap) gap="$2" ;; @@ -417,166 +632,112 @@ done # }}} -# Image {{{ - - -# If the script was called with --noimg, disable images and padding -if [ $images == "on" ]; then - # Check to see if auto=1 - if [ $img_auto == "on" ]; then - # Image size is half of the terminal - imgsize=$(($(tput cols) * fontwidth / 2)) - - # Padding is half the terminal width + gap - padding=$(($(tput cols) / 2 + gap)) - fi - - # If wall=on, Get image to display from current wallpaper. - # (only works with feh) - [ $wall == "on" ] && \ - img=$(awk '/feh/ {printf $3}' "$HOME/.fehbg" | sed -e "s/'//g") - - # Get name of image and prefix it with it's crop offset or smart_crop mode - if [ $smart_crop == "on" ]; then - imgname="$smart_crop_mode-${img##*/}" - else - imgname="$crop_offset-${img##*/}" - fi - - # This check allows you to resize the image at launch - if [ -f "$imgtempdir/$imgname" ]; then - imgheight=$(identify -format "%h" "$imgtempdir/$imgname") - [ $imgheight != $imgsize ] && rm "$imgtempdir/$imgname" - fi - - # Check to see if the tempfile exists before we do any cropping. - if [ ! -f "$imgtempdir/$imgname" ]; then - # Check if the directory exists and create it if it doesn't - [ ! -d "$imgtempdir" ] && (mkdir "$imgtempdir" || exit) - - # Get wallpaper size so that we can do a better crop - size=($(identify -format "%w %h" $img)) - - # This checks to see if height is geater than width - # so we can do a better crop of portrait images. - if [ ${size[1]} -gt ${size[0]} ]; then - size=${size[0]} - else - size=${size[1]} - fi - - # Crop the image, resize it and save it to $imgtempdir. - if [ $smart_crop == "on" ]; then - # Fixes transparent images having a white bg after -extent - c=$(convert "$img" -colorspace srgb -format "%[pixel:p{0,0}]" info:) - - if [ $smart_crop_mode == "fit" ]; then - convert \ - -trim +repage "$img" \ - -gravity south \ - -background "$c" \ - -extent "$size"x"$size" \ - -resize "$imgsize"x"$imgsize" \ - "$imgtempdir/$imgname" - else - convert \ - -trim +repage "$img" \ - -resize "$imgsize"x"$imgsize"^ \ - -background "$c" \ - -extent "$imgsize"x"$imgsize" \ - "$imgtempdir/$imgname" - fi - else - convert \ - -crop "$size"x"$size"+0+0 \ - -gravity $crop_offset "$img" \ - -resize "$imgsize"x"$imgsize" \ - "$imgtempdir/$imgname" - fi - fi - - # The final image - img="$imgtempdir/$imgname" -else - img="" -fi - - -# }}} - - # Print Info {{{ -# Get cpu speed -[ -z $speed ] && cpuspeed - -# Get packages -[ -z $packages ] && getpackages - -# Get window manager -[ -z $windowmanager ] && getwindowmanager - -# Get memory -[ -z $memory ] && getmemory - -# Check for whether or not we bold text -[ $bold == "on" ] && b=$(tput bold) - -# Padding -pad=$(printf "%${padding}s") - -# Hide the terminal cursor while we print the info -tput civis - -# Clear terminal before printing anything -clear - -# Underline title with length of title -[ $underline == "on" ] && uline=$(printf %"${#title}"s |tr " " "$underline_char") - -# Disable line wrap -[ $linewrap == "off" ] && printf '\e[?7l' - -# Print the title and underline -printf "%s\n" "$pad$b$title_color$title$clear" -[ $underline == "on" ] && printf "%s\n" "$pad$colon_color$uline$clear" - -# Custom printf function to make it easier to edit the info lines. printinfo () { - printf "$pad$b$subtitle_color$1$clear" - printf "$colon_color:$clear " - printf "%s\n" "$info_color$2$clear" + for info in "${info[@]}"; do + function=${info#*: } + subtitle=${info%:*} + + case "$info" in + echo:*:*) + info=${function#*: } + subtitle=${function%:*} + string="${bold}${subtitle_color}${subtitle}${clear}${colon_color}: ${info_color}${info}" + ;; + + echo:*) + string="${info_color}${function}" + length=${#function} + ;; + + title:*) + string="${bold}${title_color}${function}" + length=${#function} + ;; + + linebreak) + string="" + ;; + + underline) + if [ "$underline" == "on" ]; then + underline + string="${underline_color}${uline}" + fi + ;; + + *getos*) + continue + ;; + + *:*|*) + # Update the var + output=${function/get/} + typeset -n output=$output + + # Call the function + # [ -z "$output" ] && echo "$function"; time $function; continue + [ -z "$output" ] && $function + ;;& + + gettitle) + string="${bold}${title_color}${output}" + length=${#output} + ;; + + *:*) + string="${bold}${subtitle_color}${subtitle}${clear}${colon_color}: ${info_color}${output}" + length=${#subtitle} + ;; + + *) + string="$output" + length=${#output} + ;; + + esac + + printf "%s\n" "${padding}${string}${clear}" + done } -printinfo "$title_os" "$os" -printinfo "$title_kernel" "$kernel" -printinfo "$title_uptime" "$uptime" -printinfo "$title_packages" "$packages" -printinfo "$title_shell" "$shell" -printinfo "$title_windowmanager" "$windowmanager" -printinfo "$title_cpu" "$cpu @ ${speed}GHz" -printinfo "$title_memory" "$memory" -printinfo "$title_song" "$song" - -# Display the color blocks -printf "\n" -[ $color_blocks == "on" ] && printf "$pad$(printcols)" - -# Enable line wrap again -[ $linewrap == "off" ] && printf '\e[?7h' - -# If w3mimgviewer is found Display the image -if type -p /usr/lib/w3m/w3mimgdisplay >/dev/null 2>&1; then - printf "0;1;$xoffset;$yoffset;$imgsize;$imgsize;;;;;$img\n4;\n3;" |\ - /usr/lib/w3m/w3mimgdisplay -fi - -# Show the cursor again -tput cnorm - -# Move the cursor to the bottom of the terminal -tput cup $(tput lines) - # }}} + + +# Print The Info {{{ + + +# Get image +[ "$images" == "on" ] && getimage + +# Hide the terminal cursor +tput civis + +# Clear the terminal +clear + +# Disable line wrap +[ $line_wrap == "off" ] && printf '\e[?7l' + +# Call functions and display info +colors +bold +printinfo + +# Display the image +[ "$images" == "on" ] && printf "0;1;$xoffset;$yoffset;$imgsize;$imgsize;;;;;$img\n4;\n3;" |\ + /usr/lib/w3m/w3mimgdisplay + +# Enable line wrap again +[ $line_wrap == "off" ] && printf '\e[?7h' + +# Move cursor to bottom and redisplay it. +printf "cup $(tput lines) \n cuu1 \n cnorm" | tput -S + + +# }}} + +