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

Option to show memory in GB #1170

Closed
ShayBox opened this issue Jan 17, 2019 · 14 comments
Closed

Option to show memory in GB #1170

ShayBox opened this issue Jan 17, 2019 · 14 comments

Comments

@ShayBox
Copy link

ShayBox commented Jan 17, 2019

An option for the memory module that will let you show in more formats than MiB, maybe allow bits, bytes, KB, MB, GB, TB, with/without decimal place, and maybe an option for how many decimal places to show.

So instead of 3433MiB / 23974MiB It could show 3.4GiB / 24GiB

@konimex
Copy link
Contributor

konimex commented Jan 17, 2019

In bash, our normal math operations don't really allow decimals so it'd require some hack (e.g. $((3433/100)) and add a . manually (and IIRC neofetch used to do this, forgot when).

Maybe we can use both KiB, MiB, and GiB, but bits, bytes, and even TiB seems pointless unless there are someone out there that uses at least 1024GiB RAM.

@ShayBox
Copy link
Author

ShayBox commented Jan 17, 2019

yeah some of them would be pointless, More or less just to give users more options, the most useful ones would be MiB, GiB and maybe KiB if you're running linux on an old computer.

@TriMoon
Copy link

TriMoon commented Jan 18, 2019

How about using this example to make it output like you want: 😉

#!/usr/bin/env bash
num=3000000
divider=1024
units="GiB"
precision=2
printf -v out "%'.*f %s" \
	$precision \
	$(($num / $divider)).$(($num % $divider)) \
	$units
printf "out='%s'\n" "$out"

gives

out='2,929.70 GiB'

@ShayBox
Copy link
Author

ShayBox commented Jan 18, 2019

How do I use that with the neofetch memory module though 🤔

@dylanaraps
Copy link
Owner

We can do this:

    case "${mem_label:-$memory_format}" in
        "GiB")
            ((mem_used=mem_used/1024))
            ((mem_total=mem_total/1024))
            mem_label="GiB"
        ;;

        "KiB")
            ((mem_used=mem_used*1024))
            ((mem_total=mem_total*1024))
            mem_label="KiB"
        ;;
    esac

This ignores the conversion if mem_label is already set (AIX) and does a basic conversion. Only issue is it throws away the value after the decimal.

@dylanaraps
Copy link
Owner

We can also do this to "fake" it.

    # Use $mem_label if available.
    case "${mem_label:-$memory_format}" in
        "GiB")
            mem_used="${mem_used:0:-3}.${mem_used/${mem_used:0:-2}}"
            mem_total="${mem_total:0:-3}.${mem_total/${mem_total:0:-2}}"
            mem_label="GiB"
        ;;

        "KiB")
            ((mem_used=mem_used*1024))
            ((mem_total=mem_total*1024))
            mem_label="KiB"
        ;;
    esac

@TriMoon
Copy link

TriMoon commented Jan 22, 2019

@dylanaraps Only issue is it throws away the value after the decimal.

Why not use printf like i showed in #1170 (comment) ?
For example using a part of the code you posted:

    case "${mem_label:-$memory_format}" in
        "GiB")
            mem_label="GiB"
            divider=1024
            precision=${mem_precision:-2}
            printf -v mem_used "%'.*f" \
                        $precision \
                        $(($mem_used / $divider)).$(($mem_used % $divider))
            printf -v mem_total "%'.*f" \
                        $precision \
                        $(($mem_total / $divider)).$(($mem_total % $divider))
        ;;
    esac

Keep in mind though for this functionality it would be much better when the orignal mem_xxx values are in bytes instead of being transformed into MB before this code...

@konimex
Copy link
Contributor

konimex commented Jan 22, 2019

Neofetch almost never calls printf directly. It'll be taken care of in user's config (the print_info() part).

For instance, neofetch (from user config) will fetch the memory variable here

@TriMoon
Copy link

TriMoon commented Jan 22, 2019

printf in the way i use is not outputting anything to stdout/stderr, the printf -v varname in bash is the equivalent of sprintf in C/C++
Eg. The output is put in a variable, whose value can be used later by print_info()

@dylanaraps
Copy link
Owner

This has been implemented.

@dylanaraps
Copy link
Owner

0435dcd

@ShockedCoder
Copy link

In bash, our normal math operations don't really allow decimals so it'd require some hack (e.g. $((3433/100)) and add a . manually (and IIRC neofetch used to do this, forgot when).

Maybe we can use both KiB, MiB, and GiB, but bits, bytes, and even TiB seems pointless unless there are someone out there that uses at least 1024GiB RAM.

Someone ordered a 4TB computer from linustechtips, don't remember the video tho

@autoteelar
Copy link

is there a way to make it show GB instead of GiB?

@ShockedCoder
Copy link

is there a way to make it show GB instead of GiB?

Replace 1024 to 1000 in the code where needed and rebuild neofetch

TriMoon added a commit to TriMoon/neofetch that referenced this issue Oct 31, 2022
Improving dylanaraps@0435dcd
- Implementing dylanaraps#1170 (comment) to avoid awk usage.
- Adding ability to configure precision of output using `mem_precision` which defaults to `2`.
- Added `tib` to accommodate TiB mentioned in dylanaraps#1170 (comment)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants