Neofetch logo brand

Add Public IP Address Caching To Neofetch

  • Adam Douglas

In my blog post Add Weather Data With Caching To Neofetch, I talked about how I added the ability to fetch weather data and cached the weather data to save on the unnecessary use of bandwidth and to improve Neofetch’s load time performance. Let’s next apply the caching ability to getting your public IP address. In most cases one’s public IP address doesn’t change daily let alone weekly. So in most cases I would say it’s safe to say we can cache the public IP address. However the difference in this case compared to before is Neofetch already has the ability to fetch the public IP address it just doesn’t have the ability to cache it. With that said I will approach this by extending or indirectly calling the built-in function called get_public_ip() in order to add the ability of caching.

Environment

Tested with the following…

  • Arch Linux x86_64
  • Fish v3.3.1
  • GNU bash v5.1.8
  • Neofetch v7.1.0

Fetching Public IP Address

In the default configuration you would enable fetching the public IP address as follows.

info "Public IP" public_ip

This needs to be changed to the following.

getPublicIP

Script

The following script adds the ability to fetch and cache the public IP address. The public IP address will only be fetched if the set duration interval 604800 seconds (7 days) has passed.

The settings can be easily be set by alternating 2 variables.

data_path
The output public IP data directory and filename.
Variable value should be enclosed in double quotes.
duration
Interval to fetch weather data.
Represents a measurement in seconds (integer).
Do not enclose the variable value in quotes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
getPublicIP() {
    data_path="$HOME/.config/neofetch/data-public-ip.txt"
    duration=604800

    if [[ -e "$data_path" ]]; then
        public_ip_date="$(stat -c '%Y' $data_path)"
        current_date="$(date +%s)"
        date_diff=$(($current_date - $public_ip_date))

        if [ $date_diff -ge $duration ]; then
            get_public_ip
            savePublicIP
        else
            savePublicIP
        fi
    else
        get_public_ip
        savePublicIP
    fi

    showPublicIP
}
savePublicIP() {
    if [[ "${#public_ip}" -gt 0 ]]; then
        echo "$public_ip" > "$data_path"
    else
        public_ip="$(cat $data_path)"
    fi
}
showPublicIP() {
   prin "Public IP" "$public_ip"
}

Example Output

Neofetch IP address caching screenshot

Neofetch Config File

Here the complete setup done within the Neofetch configuration file “~/.config/neofetch/config.conf”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
print_info() {
    info title
    info underline
    info "OS" distro
    info "Host" model
    info "Kernel" kernel
    info "Uptime" uptime
    info "Packages" packages
    info "Shell" shell
    info "Resolution" resolution
    info "DE" de
    info "WM" wm
    info "WM Theme" wm_theme
    info "Theme" theme
    info "Icons" icons
    info "Terminal" term
    info "Terminal Font" term_font
    info "CPU" cpu
    info "GPU" gpu
    info "Memory" memory
    info "GPU Driver" gpu_driver  # Linux/macOS only
    info "CPU Usage" cpu_usage
    info "Disk" disk
    info "Local IP" local_ip
    getPublicIP
    info "Locale" locale  # This only works on glibc systems.
    info cols
}
getPublicIP() {
    data_path="$HOME/.config/neofetch/data-public-ip.txt"
    duration=604800

    if [[ -e "$data_path" ]]; then
        public_ip_date="$(stat -c '%Y' $data_path)"
        current_date="$(date +%s)"
        date_diff=$(($current_date - $public_ip_date))

        if [ $date_diff -ge $duration ]; then
            get_public_ip
            savePublicIP
        else
            savePublicIP
        fi
    else
        get_public_ip
        savePublicIP
    fi

    showPublicIP
}
savePublicIP() {
    if [[ "${#public_ip}" -gt 0 ]]; then
        echo "$public_ip" > "$data_path"
    else
        public_ip="$(cat $data_path)"
    fi
}
showPublicIP() {
   prin "Public IP" "$public_ip"
}

title_fqdn="off"
kernel_shorthand="on"
distro_shorthand="off"
os_arch="on"
uptime_shorthand="on"
memory_percent="on"
memory_unit="gib"
package_managers="on"
shell_path="off"
shell_version="on"
speed_type="bios_limit"
speed_shorthand="off"
cpu_brand="on"
cpu_speed="on"
cpu_cores="logical"
cpu_temp="off"
gpu_brand="on"
gpu_type="all"
refresh_rate="on"
gtk_shorthand="off"
gtk2="on"
gtk3="on"
public_ip_host="https://ident.me"
public_ip_timeout=2
de_version="on"
disk_show=('/' '/boot' '/home' '/srv' '/mnt/data')
disk_subtitle="mount"
disk_percent="on"
music_player="auto"
song_format="%artist% - %album% - %title%"
song_shorthand="off"
mpc_args=()
colors=(distro)
bold="on"
underline_enabled="on"
underline_char="-"
separator=":"
block_range=(0 15)
color_blocks="on"
block_width=3
block_height=1
col_offset="auto"
bar_char_elapsed="-"
bar_char_total="="
bar_border="on"
bar_length=15
bar_color_elapsed="distro"
bar_color_total="distro"
cpu_display="off"
memory_display="off"
battery_display="off"
disk_display="off"
image_backend="kitty"
image_source="ascii"
ascii_distro="auto"
ascii_colors=(distro)
ascii_bold="on"
image_loop="off"
thumbnail_dir="${XDG_CACHE_HOME:-${HOME}/.cache}/thumbnails/neofetch"
crop_mode="normal"
crop_offset="center"
image_size="auto"
gap=3
yoffset=0
xoffset=0
background_color=
stdout="off"

Tip for BASH

Call Neofetch each time a terminal/console is started by adding the command "neofetch" without double quotes to "~/.bashrc".

Tip for FiSH

Call Neofetch each time a terminal/console is started by adding the command "neofetch" without double quotes to "~/.config/fish/config.fish".

I’m publishing this as part of 100 Days To Offload. You can join in yourself by visiting 100DaysToOffload.com.

    • Change "in" to "to" in the url
    • Add old link redirection
    • Fix broken link, Add Weather Data With Caching To Neofetch
    • Fix broken link
    • change topic
    • remove tag cache
    • change topic