Sponsored Content
Top Forums Shell Programming and Scripting Blog-Thread: Creating a Shell Wrapper and Runtime Modifier (SWARM) Post 303045423 by sea on Friday 20th of March 2020 10:38:29 AM
Old 03-20-2020
Uh finaly, I have it (printe) as I want it to behave.
A simple change - long intended, that when passing 2 strings, one is left, the other right (instead of center).

It doesnt come as easy as one might think...
Code:
	swarm.print.text() { # MODE [ LEFT CENTER RIGHT]
	# This simply puts the text
	# according to MODE
		local fix_title=0
		local fix_header_r=0
		case ${1/-} in
			t)	MODE=title
				fix_title=16	;;
			h)	MODE=header
				fix_header_r=4	;;
			*)	MODE=basic	;;
		esac
		shift

		# Check if it is too short, if so, print fallback mode and return
		[[ ${COLUMNS} -lt 25 ]] && \
				$PRINTF "%s\n" "${@}" && \
				return 1

		# Make sure values are udpated
		swarm.update.geometry

		# Get the actual string
		local evalLeft=$($PRINTF "$1")
		local evalMiddle=$($PRINTF "$2")
		local evalRight=$($PRINTF "$3")

		# Retrieve their length
		local lenEvalLeft=${#evalLeft}
		local lenEvalMiddle=${#evalMiddle}
		local lenEvalRight=${#evalRight}

		# Output
		case ${#@} in
		0)	# Nothing to print
			$PRINTF "${posLEFT}"
			;;
		1)	# Just left oriented
			$PRINTF "${posLEFT}$1"
			#return
			;;
		2)	# 2nd arg is right oriented
			$PRINTF "${posLEFT}$1"
			local posRIGHT="\33[$(( $numEND - $lenMiddle - ${lenEvalMiddle} - 1 + $fix_header_r ))G"
			$PRINTF "${posRIGHT}$2"
			#return
			;;
		3)	# Default handling
			local posMIDDLE="\33[$(( $numHALF - $(( ${#2} / 2 ))  + $fix_title ))G"
			local posRIGHT="\33[$(( $numEND - $lenRight - ${lenEvalRight} - 1 + $fix_header_r ))G"
			$PRINTF "${posLEFT}$1"
			$PRINTF "${posMIDDLE}$2"
			$PRINTF "${posRIGHT}$3"
			;;
		esac

		$PRINTF "${posEND}"
	}
	printe() { # STR1 STR2 STR3
	# Simply prints the strings as passed, it requires 3 strings to use the center
	# For piping, it expects 3 lines, each representing 1 variable
		#set -x
		local MODE="none"
		# Get args
		case "$1" in
		"-1"|"-2")	MODE="${1/-}"
					shift
					;;
		*)			MODE="normal"
					;;
		esac
		# Get arg number if not forced yet
		[[ "normal" == "$MODE" ]] && [[ "-" != "$1" ]] && [[ "--" != "$1" ]] && MODE=${#@}
		# Get pipe?
		case "$1" in
		"--"|"-")
				case "$MODE" in
					"2")
						# Read pipe, expect 2 lines
						while read LEFT
						do
							read RIGHT
							printe "$LEFT" "" "$RIGHT"
						done
						return
						;;
					"1")
						# Read pipe, expect 1 lines
						while read LEFT
						do
							printe "$LEFT"
						done
						return
						;;
					*)
						# Read pipe, expect 3 lines (default)
						while read LEFT
						do
							read CENTER; read RIGHT
							printe "$LEFT" "$CENTER" "$RIGHT"
						done
						return
						;;
					esac
				;;
		esac
		#set +x
		case "$MODE" in
			"0")
				swarm.print.border -e
				swarm.print.text -e "" "" ""
				;;
			"1")
				swarm.print.border -e
				swarm.print.text -e "$1" "" ""
				;;
			"2")
				swarm.print.border -e
				swarm.print.text -e "$1" "" "$2"
				;;
			"3")
				swarm.print.border -e
				swarm.print.text -e "$1" "$2" "$3"
				;;
		esac
		$PRINTF "$posEND\n"
	}

Now comes the really fun part... passed string splitting...
And 'select'.

Also, if you wonder WHY (the heck) I upload so often pictures of SWARM, thats because 'just before' doing that picture, all of the 'UI' was terrible messed up.
So it's kind of: Hey it works (again), and as a reminder which build number I'd have to pick from the backups.
This said, here we go Smilie
Blog-Thread: Creating a Shell Wrapper and Runtime Modifier (SWARM)-community-st-swarmjpg

