User's guide (nano Shell)

AWK˜plus for Android - User's guide

 User's guide - Episode

AWK˜plus for Android への興味を更に高めて頂けるよう、開発エピソードを紹介します。
Introducing development episodes to further increase your interest in AWK˜plus for Android.

 Code tour - Comment on the continuation line

継続行にコメントが書けるよう言語仕様を拡張しました。 (Expanded the language specification so that comments can be written on continuation lines)

 foreach url ( \
  'https://www.myoshinji.or.jp/'     # 妙心寺 \
  'https://daihonzan-eiheiji.com/'   # 永平寺 \
  )
  wait
  loadHTML  $url   #1 Show the contents of variable
 # loadHTML &url   #2 variable (Silent mode for displaying large html)
 # loadHTML "html" #3 String
 end

# これによりコメントをコード定義とは別の位置に記述する必要が無くなりました。
  This eliminates the need to write comments in a location separate from the code definition.

※ loadHTML: ブラウザに表示リクエストを出し、ロード完了を最大 6秒間待ちます。
  Make a display request to the browser and wait up to 6 seconds for the load to complete.
※ loadHTML は、パラメータ指定を3種類持っており、特に2番目の変数置換は、大きな html を表示するために追加されたオリジナルには無い機能です。
  (loadHTML has three types of parameter specifications, especially the third variable substitution is a function not found in the original added to display large html)

※ wait: ブラウザに表示した画面に対して戻るボタン押下を待ちます。
  上記の例では wait の位置が通常と逆ですが、これは、最後の表示で戻るボタンを押さないで良いようにする小技です。
  (実行をキャンセルする場合は、タスクマネージャー □ で強制終了します。他に『戻る』ボタン連打、新規 shell 投入などがあります)
   Wait for the back button to be pressed for the screen displayed on the browser.
  In the above example, the wait position is opposite to normal, but this is tricks to avoid pressing the back button on the last display.
  (If you want to cancel the execution, forcibly terminate it with Task Manager □. There are other methods such as repeatedly hitting the "Back" button and entering a new shell)

※ wait でバイブレーションしていますが、個体差があり、タブレットは、振動が弱いようなので鳴動時間を可変にしました。
   Although it vibrates with wait, there are individual differences, and the tablet seems to have weak vibration, so the ringing time was made variable.

 Code tour - copy command

本来なら外部コマンドですが、変数も扱いたいので組込みコマンドとして実装しました。
(Originally it is an external command, but I implemented it as a built-in command because I want to handle variables as well)

Case 1. nano(Color grep).sh
csv ユーティリティでカラムを抽出し、grepで絞り込みます。(Extract columns with csv utility and filter with grep)
 set pattern = /pink|red|vioret/
 csv.awk -v col=1,2 ColorChart.csv
 grep.awk /$pattern/ -
 set html = "<html>AWK˜plus - nano Shell grep /$pattern/<table>"
 foreach x (stdout)
  split arr = $x
  set html += "<tr><td style='background-color:$arr[2]'>\n$arr[1]<br>$arr[2]</td></tr>"
 end
 set html += "\n</table></html>"
 loadHTML &html

Case 2. nano(Color Chart).sh
csv データを copy コマンドで直接 stdout に書き込みます。(Write csv data directly to stdout with the copy command)
 copy ColorChart.csv stdout
 set html = "<html>AWK˜plus - nano Shell<table>"
 foreach x (stdout)
  split arr = &x  # Silent mode
  set html += "<tr><td style='background-color:$arr[6]'>\n$arr[5]<br>$arr[6]</td></tr>"
 end
 set html += "\n</table></html>"
 loadHTML &html

 Code tour - Favorite

お気に入りのコード (Favorite code)

#1 禅寺ツアー (Zen Temple Tour)
  foreach ... wait ... loadHTML

#2 NetBSD 'pom.c' (phase of the moon) + NASA

#3 csv.awk [-Fs] [-v OFS=s] [-v col=<n1[,n2...]>] [-v fmt=<formats>] file...

#4 range
  foreach <var> (`(?:range(<start>,<end>[,<incremental>]))`)

#5 画像拡大表示ライブラリ (Image enlargement display library, Lightbox2 + jQuery)

#1 nano shell の当初開発目標は、これ (プレゼンツール)でした。
 #2 AWK + HTML でコードは、10年前に書きました。
 #3 AWK をワンライナーで使用し不足分を sed で補うといった使い方は、もう無用ですね。
 #4 foreach と range を組み合わせることで for 文として使用でき while 文の出番は、少なくなります。
 #5 js ライブラリを始めて利用しましたがいい感じです。

#1 The initial development goal of the nano shell was this (presentation tool).
 #2 I wrote the code in AWK + HTML 10 years ago.
 #3 It's no longer necessary to use AWK in one liner and make up for the shortfall with sed.
 #4 By combining foreach and range, it can be used as a for statement, and the while statement comes into play less often.
 #5 I used the library for the first time, but it feels good.

 Code tour - blue Bird

変数参照($var)の解析は、厄介です。これは、難解なパーサを書く元気が無いので正規表現で実装しています。
(Parsing variable references ($var) is tricky. This is implemented with a regular expression because I don't have the energy to write an esoteric parser)

 # Multiple indexes
 #           123456789
  split N = "987654321" ""  # Arrangement
  split A = "ABCDEFGHI" ""
  set two = 2
  set three = 3
  echo "X1: (8)   ($N[$two])"
  echo "X2: (H)   ($A[$N[$two]])"
  echo "X3: (7)   ($N[$three])"
  echo "X4: (H9G) ($A[$N[$two]]$#N$A[$N[$three]])"

正規表現には、最長一致(X*)と最短一致(X*?)が有りますが上記の例では、とちらも使い物になりません。
そこで最長一致でマッチさせた後に、{[]} を頼りに枝刈りを行い、再マッチさせるという手法を取りました。 後は、再帰呼び出しでインデクスを処理するだけです。

Regular expressions include the longest match (X*) and the shortest match (X*?), But in the above example, neither is useful.
Therefore, after matching with the longest match, we pruned and rematched by relying on {[]}. All i have to do now is handle the index with a recursive call.

※ 傍にいる青い鳥に気付けばコードを書くのは簡単ですね。 It's easy to write code if you notice the blue bird by your side.

 Code tour - `AWK Command` secret

AWK コマンドは、`<AWK Command>`の形式で、論理演算式・算術演算式・(マクロを含む) 関数を呼び出します。
(AWK commands call logical expressions, arithmetic expressions, and functions (including macro) in the form `<AWK Command>`)

※ コードの実態は、`BEGIN{exit(<expression>)}`です。シンプルですね。
  The actual code is `BEGIN{exit(<expression>)}`. It's simple.
  しかも EXPRESSION :: = EXPRESSION [, EXPRESSION ...] です。 Moreover, EXPRESSION :: = EXPRESSION [, EXPRESSION ...].

`a=0, a++, sqrt(++a)` これは、関数からの戻り値と引数に値を返す処理を 1文で実行するために Scala 用に実装した構文ですが、今は、Groovy のクロージャを使っています。 ということで、存在を忘れていましたが、最後の式の値を返します。こんな所で復活するとは... 女神が微笑んでいるようです。
This is the syntax implemented for Scala to execute the process of returning the return value from the function and the value as an argument in one statement. but now I'm using Groovy closures. So I forgot to exist, but it returns the value of the last expression. Resurrection in such a place ... The goddess seems to be smiling. (Ref. X AWK secret.sh)

 Code tour - Beautiful code

パス /foo/bar/baz の最後部 /bar/baz を抽出せよ。 (Extract the last part of the path)

 String path = "/foo/bar/baz";
 int part = path.replaceAll("[^/]+", "").length();
 while (part-- > 2)
   path = path.replaceFirst("^/[^/]+", "");

シンプルで美しいコードは、バグの入る余地がないですね。(Simple and beautiful code has no room for bugs)

シンプルであることは、複雑であることよりもむずかしいときがある。 物事をシンプルにするためには、懸命に努力して思考を明瞭にしなければならないからだ。だが、それだけの価値はある。 なぜなら、ひとたびそこに到達できれば、山をも動かせるからだ。(ジョブズ)

Simple can be harder than complex. You have to work hard to get your thinking clean to make it simple. But it’s worth it in the end because once you get there, you can move mountains. (Steve Jobs)

 Scripts and extensions

シエルスクリプトの配置場所の制限は有りませんが、ダイアログ表示の行数削減のために親子に分離するルールを作成しました。
 (There is no limit to the location of the shell script, but i have created a rule to separate it into parent and child in order to reduce the number of lines displayed in the dialog)

 SCRIPT ::= /.+[.](awk|.*sh.*)/       (nano Shellでの定義、definition)

 PARENT_SHELL ::= /.+[.](sh)/         (users holder: sh/ ...)

 CHILD_SHELL ::= /.+[.](bsh|csh|nsh)/ (users holder: (sh/) bsh/ csh/ nsh/ ...)

※ 拡張子により親子を識別します。フォルダ名は、お好きなのをどうぞ。 (Identifies parent and child by extension. Please choose the folder name you like)

※ データフォルダ(/data、拡張子の制限無し)、作業フォルダ(/temp、HTML Tool で使用)、/sample.* は、固定です。
   Data folder (/data, unlimited extension), working folder (/temp, used in HTML Tool), /sample.*, fixed.
   また、/awk, /sh を事前定義していますので自由に使用してください。(It is predefined, so feel free to use it)

 Troubleshooting

 Contact address

・Open source project ("awk4j" → search)
https://osdn.net/projects/awk4j/releases/

・Email address
awk4j2020(at sign)gmail.com