Skip to content

Commit 2bf74d3

Browse files
fix(theming): readjusting the starship config file for a more reliable script
1 parent e8418c3 commit 2bf74d3

1 file changed

Lines changed: 61 additions & 45 deletions

File tree

Scripts/bash/template-apply.sh

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -535,57 +535,73 @@ zathura)
535535
;;
536536

537537
starship)
538-
# Check if the nested starship config exists first
539-
if [ -f "$HOME/.config/starship/starship.toml" ]; then
540-
CONFIG_FILE="$HOME/.config/starship/starship.toml"
541-
else
542-
# Fallback to the default path
543-
CONFIG_FILE="$HOME/.config/starship.toml"
544-
fi
538+
PALETTE_FILE="$HOME/.cache/noctalia/starship-palette.toml"
539+
540+
# Respect STARSHIP_CONFIG env var, then fall back to standard lookup order
541+
if [ -n "$STARSHIP_CONFIG" ]; then
542+
CONFIG_FILE="$STARSHIP_CONFIG"
543+
elif [ -f "$HOME/.config/starship.toml" ]; then
544+
CONFIG_FILE="$HOME/.config/starship.toml"
545+
elif [ -f "$HOME/.config/starship/starship.toml" ]; then
546+
CONFIG_FILE="$HOME/.config/starship/starship.toml"
547+
else
548+
CONFIG_FILE="$HOME/.config/starship.toml"
549+
fi
545550

546-
# Check if the generated palette file exists
547-
if [ ! -f "$PALETTE_FILE" ]; then
548-
echo "Error: Starship palette file not found at $PALETTE_FILE" >&2
549-
exit 1
550-
fi
551+
if [ ! -f "$PALETTE_FILE" ]; then
552+
echo "Error: Starship palette file not found at $PALETTE_FILE" >&2
553+
return 1
554+
fi
551555

552-
MARKER_BEGIN="# >>> NOCTALIA STARSHIP PALETTE >>>"
553-
MARKER_END="# <<< NOCTALIA STARSHIP PALETTE <<<"
556+
MARKER_BEGIN='# >>> NOCTALIA STARSHIP PALETTE >>>'
557+
MARKER_END='# <<< NOCTALIA STARSHIP PALETTE <<<'
558+
559+
# Create config file from scratch if it doesn't exist yet
560+
if [ ! -f "$CONFIG_FILE" ]; then
561+
mkdir -p "$(dirname "$CONFIG_FILE")"
562+
{
563+
printf 'palette = "noctalia"\n\n'
564+
printf '%s\n' "$MARKER_BEGIN"
565+
cat "$PALETTE_FILE"
566+
printf '%s\n' "$MARKER_END"
567+
} > "$CONFIG_FILE"
568+
return 0
569+
fi
554570

555-
# Create config file if it doesn't exist
556-
if [ ! -f "$CONFIG_FILE" ]; then
557-
mkdir -p "$(dirname "$CONFIG_FILE")"
558-
echo 'palette = "noctalia"' > "$CONFIG_FILE"
559-
echo "" >> "$CONFIG_FILE"
560-
echo "$MARKER_BEGIN" >> "$CONFIG_FILE"
561-
cat "$PALETTE_FILE" >> "$CONFIG_FILE"
562-
echo "$MARKER_END" >> "$CONFIG_FILE"
563-
exit 0
564-
fi
571+
# Follow symlinks so we edit the real file (safe for stow / dotfile managers)
572+
if [ -L "$CONFIG_FILE" ]; then
573+
CONFIG_FILE="$(readlink -f "$CONFIG_FILE")"
574+
fi
565575

566-
# 1. Set palette = "noctalia" at top level
567-
if grep -qE '^palette\s*=' "$CONFIG_FILE"; then
568-
sed -i -E 's/^palette\s*=.*/palette = "noctalia"/' "$CONFIG_FILE"
569-
else
570-
# Insert after schema line if present, otherwise at line 1
571-
if grep -qE '^\"\$schema\"' "$CONFIG_FILE"; then
572-
sed -i '/^\"\$schema\"/a palette = "noctalia"' "$CONFIG_FILE"
573-
else
574-
sed -i '1i palette = "noctalia"' "$CONFIG_FILE"
575-
fi
576-
fi
576+
# Set or insert top-level palette = "noctalia"
577+
if grep -qE '^[[:space:]]*palette[[:space:]]*=' "$CONFIG_FILE"; then
578+
sed -i -E 's/^([[:space:]]*)palette([[:space:]]*)=.*/\1palette\2= "noctalia"/' "$CONFIG_FILE"
579+
elif grep -qE '^[[:space:]]*"\$schema"' "$CONFIG_FILE"; then
580+
sed -i '/^[[:space:]]*"\$schema"/a palette = "noctalia"' "$CONFIG_FILE"
581+
else
582+
sed -i '1i palette = "noctalia"' "$CONFIG_FILE"
583+
fi
577584

578-
# 2. Remove existing noctalia palette block (between markers)
579-
if grep -qF "$MARKER_BEGIN" "$CONFIG_FILE"; then
580-
sed -i "/$(echo "$MARKER_BEGIN" | sed 's/[[\.*^$()+?{|]/\\&/g')/,/$(echo "$MARKER_END" | sed 's/[[\.*^$()+?{|]/\\&/g')/d" "$CONFIG_FILE"
581-
fi
585+
# Remove existing palette block using awk for literal string matching
586+
# (avoids sed misinterpreting >, #, or other chars in the markers as regex)
587+
if grep -qF "$MARKER_BEGIN" "$CONFIG_FILE"; then
588+
awk -v begin="$MARKER_BEGIN" -v end="$MARKER_END" '
589+
$0 == begin { skip = 1; next }
590+
$0 == end { skip = 0; next }
591+
!skip
592+
' "$CONFIG_FILE" > "${CONFIG_FILE}.noctalia.tmp" \
593+
&& mv "${CONFIG_FILE}.noctalia.tmp" "$CONFIG_FILE"
594+
fi
582595

583-
# 3. Append the new palette block
584-
echo "" >> "$CONFIG_FILE"
585-
echo "$MARKER_BEGIN" >> "$CONFIG_FILE"
586-
cat "$PALETTE_FILE" >> "$CONFIG_FILE"
587-
echo "$MARKER_END" >> "$CONFIG_FILE"
588-
;;
596+
# Append fresh palette block, ensuring a clean newline boundary
597+
{
598+
printf '\n%s\n' "$MARKER_BEGIN"
599+
cat "$PALETTE_FILE"
600+
# Guard: ensure palette file ends with newline before closing marker
601+
tail -c1 "$PALETTE_FILE" | grep -q $'\n' || printf '\n'
602+
printf '%s\n' "$MARKER_END"
603+
} >> "$CONFIG_FILE"
604+
;;
589605

590606
*)
591607
# Handle unknown application names.

0 commit comments

Comments
 (0)