And I just thought, it might be 'easier' to 'make': select = pick (instead of choose) and ask = yesno, because for regular user input, I'd make a function called input.
What do you think?

TODO:
  • Maybe even remove BOLD from title, looks a bit 'light' on TTY (hard to read : try different colors/theme first)
  • Find a non-tempfile using method printing different stages/index of an array (\ | / - \ | )
  • Fiddle my way around an 'intervall based' on UNIX seconds differences between calls from within a function without using tempfiles (alternative to the previous 'multi task' attempt)
  • select
  • edit/web/terminals
  • progress/bar
  • download
  • cfg.get & cfg.set
  • swarm configuration (general purpose *conf file handler, supports in-file-commented options for the handlers)
  • more themes (ideas or wishes, anyone? Smilie )
  • typewriter
  • swarm.bol.dir ?
  • swarm.str.distro ?
  • swarm.install (distro package manager wrapper ; install only)

And these are just whats on top of my mind.
Now, allthough I have most of this "done already" - it's for TUI which was based on individual files, rather than functions - and I'm using new syntax and methods which 'dont help' to reduce 'coding time' but should help with readability and reusability of the code.
If you compare printe, well incl swarm.print.border and swarm.print.text to it's origin tui-echo, it is ALOT more readable!

However, when it was 'all file based', it was simple, all user-commands were files, and had to have args like --help and --version, but with funcions?
Sure, I could do that.
Issue is, I want it translate-able.. by which I mean, people should be able to do that 'quickly' as part of a hobby or enhusiastic week.
Thus, I only want to focus on CORE (end-user) functions (that other script authors are supposed to be using - only - I know, wont happen).

Thanks to the syntax rules I had set within my project, I could already parse current files functions and list their description (First 2 commented lines after the function definition, which as well provides the ARGS syntax, where applicable Smilie
Code:
	swarm.eu.function.list() { # [FILE]
	# Prints a list of properly declared functions
	# Please see: ./docs/{MANUAL,SYNTAX}.md)
		local tmp_list=""
		local tmp_oldpwd="${PWD:-$($PWD_EXEC)}"

		if [[ -n "$1" ]] && [[ -f "$1" ]]
		then	# There is a specific file passed to parse
				cd "$SWARM_DIR_LIBS"
				$GREP "() { #" "$1"| \
					$GREP -v GREP | \
					$AWK -v FS='() ' '{print $1}' | \
					$SED s,'()','',g
				cd "$tmp_oldpwd"
		else	# Just parse all files in SWARM_DIR_LIBS
				raw_output() {
					(
						cd "$SWARM_DIR_LIBS"
						$GREP init.*"() {" * | $GREP -v GREP
						$GREP cfg.*"() {" * | $GREP -v GREP
						$GREP swarm.*"() {" * | $GREP -v GREP
						cd "$tmp_oldpwd"
					) | while IFS=": " read one two three
					do
						# The IFS takes care of the GREP filenames
						# and this variable-regex takes care of the function definition
						echo "${two/()}"
					done
				}
				# Show data
				raw_output | sort -u
				unset -f raw_output
		fi
	}
	swarm.eu.function.show() { # MODE [FILE] FUNCTIONNAME		## MODE= --text || --code
	# Prints either these 2 comment lines or the code
	# Please see: ./docs/{MANUAL,SYNTAX}.md)
		#
		# Vars
		#
			local tmp_oldpwd="${PWD:-$($PWD_EXEC)}"
			local GREP_OPTS=""
		#
		# Check for args
		#
			local MODE=none
			case "$1" in
				"--text")
					MODE=text
					shift
					;;
				"--code")
					MODE=code
					shift
					;;
				*)
					MODE=text
					#return 1
					;;
			esac
			[[ -f "$1" ]] && \
				local curFILE="$1" && shift || \
				local curFILE=""
		#
		# Code
		#
			# Prepare command
			case "$MODE" in
				"text")
					GREP_OPTS="-h -a2  ${1}"
					cd "$SWARM_DIR_LIBS"
					# This is just to reduce unrequired disk usage
					[[ -n "$curFILE" ]] && \
						$GREP $GREP_OPTS "$curFILE"|| \
						$GREP $GREP_OPTS *
					cd "$tmp_oldpwd"
					;;
				"code")
					type "$1"
					;;
			esac
	}

Though, the *show function still needs some tweaking on the --text handling.

