This nifty little addition allows users to create their own segments for displaying information in the terminal. And let me tell ya, , it’s about time.
For years now, we’ve been stuck with the same old boring segments: current directory, git branch, and a few others that are equally uninspiring. But thanks to Powerlevel10k’s custom segment feature, you can finally add some personality to your terminal game.
Here’s how it works: first, open up your .zshrc file (or whatever shell you prefer) and navigate to the Powerlevel10k section. Then, uncomment the following line:
# Enable custom segments
# This line enables the use of custom segments in Powerlevel10k.
# It evaluates the "add-segment" function and allows for customization of the terminal appearance.
# Uncomment the following line to enable custom segments
eval "$(powerlevel10k add-segment)"
# This line evaluates the "add-segment" function and enables the use of custom segments in Powerlevel10k.
# Uncommenting this line will activate the custom segments feature.
Once that’s done, you can start creating your own segments using a simple syntax:
# Define a new segment called 'weather'
function weather() {
# Use curl to fetch the current weather data from an API
local response=$(curl -s "https://api.openweathermap.org/data/2.5/weather?q=San%20Francisco&appid=YOUR_API_KEY") # Uses curl command to retrieve weather data from OpenWeatherMap API and stores it in a variable called "response"
# Extract the temperature and description from the JSON data
local temp=$(echo "$response" | jq -r '.main.temp') # Uses jq command to extract the temperature from the JSON data and stores it in a variable called "temp"
local desc=$(echo "$response" | jq -r '.weather[0].description') # Uses jq command to extract the weather description from the JSON data and stores it in a variable called "desc"
# Print the results to stdout in a human-readable format
echo "Weather: $desc ($temp°F)" # Prints the weather description and temperature in a human-readable format
}
In this example, we’re using curl and jq to fetch weather data from an API. The output is then formatted into a string that can be displayed as a segment in Powerlevel10k.
To add the ‘weather’ segment to your terminal setup, simply call it within the custom segments block:
# Add the 'weather' segment to the list of available segments
# This line adds a new segment called 'weather' to the list of available segments
add-segment weather
# This line calls the add-segment function and passes in the name of the new segment, 'weather'
And that’s it! Your new ‘weather’ segment will now be displayed in Powerlevel10k. You can create as many custom segments as you like, and even share them with others using GitHub or other platforms.
So go ahead, unleash your creativity and make your terminal game truly unique. With Powerlevel10k Custom Segments, the possibilities are endless!