From 696a3258d196cb05e5b67cd4169f465aaa70fb83 Mon Sep 17 00:00:00 2001
From: M33 <327-m33@git.qoto.org>
Date: Sat, 20 Mar 2021 16:08:11 +0000
Subject: [PATCH] an automatic terminal keyboard layout generator

---
 terminal_layout.sh | 165 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 165 insertions(+)
 create mode 100644 terminal_layout.sh

diff --git a/terminal_layout.sh b/terminal_layout.sh
new file mode 100644
index 0000000..3392c1f
--- /dev/null
+++ b/terminal_layout.sh
@@ -0,0 +1,165 @@
+#!/bin/bash
+#
+# terminal_layout.sh
+#
+# A simple script that will generate "terminal" layouts from "normal" layouts for squeekboard
+# Gitlab depot : https://git.qoto.org/m33/squeekboard-terminal-layout-generator
+#
+# M33 (https://octodon.social/M33) - 20.03.2021
+#
+
+# Adjust the destination directory to your liking
+TERMINAL_DIR="/Users/ml/Documents/src/bash/squeekboard-terminal-layout-generator/terminal_layout"
+NORMAL_DIR="/Users/ml/Documents/src/bash/squeekboard-terminal-layout-generator/normal_layout"
+# Add a special first row, with quick access to usefull keys in a terminal
+FIRST_ROW="first_row.template"
+# Additional outlines
+OUTLINES="outlines.template"
+# Additionnal buttons entries
+BUTTONS="buttons.template"
+# Spacebar resize since we may add usefull caracters on the last row, prevent the whole layout shrinking
+SPACE_RESIZE="110"
+
+##########################################################################################
+
+
+#
+# add the terminal specific outline header
+#
+function add_outline { file=$1
+  echo "  add_outline()..."
+  cat /dev/null > $TERMINAL_DIR/$file.tmp
+
+  IFS=''
+  while read line; do
+    echo $line | grep -q -E "^outlines:$"
+    if [ $? -eq 0 ]; then
+      # begin outline section
+      echo "    Found outline header, adding datas..."
+      echo $line    >> $TERMINAL_DIR/$file.tmp
+      cat $OUTLINES >> $TERMINAL_DIR/$file.tmp
+    else
+      echo $line | grep -q -e "[ ][ ][ ][ ]spaceline:.*"
+      if [ $? -eq 0 ]; then
+        echo "    Found space, resizing..."
+        echo "s/width: [0-9]*/width: $SPACE_RESIZE/" > resize.sed
+        echo $line | sed -f resize.sed >> $TERMINAL_DIR/$file.tmp
+        rm -f resize.sed
+      else
+        echo $line    >> $TERMINAL_DIR/$file.tmp
+      fi
+    fi
+  done < $NORMAL_DIR/$file
+}
+
+#
+# add the action row on top of the layout
+#
+function add_first_row { file=$1
+  echo "  add_first_row()..."
+  cat /dev/null > $TERMINAL_DIR/$file.tmp1
+  in_section_views=0
+
+  IFS=''
+  while read line; do
+    echo $line | grep -q -E "^views:$"
+    if [ $? -eq 0 ]; then
+      # begin views section
+      echo "    Found views header, looking for individual layouts..."
+      in_section_views=1
+      echo $line    >> $TERMINAL_DIR/$file.tmp1
+    else
+      echo $line | grep -q -E "^[ ][ ][ ][ ][a-z]*:$"
+      if [ $? -eq 0 ] && [ $in_section_views -eq 1 ]; then
+        echo "      Found layout subsection ($line), adding data..."
+        echo $line     >> $TERMINAL_DIR/$file.tmp1
+        cat $FIRST_ROW >> $TERMINAL_DIR/$file.tmp1
+      else
+        # a normal line, just append it to the destination file
+        echo $line    >> $TERMINAL_DIR/$file.tmp1
+        echo $line | grep -q -E "^buttons:$"
+        if [ $? -eq 0 ]; then
+          in_section_views=0
+        fi
+      fi
+    fi
+  done < $TERMINAL_DIR/$file
+
+  mv $TERMINAL_DIR/$file.tmp1 $TERMINAL_DIR/$file
+}
+
+function add_buttons { file=$1
+  echo "  add_buttons()..."
+  cat /dev/null > $TERMINAL_DIR/$file.tmp1
+  in_buttons_views=0
+
+  IFS=''
+  while read line; do
+    echo $line | grep -q -E "^buttons:$"
+    if [ $? -eq 0 ]; then
+      # begin views section
+      echo "    Found buttons header, adding datas..."
+      echo $line   >> $TERMINAL_DIR/$file.tmp1
+      cat $BUTTONS >> $TERMINAL_DIR/$file.tmp1
+    else
+      # a normal line, just append it to the destination file
+      echo $line    >> $TERMINAL_DIR/$file.tmp1
+    fi
+  done < $TERMINAL_DIR/$file
+
+  mv $TERMINAL_DIR/$file.tmp1 $TERMINAL_DIR/$file
+}
+
+#
+# transform $files normal layout in a terminal layout
+#
+function transform { file=$1
+  echo "transform($file)..."
+
+  add_outline   "$file"
+  add_first_row "$file.tmp"
+  add_buttons   "$file"
+  echo " "
+
+  mv -f $TERMINAL_DIR/$file $TERMINAL_DIR/$1
+}
+
+#
+# main
+#
+function main {
+  if [ "$1" == "" ]; then
+    echo "terminal_layout.sh <file.yaml>  => generate a terminal layout from this file"
+    echo "terminal_layout.sh ALL => generate all terminal layouts"
+    exit 0
+  fi
+
+  FILE_LIST="$1"
+
+  if [ "$FILE_LIST" == "ALL" ]; then
+    PREV_DIR=$PWD
+    cd $NORMAL_DIR
+    FILE_LIST=`ls *.yaml`
+    cd $PREV_DIR
+  else
+    if [ ! -e "$NORMAL_DIR/$FILE_LIST" ]; then
+      echo "Error: $FILE_LIST incorrect"
+      exit 1
+    fi
+  fi
+
+  mkdir -p $TERMINAL_DIR $NORMAL_DIR
+  if [ $? -ne 0 ]; then
+    echo "Error: can't create $TERMINAL_DIR or $NORMAL_DIR"
+    exit 1
+  fi
+
+  for file in $FILE_LIST; do
+    transform $file
+  done
+
+  echo "bye!"
+}
+
+main $@
+exit 0
-- 
GitLab