It's funny, I know how cool it is when it's done, but still I'm kind of overhelmed by the annoyance of 'reinventing the wheel' (redo what's already done).
And that is despite the fact, that I get alot done, alot quicker than I had anticipated - with alot more tweaks than it used to have.

If I had done this like 20 or 25 years ago.. oh boy this would have been THE invention of the decade. (speaking of how 'swarm' (function, aka tui-browser) will behave when all is done and works properly)
Now it's just some enthusastic old reto fanboy hobby project.

Oh geez, guess I have one of my moments, as I was just preparing some post offline, lots of text to be structured before actual posting it.
Well, while I prepared that text - regarding (my hopes for) the community-project Script-Tools, I had figured how much work there's still to do.

If I could briefly describe what swarm does, I would.
And that part I could describe in short, doesnt nearly reflect its full potential!

Once I gotten that far, I'll need to do a good video (with actual editing and voice-overs) to show at least parts of it's full power.
Because just describing is WAY too abstract, even for me reading my own text - despite knowing what I'm refering to...
Yet it's all very simple and almost obvious - at least I tried to achieve this for me.

But I'm already thinking ahead of time.
Stay focused, here and now.

Oh boy, I cant wait until I have all basics covered.
Yeah that help thing....
Since no install, no need to adjust the manpages I'd say, dont you?
So just basic --help coverage for the intended 'user'/author functions usage, sounds about right'ish, right? Smilie
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

korn shell version at runtime?

How can I check what kornshell version I am using at runtime from within a kornshell script? (3 Replies)
Discussion started by: qanda
3 Replies

2. Shell Programming and Scripting

crontab is not creating runtime files which are in script..

this is the output i am getting here.. cp: cannot create /wls_domains/eoigw/eoigwsA/deliv/cron/MailingScript/eoigwsA_Health_Status_Report.html: Permission denied /wls_domains/eoigw/eoigwsA/deliv/cron/MailingScript/ /wls_domains/eoigw/eoigwsA/deliv/cron/MailingScript/GenerateReport.sh:... (6 Replies)
Discussion started by: surekha268
6 Replies

3. Shell Programming and Scripting

Korn Shell Wrapper script

Hi Guys, I am trying write a wrapper script but I don't have any idea. I have 4 different korn shell scripts and all of them needs some parameters from command line (positional parameter). My script cant be interactive because its supposed to be automated. I am confused how can I write a wrapper... (6 Replies)
Discussion started by: pareshan
6 Replies

4. Web Development

Creating a blog site on a local computer

Hello! I would like to create a blog website on a web domain of mine. The blog will be used for publishing economics-lated articles. I tried to use a few open source packages for blog creation (WorldPress, b2evolution, Movable type) which I wanted to test on a local computer before arranging... (5 Replies)
Discussion started by: degoor
5 Replies

5. Programming

creating multiple threads using single thread id

Hi all, Can I create multiple threads using single thread_id like pthread_t thread_id; pthread_create(&thread_id, NULL, &print_xs, NULL); pthread_create(&thread_id, NULL, &print_ys, NULL); pthread_create(&thread_id, NULL, &print_zs, NULL); pthread_join(thread_id, NULL); what... (2 Replies)
Discussion started by: zing_foru
2 Replies

6. Shell Programming and Scripting

Shell Runtime Statistics

Hi, I am trying to capture runtime stats of a shell script (c shell). Are there system variables to call? Or should I create a date variable at the start of the script and at the end of the script? I am trying to capture the time if the script stops or ends with error. Please help. ... (4 Replies)
Discussion started by: CKT_newbie88
4 Replies

7. Shell Programming and Scripting

Wrapper Script in Perl Or shell

Hello, My requirement is based on Oracle where we run a perl script and it asked some questions.I want to write a wrapper which will answer all these questions. How is it possible. Thanks (16 Replies)
Discussion started by: cotton
16 Replies

8. Homework & Coursework Questions

Shell Script average runtime

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Make a bash script that calculates average runtime for the first two scripts you made. The average should be... (17 Replies)
Discussion started by: navlelo
17 Replies

9. Shell Programming and Scripting

Shell Script and Progress Bar or GUI Wrapper

Hi, I have shell script that I am running under Ubuntu as root. Is it possible to hide the command window and show the user some sort of progress /random progress bar / or other form of GUI interaction? On MAC, I have been using Platypus but on Ubuntu I am not sure what to do. (4 Replies)
Discussion started by: naveedanwar4u
4 Replies
All times are GMT -4. The time now is 11:36 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy