A variety of simple automation scripts and enhancements to Sway and i3.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

756 lines
27KB

  1. #!/usr/bin/env bash
  2. # Single theme copy of apply-colors.sh from: https://raw.githubusercontent.com/Mayccoll/Gogh
  3. # ====================CONFIG THIS =============================== #
  4. export COLOR_01="#282828" # HOST
  5. export COLOR_02="#cc241d" # SYNTAX_STRING
  6. export COLOR_03="#98971a" # SYNTAX_STRING
  7. export COLOR_04="#d79921" # COMMAND
  8. export COLOR_05="#458588" # COMMAND_COLOR2
  9. export COLOR_06="#b16286" # PATH
  10. export COLOR_07="#68986a" # SYNTAX_VAR
  11. export COLOR_08="#a89984" # PROMP
  12. export COLOR_09="#928374" #
  13. export COLOR_10="#fb4934" #
  14. export COLOR_11="#b8bb26" # COMMAND_ERROR
  15. export COLOR_12="#fabd2f" # EXEC
  16. export COLOR_13="#83a598" #
  17. export COLOR_14="#d3869b" # FOLDER
  18. export COLOR_15="#8ec07c" #
  19. export COLOR_16="#ebdbb2" #
  20. export BACKGROUND_COLOR="#1d2021" # Background Color
  21. export FOREGROUND_COLOR="#ebdbb2" # Text
  22. export CURSOR_COLOR="$FOREGROUND_COLOR" # Cursor
  23. export PROFILE_NAME="Gruvbox"
  24. # | ===========================================
  25. # | Early pre-requisites check
  26. # | ===========================================
  27. UUIDGEN="${UUIDGEN:-$(command -v uuidgen | xargs echo)}"
  28. DCONF="${DCONF:-$(command -v dconf | xargs echo)}"
  29. GCONF="${GCONF:-$(command -v gconftool-2 | xargs echo)}"
  30. GS="${GS:-$(command -v gsettings | xargs echo)}"
  31. # Note: xargs echo is to make the command sucessful even if it was not
  32. # otherwise the script will exit if the command does not exist (elementary os)
  33. # | ===========================================
  34. # | Make sure all exported variables get unset no matter what
  35. # | Defining this in this script because it gets called even if
  36. # | gogh.sh was not called. Exported variables in gogh.sh gets
  37. # | handled there in case there was en error before this script was called
  38. # | ============================================
  39. GLOBAL_VAR_CLEANUP() {
  40. unset PROFILE_NAME
  41. unset PROFILE_SLUG
  42. unset scratchdir
  43. unset TILIX_RES
  44. unset TERMINAL
  45. unset LOOP
  46. unset OPTLENGTH
  47. for c in $(seq -s " " -w 16); do
  48. unset DEMO_COLOR_${c}
  49. unset COLOR_${c}
  50. done
  51. unset BACKGROUND_COLOR
  52. unset FOREGROUND_COLOR
  53. unset CURSOR_COLOR
  54. unset HIGHLIGHT_FG_COLOR
  55. unset HIGHLIGHT_BG_COLOR
  56. unset USE_SYS_TRANSPARENCY
  57. unset PROFILE_NAME
  58. }
  59. # Note: Since all scripts gets invoked in a subshell the traps from the parent shell
  60. # will not get inherited. Hence traps defined in gogh.sh and print-themes.sh will still trigger
  61. trap 'GLOBAL_VAR_CLEANUP; trap - EXIT' EXIT HUP INT QUIT PIPE TERM
  62. # | ===========================================
  63. # | Second test for TERMINAL in case user ran
  64. # | theme script directly instead of gogh.sh
  65. # | ============================================
  66. if [[ -z "${TERMINAL:-}" ]]; then
  67. # | ===========================================
  68. # | Check for the terminal name (depening on os)
  69. # | ===========================================
  70. OS="$(uname)"
  71. if [[ "$OS" = "Darwin" ]]; then
  72. TERMINAL=$TERM_PROGRAM
  73. elif [[ "${OS#CYGWIN}" != "${OS}" ]]; then
  74. TERMINAL="mintty"
  75. else
  76. # | ===========================================
  77. # | Depending on how the script was invoked, we need
  78. # | to loop until pid is no longer a subshell
  79. # | ===========================================
  80. pid="$$"
  81. TERMINAL="$(ps -h -o comm -p $pid)"
  82. while [[ "${TERMINAL:(-2)}" == "sh" ]]; do
  83. pid="$(ps -h -o ppid -p $pid)"
  84. TERMINAL="$(ps -h -o comm -p $pid)"
  85. done
  86. fi
  87. fi
  88. case "${TERMINAL}" in
  89. pantheon-terminal|io.elementary.t* )
  90. if [[ -z "${GS}" ]]; then
  91. printf '\n%s\n' "Error gsettings not found"
  92. printf '%s\n' "sudo apt install dconf?"
  93. printf '%s\n\n' "or export GS=/path/to/gsettings"
  94. exit 1
  95. fi
  96. ;;
  97. mintty )
  98. CFGFILE="${HOME}/.minttyrc"
  99. if [[ ! -f "${CFGFILE}" ]]; then
  100. printf '\n%s\n' "Warning: Couldn't find an existing configuration file, so one will be created for you."
  101. printf '%s\n\n' "Warning: Are you really running Cygwin's mintty?"
  102. touch "${CFGFILE}"
  103. fi
  104. ;;
  105. guake|tilix|mate-terminal|gnome-terminal* )
  106. case "${TERMINAL}" in
  107. guake|gnome-terminal* )
  108. if [[ -z "${DCONF}" ]] && [[ -z "${GCONF}" ]]; then
  109. printf '\n%s\n' "Error gconftool not found!"
  110. printf '%s\n' "sudo apt install gconftool?"
  111. printf '%s\n\n' "or export GCONF=/path/to/gconftool-2/"
  112. exit 1
  113. fi
  114. ;;
  115. esac
  116. if [[ -z "${DCONF}" ]]; then
  117. printf '\n%s\n' "Error dconf not found"
  118. printf '%s\n' "sudo apt install dconf?"
  119. printf '%s\n\n' "or export DCONF=/path/to/dconf"
  120. exit 1
  121. fi
  122. ;;
  123. esac
  124. # | ===========================================
  125. # | Convert RGB to gnome colors
  126. # | ===========================================
  127. gnome_color () {
  128. AA=${1:1:2}
  129. BB=${1:3:2}
  130. CC=${1:5:2}
  131. echo "#${AA}${AA}${BB}${BB}${CC}${CC}"
  132. }
  133. hexToDec () {
  134. echo "$((16#${1}))"
  135. }
  136. hexRGBtoDecRGB () {
  137. R="$(hexToDec "${1:1:2}")"
  138. G="$(hexToDec "${1:3:2}")"
  139. B="$(hexToDec "${1:5:2}")"
  140. echo "${R}" "${G}" "${B}"
  141. }
  142. convertRGBtoMac () {
  143. local color="${1}"
  144. set --
  145. set -- $(hexRGBtoDecRGB "${color}")
  146. R=${1}; shift; G=${1}; shift; B=${1}; shift
  147. R=$(echo "${R} / 255" | bc -l)
  148. G=$(echo "${G} / 255" | bc -l)
  149. B=$(echo "${B} / 255" | bc -l)
  150. echo "${R}" "${G}" "${B}"
  151. }
  152. createMinttyEntry () {
  153. local name="${1}"
  154. local color="${2}"
  155. set --
  156. set -- $(hexRGBtoDecRGB "${color}")
  157. R=${1}; shift; G=${1}; shift; B=${1}; shift
  158. echo "${name}=${R},${G},${B}"
  159. }
  160. updateMinttyConfig () {
  161. local config="${1}"
  162. local color="${2}"
  163. local name="${3}"
  164. sed -i -r -e "s/^${name}=.+/$(createMinttyEntry "${name}" "${color}")/g" "${config}"
  165. }
  166. convertNameAndRGBtoITerm() {
  167. local name="${1}"
  168. local color="${2}"
  169. set --
  170. set -- $(convertRGBtoMac "${color}")
  171. R=${1}; shift; G=${1}; shift; B=${1}; shift
  172. echo "<key>${name}</key><dict><key>Blue Component</key><real>${B}</real><key>Green Component</key><real>${G}</real><key>Red Component</key><real>${R}</real></dict>"
  173. }
  174. dset() {
  175. local key="${1}"; shift
  176. local val="${1}"
  177. "${DCONF}" write "${PROFILE_KEY}/${key}" "${val}"
  178. }
  179. # Because dconf still doesn't have "append"
  180. dlist_append() {
  181. local key="${1}"; shift
  182. local val="${1}"; shift
  183. local entries
  184. entries="$(
  185. {
  186. "${DCONF}" read "${key}" | tr -d "[]" | tr , "\n" | grep -F -v "${val}"
  187. echo "'${val}'"
  188. } | head -c-1 | tr "\n" ,
  189. )"
  190. "${DCONF}" write "${key}" "[${entries}]"
  191. }
  192. gcset() {
  193. local type="${1}"; shift
  194. local key="${1}"; shift
  195. local val="${1}"
  196. "${GCONF}" --set --type "${type}" "${PROFILE_KEY}/${key}" -- "${val}"
  197. }
  198. # Because gconftool doesn't have "append"
  199. glist_append() {
  200. local type="${1}"; shift
  201. local key="${1}"; shift
  202. local val="${1}"; shift
  203. local entries
  204. entries="$(
  205. {
  206. "${GCONF}" --get "${key}" | tr -d "[]" | tr , "\n" | grep -F -v "${val}"
  207. echo "${val}"
  208. } | head -c-1 | tr "\n" ,
  209. )"
  210. "${GCONF}" --set --type list --list-type "${type}" "${key}" "[${entries}]"
  211. }
  212. gset() {
  213. local key="${1}"; shift
  214. local val="${1}"
  215. "${GS}" set "${PROFILE_KEY}" "${key}" "${val}"
  216. }
  217. set_theme() {
  218. dset visible-name "'${PROFILE_NAME}'"
  219. dset background-color "'${BACKGROUND_COLOR}'"
  220. dset foreground-color "'${FOREGROUND_COLOR}'"
  221. if [[ -n "${HIGHLIGHT_BG_COLOR:-}" ]]; then
  222. dset highlight-colors-set "true"
  223. dset highlight-background-color "'${HIGHLIGHT_BG_COLOR}'"
  224. if [[ -n "${HIGHLIGHT_FG_COLOR:-}" ]]; then
  225. dset highlight-foreground-color "'${HIGHLIGHT_FG_COLOR}'"
  226. fi
  227. fi
  228. if [[ -n "${BOLD_COLOR:-}" ]]; then
  229. dset bold-color "'${BOLD_COLOR}'"
  230. dset bold-color-same-as-fg "false"
  231. else
  232. dset bold-color "'${FOREGROUND_COLOR}'"
  233. dset bold-color-same-as-fg "true"
  234. fi
  235. dset use-theme-colors "false"
  236. dset use-theme-background "false"
  237. dset use-theme-transparency "${USE_SYS_TRANSPARENCY:-false}"
  238. }
  239. legacy_set_theme() {
  240. gcset string visible_name "${PROFILE_NAME}"
  241. gcset string background_color "${BACKGROUND_COLOR}"
  242. gcset string foreground_color "${FOREGROUND_COLOR}"
  243. if [[ -n "${BOLD_COLOR:-}" ]]; then
  244. gcset string bold_color "${BOLD_COLOR}"
  245. gcset bool bold_color_same_as_fg "false"
  246. else
  247. gcset string bold_color "${FOREGROUND_COLOR}"
  248. gcset bool bold_color_same_as_fg "true"
  249. fi
  250. gcset bool use_theme_colors "false"
  251. gcset bool use_theme_background "false"
  252. }
  253. # | ===========================================
  254. # | If terminal supports truecolor then we can show theme colors without applying the theme
  255. # | ===========================================
  256. if [[ "${COLORTERM:-}" == "truecolor" ]] || [[ "${COLORTERM:-}" == "24bit" ]]; then
  257. # gogh_colors have been moved here to avoid multiple definitions
  258. function gogh_colors () {
  259. # Build up the color string to avoid visual rendering
  260. local color_str
  261. # Note: {01..16} does not work on OSX
  262. for c in $(seq -s " " -w 16); do
  263. local color="COLOR_$c"
  264. set -- $(hexRGBtoDecRGB "${!color}")
  265. color_str+="\033[38;2;${1};${2};${3}m█████$(tput sgr0)"
  266. [[ ${GOGH_DRY_RUN:-0} -eq 1 ]] && export "DEMO_COLOR_$c=\033[38;2;${1};${2};${3}m"
  267. [[ "$c" == "08" ]] && color_str+="\n" # new line
  268. done
  269. printf '\n%b\n\n\n' "${color_str}"
  270. unset color_str
  271. }
  272. else
  273. function gogh_colors () {
  274. # Build up the color string to avoid visual rendering
  275. local color_str
  276. for c in {0..15}; do
  277. color_str+="$(tput setaf $c)█████$(tput sgr0)"
  278. [[ $c == 7 ]] && color_str+="\n" # new line
  279. done
  280. printf '\n%b\n\n' "${color_str}"
  281. unset color_str
  282. }
  283. fi
  284. # | ===========================================
  285. # | Print theme colors
  286. # | ===========================================
  287. gogh_colors
  288. if [[ ${GOGH_DRY_RUN:-0} -eq 1 ]]; then
  289. color
  290. # End here if dry run was initiated
  291. exit 0
  292. fi
  293. apply_elementary() {
  294. # | ===========================================
  295. # | Applying values on elementary/pantheon terminal
  296. # | ===========================================
  297. gset background "${BACKGROUND_COLOR}"
  298. gset foreground "${FOREGROUND_COLOR}"
  299. gset cursor-color "${CURSOR_COLOR}"
  300. gset palette "${COLOR_01}:${COLOR_02}:${COLOR_03}:${COLOR_04}:${COLOR_05}:${COLOR_06}:${COLOR_07}:${COLOR_08}:${COLOR_09}:${COLOR_10}:${COLOR_11}:${COLOR_12}:${COLOR_13}:${COLOR_14}:${COLOR_15}:${COLOR_16}"
  301. }
  302. apply_cygwin() {
  303. # | ===========================================
  304. # | Applying values on mintty (cygwin)
  305. # | ===========================================
  306. echo "Patching mintty configuration file (${CFGFILE}) with new colors..."
  307. updateMinttyConfig "$CFGFILE" "$COLOR_01" "Black"
  308. updateMinttyConfig "$CFGFILE" "$COLOR_02" "Red"
  309. updateMinttyConfig "$CFGFILE" "$COLOR_03" "Green"
  310. updateMinttyConfig "$CFGFILE" "$COLOR_04" "Yellow"
  311. updateMinttyConfig "$CFGFILE" "$COLOR_05" "Blue"
  312. updateMinttyConfig "$CFGFILE" "$COLOR_06" "Magenta"
  313. updateMinttyConfig "$CFGFILE" "$COLOR_07" "Cyan"
  314. updateMinttyConfig "$CFGFILE" "$COLOR_08" "White"
  315. updateMinttyConfig "$CFGFILE" "$COLOR_09" "BoldBlack"
  316. updateMinttyConfig "$CFGFILE" "$COLOR_10" "BoldRed"
  317. updateMinttyConfig "$CFGFILE" "$COLOR_11" "BoldGreen"
  318. updateMinttyConfig "$CFGFILE" "$COLOR_12" "BoldYellow"
  319. updateMinttyConfig "$CFGFILE" "$COLOR_13" "BoldBlue"
  320. updateMinttyConfig "$CFGFILE" "$COLOR_14" "BoldMagenta"
  321. updateMinttyConfig "$CFGFILE" "$COLOR_15" "BoldCyan"
  322. updateMinttyConfig "$CFGFILE" "$COLOR_16" "BoldWhite"
  323. updateMinttyConfig "$CFGFILE" "$BACKGROUND_COLOR" "Backgroundcolor"
  324. updateMinttyConfig "$CFGFILE" "$FOREGROUND_COLOR" "Foregroundcolor"
  325. updateMinttyConfig "$CFGFILE" "$CURSOR_COLOR" "Cursorcolor"
  326. echo "Done - please reopen your Cygwin terminal to see the changes"
  327. }
  328. apply_darwin() {
  329. # | ===========================================
  330. # | Applying values on iTerm2
  331. # | ===========================================
  332. BACKGROUND_COLOR=$(convertNameAndRGBtoITerm "Background Color" "$BACKGROUND_COLOR")
  333. FOREGROUND_COLOR=$(convertNameAndRGBtoITerm "Foreground Color" "$FOREGROUND_COLOR")
  334. COLOR_01=$(convertNameAndRGBtoITerm "Ansi 0 Color" "$COLOR_01")
  335. COLOR_02=$(convertNameAndRGBtoITerm "Ansi 1 Color" "$COLOR_02")
  336. COLOR_03=$(convertNameAndRGBtoITerm "Ansi 2 Color" "$COLOR_03")
  337. COLOR_04=$(convertNameAndRGBtoITerm "Ansi 3 Color" "$COLOR_04")
  338. COLOR_05=$(convertNameAndRGBtoITerm "Ansi 4 Color" "$COLOR_05")
  339. COLOR_06=$(convertNameAndRGBtoITerm "Ansi 5 Color" "$COLOR_06")
  340. COLOR_07=$(convertNameAndRGBtoITerm "Ansi 6 Color" "$COLOR_07")
  341. COLOR_08=$(convertNameAndRGBtoITerm "Ansi 7 Color" "$COLOR_08")
  342. COLOR_09=$(convertNameAndRGBtoITerm "Ansi 8 Color" "$COLOR_09")
  343. COLOR_10=$(convertNameAndRGBtoITerm "Ansi 9 Color" "$COLOR_10")
  344. COLOR_11=$(convertNameAndRGBtoITerm "Ansi 10 Color" "$COLOR_11")
  345. COLOR_12=$(convertNameAndRGBtoITerm "Ansi 11 Color" "$COLOR_12")
  346. COLOR_13=$(convertNameAndRGBtoITerm "Ansi 12 Color" "$COLOR_13")
  347. COLOR_14=$(convertNameAndRGBtoITerm "Ansi 13 Color" "$COLOR_14")
  348. COLOR_15=$(convertNameAndRGBtoITerm "Ansi 14 Color" "$COLOR_15")
  349. COLOR_16=$(convertNameAndRGBtoITerm "Ansi 15 Color" "$COLOR_16")
  350. # Assemble color scheme file contents
  351. ITERMCOLORS='<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict>'${BACKGROUND_COLOR}${FOREGROUND_COLOR}${COLOR_01}${COLOR_02}${COLOR_03}${COLOR_04}${COLOR_05}${COLOR_06}${COLOR_07}${COLOR_08}${COLOR_09}${COLOR_10}${COLOR_11}${COLOR_12}${COLOR_13}${COLOR_14}${COLOR_15}'</dict></plist>'
  352. # Dump iTerm color scheme to file and import it by opening it
  353. echo "${ITERMCOLORS}" > "${PROFILE_NAME}.itermcolors"
  354. open "${PROFILE_NAME}.itermcolors"
  355. rm "${PROFILE_NAME}.itermcolors"
  356. }
  357. apply_gtk() {
  358. # | ===========================================
  359. # | Applying values to gnome/mate/tilix
  360. # | ===========================================
  361. local legacy="${1:-}"
  362. # This is to avoid doing the profile loop definition twice
  363. if [[ -z "${legacy}" ]]; then
  364. CONFTOOL="${DCONF} read"
  365. VISIBLE_NAME="visible-name"
  366. else
  367. CONFTOOL="${GCONF} --get"
  368. VISIBLE_NAME="visible_name"
  369. fi
  370. # Check first wether profile already exists
  371. profile_hashes=($(${CONFTOOL} "${PROFILE_LIST_KEY}" | tr "[]'," " "))
  372. for profile in "${profile_hashes[@]}"; do
  373. if [[ "$(${CONFTOOL} "${BASE_DIR}${profile}/${VISIBLE_NAME}" | tr -d "'")" == "${PROFILE_NAME}" ]]; then
  374. printf '%s\n' "Profile already exists" "Skipping..."
  375. exit 0
  376. fi
  377. done
  378. # Fallback if there is no default profile
  379. set -- $(${CONFTOOL} ${PROFILE_LIST_KEY} | tr "[]'," " ")
  380. : ${DEFAULT_SLUG:="$1"}
  381. : ${PROFILE_NAME:="Default"}
  382. : ${PROFILE_SLUG:="Default"}
  383. DEFAULT_KEY="${BASE_DIR}${DEFAULT_SLUG:-}"
  384. PROFILE_KEY="${BASE_DIR}${PROFILE_SLUG:-}"
  385. if [[ -z "${legacy}" ]]; then
  386. if [[ -z "$(${DCONF} list ${BASE_DIR%:})" ]]; then
  387. # Provide a user friendly error text if no saved profile exists, otherwise it will display "Error gconftool not found!"
  388. # it could happen on a newly installed system. (happened on CentOS 7)
  389. printf '%s\n' \
  390. "Error, no saved profiles found!" \
  391. "Possible fix, new a profile (Terminal > Edit > Preferences > Profiles > New, then Close) and try again." \
  392. "You can safely delete the created profile after the installation."
  393. exit 1
  394. fi
  395. BACKGROUND_COLOR=$(gnome_color "$BACKGROUND_COLOR")
  396. FOREGROUND_COLOR=$(gnome_color "$FOREGROUND_COLOR")
  397. HIGHLIGHT_BG_COLOR=$(gnome_color "$HIGHLIGHT_BG_COLOR")
  398. HIGHLIGHT_FG_COLOR=$(gnome_color "$HIGHLIGHT_FG_COLOR")
  399. COLOR_01=$(gnome_color "$COLOR_01")
  400. COLOR_02=$(gnome_color "$COLOR_02")
  401. COLOR_03=$(gnome_color "$COLOR_03")
  402. COLOR_04=$(gnome_color "$COLOR_04")
  403. COLOR_05=$(gnome_color "$COLOR_05")
  404. COLOR_06=$(gnome_color "$COLOR_06")
  405. COLOR_07=$(gnome_color "$COLOR_07")
  406. COLOR_08=$(gnome_color "$COLOR_08")
  407. COLOR_09=$(gnome_color "$COLOR_09")
  408. COLOR_10=$(gnome_color "$COLOR_10")
  409. COLOR_11=$(gnome_color "$COLOR_11")
  410. COLOR_12=$(gnome_color "$COLOR_12")
  411. COLOR_13=$(gnome_color "$COLOR_13")
  412. COLOR_14=$(gnome_color "$COLOR_14")
  413. COLOR_15=$(gnome_color "$COLOR_15")
  414. COLOR_16=$(gnome_color "$COLOR_16")
  415. # copy existing settings from default profile
  416. $DCONF dump "${DEFAULT_KEY}/" | $DCONF load "${PROFILE_KEY}/"
  417. # add new copy to global list of profiles
  418. dlist_append "${PROFILE_LIST_KEY}" "${PROFILE_SLUG#:}"
  419. set_theme
  420. dset palette "${LEFT_WRAPPER:-}'${COLOR_01}${PALETTE_DELIM}${COLOR_02}${PALETTE_DELIM}${COLOR_03}${PALETTE_DELIM}${COLOR_04}${PALETTE_DELIM}${COLOR_05}${PALETTE_DELIM}${COLOR_06}${PALETTE_DELIM}${COLOR_07}${PALETTE_DELIM}${COLOR_08}${PALETTE_DELIM}${COLOR_09}${PALETTE_DELIM}${COLOR_10}${PALETTE_DELIM}${COLOR_11}${PALETTE_DELIM}${COLOR_12}${PALETTE_DELIM}${COLOR_13}${PALETTE_DELIM}${COLOR_14}${PALETTE_DELIM}${COLOR_15}${PALETTE_DELIM}${COLOR_16}'${RIGHT_WRAPPER:-}"
  421. ${LEGACY_BOLD:-} && dset allow-bold "true" # mate
  422. else
  423. # Append the Base16 profile to the profile list
  424. glist_append string "${PROFILE_LIST_KEY}" "${PROFILE_SLUG}"
  425. legacy_set_theme
  426. gcset string palette "${COLOR_01}:${COLOR_02}:${COLOR_03}:${COLOR_04}:${COLOR_05}:${COLOR_06}:${COLOR_07}:${COLOR_08}:${COLOR_09}:${COLOR_10}:${COLOR_11}:${COLOR_12}:${COLOR_13}:${COLOR_14}:${COLOR_15}:${COLOR_16}"
  427. ${LEGACY_BOLD:-} && gcset bool allow-bold "true"
  428. fi
  429. }
  430. apply_guake() {
  431. # | ===========================================
  432. # | Applying values to guake
  433. # | ===========================================
  434. local legacy="${1:-}"
  435. PROFILE_KEY="/apps/guake/style/font"
  436. if [[ -z "${legacy}" ]]; then
  437. dset palette "'${COLOR_01}:${COLOR_02}:${COLOR_03}:${COLOR_04}:${COLOR_05}:${COLOR_06}:${COLOR_07}:${COLOR_08}:${COLOR_09}:${COLOR_10}:${COLOR_11}:${COLOR_12}:${COLOR_13}:${COLOR_14}:${COLOR_15}:${COLOR_16}:${FOREGROUND_COLOR}:${BACKGROUND_COLOR}'"
  438. dset palette-name "'${PROFILE_NAME}'"
  439. dset allow-bold 'true'
  440. else
  441. gcset string color "${FOREGROUND_COLOR}"
  442. gcset string palette "${COLOR_01}:${COLOR_02}:${COLOR_03}:${COLOR_04}:${COLOR_05}:${COLOR_06}:${COLOR_07}:${COLOR_08}:${COLOR_09}:${COLOR_10}:${COLOR_11}:${COLOR_12}:${COLOR_13}:${COLOR_14}:${COLOR_15}:${COLOR_16}"
  443. gcset string palette-name "${PROFILE_NAME}"
  444. PROFILE_KEY="/apps/guake/style/background"
  445. gcset string color "${BACKGROUND_COLOR}"
  446. fi
  447. }
  448. appy_tilixschemes() {
  449. # | ===========================================
  450. # | Applying values to tilix colorschemes
  451. # | ===========================================
  452. if [[ ${TILIX_RES::1} =~ ^(y|Y)$ ]]; then
  453. [[ -d "${HOME}/.config/tilix/schemes" ]] || mkdir -p "${HOME}/.config/tilix/schemes"
  454. TILIXCOLORS='{\n\t"name": "'${PROFILE_NAME}'",\n\t"comment": "Generated by Gogh",\n\t"foreground-color": "'${FOREGROUND_COLOR}'",\n\t"background-color":"'${BACKGROUND_COLOR}'",\n\t"cursor-background-color": "'${CURSOR_COLOR}'",\n\t"palette": [\n\t\t"'${COLOR_01}'",\n\t\t"'${COLOR_02}'",\n\t\t"'${COLOR_03}'",\n\t\t"'${COLOR_04}'",\n\t\t"'${COLOR_05}'",\n\t\t"'${COLOR_06}'",\n\t\t"'${COLOR_07}'",\n\t\t"'${COLOR_08}'",\n\t\t"'${COLOR_09}'",\n\t\t"'${COLOR_10}'",\n\t\t"'${COLOR_11}'",\n\t\t"'${COLOR_12}'",\n\t\t"'${COLOR_13}'",\n\t\t"'${COLOR_14}'",\n\t\t"'${COLOR_15}'",\n\t\t"'${COLOR_16}'"\n\t],\n\t"use-badge-color": false,\n\t"use-bold-color": false,\n\t"use-cursor-color": false,\n\t"use-highlight-color": false,\n\t"use-theme-colors": false\n}'
  455. echo -e "${TILIXCOLORS}" > "${scratchdir}/${PROFILE_NAME}.json"
  456. # Note: Tilix does not store color scheme name in dconf
  457. # so we have to update color palette for the current profile in order to switch to the new theme
  458. # but only set the palette on the last loop to avoid a flashing terminal
  459. if ((LOOP == OPTLENGTH)); then
  460. cp -f ${scratchdir}/* "$HOME/.config/tilix/schemes/"
  461. rm -rf "${scratchdir}"
  462. read -r -p "All done - apply new theme? [y/N] " -n 1 TILIX_RES
  463. if [[ ${TILIX_RES::1} =~ ^(y|Y)$ ]]; then
  464. PROFILE_KEY="${BASE_DIR}${DEFAULT_SLUG}"
  465. PROFILE_NAME="$(${DCONF} read ${PROFILE_KEY}/visible-name | tr -d \')"
  466. set_theme
  467. dset palette "['${COLOR_01}', '${COLOR_02}', '${COLOR_03}', '${COLOR_04}', '${COLOR_05}', '${COLOR_06}', '${COLOR_07}', '${COLOR_08}', '${COLOR_09}', '${COLOR_10}', '${COLOR_11}', '${COLOR_12}', '${COLOR_13}', '${COLOR_14}', '${COLOR_15}', '${COLOR_16}']"
  468. fi
  469. fi
  470. unset PROFILE_NAME
  471. unset PROFILE_SLUG
  472. unset TILIXCOLORS
  473. exit 0
  474. fi
  475. }
  476. apply_xfce4-terminal() {
  477. # XFCE4 terminal has no profiles, instead it uses color presets
  478. SCHEMEDIR="${HOME}/.local/share/xfce4/terminal/colorschemes"
  479. CONFFILE="${HOME}/.config/xfce4/terminal/terminalrc"
  480. if [[ ! (-w "${CONFFILE}") ]]; then
  481. if [[ -r "${XDG_CONFIG_DIRS%%:*}/Terminal/terminalrc" ]]; then
  482. cp "${XDG_CONFIG_DIRS%%:*}/Terminal/terminalrc" ${CONFFILE}
  483. else
  484. echo "ERROR: config file not present or not writable!"
  485. exit 1
  486. fi
  487. fi
  488. [[ -d "${SCHEMEDIR}" ]] || mkdir -p "${SCHEMEDIR}"
  489. F_NAME=${PROFILE_NAME// /-}
  490. F_NAME=$(echo ${F_NAME} | tr -d ":()")
  491. F_NAME=$(echo "${F_NAME}" | awk '{print tolower($0)}')
  492. FF_NAME="${SCHEMEDIR}/${F_NAME}.theme"
  493. touch "${FF_NAME}"
  494. L_COLORCURSOR="ColorCursor=${CURSOR_COLOR}"
  495. L_COLORPALETTE="ColorPalette=${COLOR_01};${COLOR_02};${COLOR_03};${COLOR_04};${COLOR_05};${COLOR_06};${COLOR_07};${COLOR_08};${COLOR_09};${COLOR_10};${COLOR_11};${COLOR_12};${COLOR_13};${COLOR_14};${COLOR_15};${COLOR_16}"
  496. printf '%s\n' \
  497. "; Generated by Gogh" \
  498. "; https://mayccoll.github.io/Gogh" \
  499. "[Scheme]" \
  500. "Name=${PROFILE_NAME}" \
  501. "ColorForeground=${FOREGROUND_COLOR}" \
  502. "ColorBackground=${BACKGROUND_COLOR}" \
  503. "${L_COLORCURSOR}" \
  504. "${L_COLORPALETTE}" \
  505. "ColorCursorUseDefault=FALSE" > ${FF_NAME}
  506. # apply last theme in queue
  507. # xfce4-terminal monitors its rc file and doesn't reference
  508. # any of the themes in there. The color settings need to
  509. # be written there directly.
  510. if ((LOOP == OPTLENGTH)); then
  511. read -r -p "All done - apply new theme? [y/N] " -n 1 XFCE4_APPLY_CURR_THEME
  512. if [[ ${XFCE4_APPLY_CURR_THEME::1} =~ ^(y|Y)$ ]]; then
  513. if grep -q "^ColorPalette=" "${CONFFILE}"; then
  514. sed -i -r -e "s/^ColorPalette=.*/${L_COLORPALETTE}/" "${CONFFILE}"
  515. else
  516. echo "${L_COLORPALETTE}" >> "${CONFFILE}"
  517. fi
  518. if grep -q "^ColorCursor=" "${CONFFILE}"; then
  519. sed -i -r -e "s/^ColorCursor=.*/${L_COLORCURSOR}/" "${CONFFILE}"
  520. else
  521. echo "${L_COLORCURSOR}" >> "${CONFFILE}"
  522. fi
  523. if grep -q "^ColorForeground=" "${CONFFILE}"; then
  524. sed -i -r -e "s/^ColorForeground=.*/ColorForeground=${FOREGROUND_COLOR}/" "${CONFFILE}"
  525. else
  526. echo "ColorForeground=${FOREGROUND_COLOR}" >> "${CONFFILE}"
  527. fi
  528. if grep -q "^ColorBackground=" "${CONFFILE}"; then
  529. sed -i -r -e "s/^ColorBackground=.*/ColorBackground=${BACKGROUND_COLOR}/" "${CONFFILE}"
  530. else
  531. echo "ColorBackground=${BACKGROUND_COLOR}" >> "${CONFFILE}"
  532. fi
  533. if grep -q "^ColorCursorUseDefault=FALSE" "${CONFFILE}"; then
  534. true
  535. else
  536. echo "ColorCursorUseDefault=FALSE" >> "${CONFFILE}"
  537. fi
  538. fi
  539. fi
  540. unset SCHEMEDIR
  541. unset CONFFILE
  542. unset PROFILE_NAME
  543. unset F_NAME
  544. unset FF_NAME
  545. unset L_COLORCURSOR
  546. unset L_COLORPALETTE
  547. exit 0
  548. }
  549. [[ -n "${UUIDGEN}" ]] && PROFILE_SLUG="$(uuidgen)"
  550. case "${TERMINAL}" in
  551. pantheon-terminal|io.elementary.t* )
  552. if [[ "${TERMINAL}" == "pantheon-terminal" ]]; then
  553. PROFILE_KEY="org.pantheon.terminal.settings"
  554. else
  555. PROFILE_KEY="io.elementary.terminal.settings"
  556. fi
  557. apply_elementary
  558. ;;
  559. iTerm.app )
  560. apply_darwin
  561. ;;
  562. mintty )
  563. apply_cygwin
  564. ;;
  565. guake )
  566. if [[ -n "$(${DCONF} list /apps/guake/style/)" ]]; then
  567. apply_guake
  568. else
  569. apply_guake legacy
  570. fi
  571. ;;
  572. gnome-terminal* )
  573. if [[ -n "$(${DCONF} list /org/gnome/terminal/)" ]]; then
  574. BASE_DIR="/org/gnome/terminal/legacy/profiles:/:"
  575. PROFILE_LIST_KEY="${BASE_DIR%:}list"
  576. PROFILE_SLUG="${PROFILE_SLUG}"
  577. # Note -- ${BASE_DIR%s} is a workaround to avoid doing additional conditional testing for existing profiles
  578. # if terminal is set to gnome-terminal
  579. : ${DEFAULT_SLUG:="$(${DCONF} read ${BASE_DIR%:}default | tr -d \')"}
  580. LEFT_WRAPPER="["
  581. RIGHT_WRAPPER="]"
  582. PALETTE_DELIM="', '"
  583. apply_gtk
  584. else
  585. BASE_DIR="/apps/gnome-terminal/profiles/"
  586. PROFILE_LIST_KEY="${BASE_DIR/profiles/global}profile_list"
  587. LEGACY_BOLD=true
  588. : ${DEFAULT_SLUG:="$(${GCONF} read ${BASE_DIR}default_profile)"}
  589. apply_gtk legacy
  590. fi
  591. ;;
  592. mate-terminal )
  593. BASE_DIR="/org/mate/terminal/profiles/"
  594. PROFILE_LIST_KEY="${BASE_DIR/profiles/global}profile-list"
  595. LEGACY_BOLD=true
  596. : ${DEFAULT_SLUG:="$(${DCONF} read ${BASE_DIR/profiles/global}default-profile | tr -d \')"}
  597. PALETTE_DELIM=":"
  598. apply_gtk
  599. ;;
  600. tilix )
  601. BASE_DIR="/com/gexperts/Tilix/profiles/"
  602. PROFILE_LIST_KEY="${BASE_DIR}list"
  603. : ${DEFAULT_SLUG:="$(${DCONF} read ${BASE_DIR}default | tr -d \')"}
  604. LEFT_WRAPPER="["
  605. RIGHT_WRAPPER="]"
  606. PALETTE_DELIM="', '"
  607. appy_tilixschemes
  608. apply_gtk
  609. ;;
  610. xfce4-terminal )
  611. apply_xfce4-terminal
  612. ;;
  613. * )
  614. printf '%s\n' \
  615. "Unsupported terminal!" \
  616. "" \
  617. "Supported terminals:" \
  618. " mintty and deriviates" \
  619. " guake" \
  620. " iTerm2" \
  621. " elementary terminal (pantheon/elementary)" \
  622. " mate-terminal" \
  623. " gnome-terminal" \
  624. " tilix" \
  625. " xfce4-terminal" \
  626. "" \
  627. "If you believe you have recieved this message in error," \
  628. "try manually setting \`TERMINAL', hint: ps -h -o comm -p \$PPID"
  629. exit 1
  630. ;;
  631. esac
  632. unset PROFILE_NAME
  633. unset PROFILE_SLUG
  634. unset DEFAULT_SLUG