Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add text alignment #1282

Closed
wants to merge 0 commits into from
Closed

Add text alignment #1282

wants to merge 0 commits into from

Conversation

zjp
Copy link
Contributor

@zjp zjp commented Jul 13, 2019

Let's try this again with a clean git log -- sorry about that.

Description

Adds the option to align the output to the space after the end of the separator string ":", "->", etc by padding all arguments to printf by the same amount regardless of length

Features

--align_output 'left', 'right', 'off'
left: Don't right-justify labels like "OS", but align what gets listed after
right: Align labels to make their contents aligned
off: Default behavior of neofetch

Issues

  1. Addresses [Feature Request] Formatting #1241 and Is there padding/align option? #895

Screenshot 2019-07-12 at 16 46 11

Screenshot 2019-07-12 at 16 46 28

Screenshot 2019-07-12 at 16 46 49

neofetch Outdated Show resolved Hide resolved
neofetch Outdated
@@ -3978,27 +3984,31 @@ prin() {
if [[ "$(trim "$1")" && "$2" ]]; then
[[ "$json" ]] && { printf ' %s\n' "\"${1}\": \"${2}\","; return; }

string="${1}${2:+: $2}"
string="${1}"
Copy link
Owner

Choose a reason for hiding this comment

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

Why was this removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The way things are printed in this PR makes concatenating arg2 to arg1 redundant, but arg1 is still formatted correctly.

Copy link
Contributor Author

@zjp zjp Nov 28, 2019

Choose a reason for hiding this comment

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

I have a more complete explanation now.

Originally, this if statement joined the two arguments into the same string, but to properly align the info by label, the labels and their corresponding data had to stay separate. I've left the JSON logic in place, but set string to the first argument in both cases, so I've updated the code to be

# If $2 doesn't exist we format $1 as info.
if [[ "$(trim "$1")" && "$2" ]]; then
    [[ "$json" ]] && { printf '    %s\n' "\"${1}\": \"${2}\","; return; }
fi
string="${1}"
string="$(trim "${string//$'\e[0m'}")"

Now I'm not really sure what the lines

length="$(strip_sequences "$string")"
length="${#length}"

do, but removing them doesn't seem to change anything, so I've left them in.

Anyway, not concatenating $2 to $1 has several benefits:

First, we still strip all sequences from the labels, standardize them to bold, and get the length.

Second, now I can pad the first argument of calls to prin with an arbitrary number of \033[0m \033[0m ... and manually center text if I want to give my own short byline like this screenshot:
Screenshot 2019-11-28 at 00 44 28
BUT DO NOTE that using sequences in prin -- for whatever reason -- means the string is not considered by the alignment logic. For this statement to be true the sequences have to be raw (e.g. you have to put \33[0m in your string instead of ${reset}). If you try to use substitutions the whole strong is considered and it throws the info way to the right. I consider this a feature, but I can see how someone might want this to be fixed later, so that they can choose whether to exclude something and use ${reset}${bold}${italic} etc.

Third, we can still properly detect automatically whether or not the user probably wants a separator at the end of the string with

string="${string}${reset}${colon_color}"
[[ "$2" ]] && string="${string}${separator}${separator:=:}"

Fourth: Now users can use arbitrary sequences to format text the way they like (italic, flashing, etc).

With the string formatting simplified we can do:

printf '%b' "${text_padding:+\e[${text_padding}C}${zws}"
case "$align_output" in
    "left")  printf '%-*b' $offset "${string}" ;;
    "right") printf '%+*b' $offset "${string}" ;;
    "off") printf '%b' "${string}" ;;
esac
[[ "$2" ]] && printf '%b' "${info_color}${2}"
printf '\n'

If any of this breaks existing behavior I haven't witnessed it, so I'll need feedback from people with more exotic setups.

neofetch Outdated Show resolved Hide resolved
neofetch Outdated Show resolved Hide resolved
neofetch Outdated Show resolved Hide resolved
neofetch Outdated Show resolved Hide resolved
neofetch Outdated Show resolved Hide resolved
@zjp zjp force-pushed the alignment branch 2 times, most recently from 2399ff0 to 3415b90 Compare November 16, 2019 13:27
@zjp
Copy link
Contributor Author

zjp commented Nov 16, 2019

IT'S ALIVE! Finally. Pending your review, I think it's ready -- at least, it's as good as it can be given my novice bash-fu.

Screen Shot 2019-11-16 at 7 16 52 AM

@zjp zjp force-pushed the alignment branch 2 times, most recently from df1bccb to 1d4b925 Compare December 4, 2019 17:36
@zjp zjp changed the title Beta version of alignment algorithm Add alignment Dec 4, 2019
@zjp zjp changed the title Add alignment Add text alignment Dec 4, 2019
neofetch Outdated Show resolved Hide resolved
neofetch Outdated Show resolved Hide resolved
neofetch Outdated Show resolved Hide resolved
neofetch Outdated Show resolved Hide resolved
neofetch Outdated Show resolved Hide resolved
neofetch Outdated Show resolved Hide resolved
neofetch Outdated Show resolved Hide resolved
neofetch Outdated Show resolved Hide resolved
neofetch Outdated Show resolved Hide resolved
neofetch Outdated Show resolved Hide resolved
neofetch Outdated Show resolved Hide resolved
neofetch Outdated Show resolved Hide resolved
neofetch Outdated Show resolved Hide resolved
@zjp zjp force-pushed the alignment branch 2 times, most recently from a6da753 to c52715a Compare December 6, 2019 17:18
neofetch Outdated Show resolved Hide resolved
@zjp
Copy link
Contributor Author

zjp commented Jul 19, 2020

I went ahead and rebased this on master to see if it would fix those failing checks from a few months ago, and it seems that it has.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants