Wizards should always be able to run from console without X

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
goingnuts
Posts: 932
Joined: Sun 07 Dec 2008, 13:33
Contact:

Wizards should always be able to run from console without X

#1 Post by goingnuts »

Might be a little provocative but:
Setup-wizards should as a minimum be able to run from shell without any GUI wrapped around it. As a secondary demand it should run in a very basic environment (like ash...).
The attached demo script (floppytools.sh) contains 5 functions to help accomplish above without missing an opportunity to use a GUI if present:
message_fc, input_fc, menu_fc, input_date and input_time.
All 5 functions is called with the same arguments but can output in pure shell, in dialog or in Xdialog.
This can be set automatic in the script so the users environment determine which output should be used.

floppytools.sh need some polish in coding but main purpose is to demonstrate the use of the 3 of the 5 functions...

Functions can be refined to be more selective ex. choosing more advanced GUIs...
Update: Always discovers the bug after posting...now it actually runs form shell in autoselect mode...(changed [ -n $DISPLAY ] to [ ! "$(ps | grep xwin)" = "" ] to determine if in X or not)

Update - see 4th post
View how-to further down - and the output of message_fc and menu_fc from the same code...
Attachments
floppytools.sh.tar.gz
tools for floppy things (copy, formate, apply image, make image)
(2.13 KiB) Downloaded 578 times
Last edited by goingnuts on Mon 22 Nov 2010, 17:39, edited 5 times in total.

User avatar
abushcrafter
Posts: 1418
Joined: Fri 30 Oct 2009, 16:57
Location: England
Contact:

#2 Post by abushcrafter »

+1
[url=http://www.adobe.com/flashplatform/]adobe flash is rubbish![/url]
My Quote:"Humans are stupid, though some are clever but stupid." http://www.dependent.de/media/audio/mp3/System_Syn_Heres_to_You.zip http://www.systemsyn.com/

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#3 Post by jpeps »

Code: Select all

 #main code from P412 floppy-format.sh Copyright (c) Barry Kauler 2004 www.goosee.com/puppy
  #and floppytool.sh by J.P. Nimmo(from tinycore)
 #function to use pure shell, dialog or xdialog output 
Last time I was around, tinycore didn't even have dialog (roberts removed it when he couldn't figure out how to get it to work). I doubt if they have Xdialog. edit; not in base, anyway (no more comments re: tc )
If you've noticed, puppy uses gtkdialog3 (and dialog and Xdialog).

goingnuts
Posts: 932
Joined: Sun 07 Dec 2008, 13:33
Contact:

#4 Post by goingnuts »

...puppy uses gtkdialog3 (and dialog and Xdialog).
...and xmessage and yaf-splash and gtkdialog2...
of which only dialog works from console... :)

goingnuts
Posts: 932
Joined: Sun 07 Dec 2008, 13:33
Contact:

#5 Post by goingnuts »

Updated the functions:
Now input handles both text and file-select
"Infinite" selects in menu
Much better sizing of Xdialog-boxes
Added date and time dialogs

Attached two examples using the functions: settime.sh and floppytools.sh
Floppytools has been updated so cancel is handled more logic...

Both examples use the dialogfunctions.sh as includes.
Attachments
dialog_functions.pet
Examples (floppytools and settime) using dialogfunctions.sh
(3.88 KiB) Downloaded 552 times

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#6 Post by big_bass »

Setup-wizards should as a minimum be able to run from shell without any GUI wrapped around it.
slackware has pkgtool that does all the important things to set up and install packages

people still complain because it looks old but they miss the point that it runs without X

*I re wrote it in Xdialog to give it a modern look and added functions to it such as dependency checking but I still keep the non X version as an emergency when X is broken for some reason

it already works on a "puppy base" and its easy to modify the set up part
to your special needs

thats the script to use and modify

Joe

goingnuts
Posts: 932
Joined: Sun 07 Dec 2008, 13:33
Contact:

#7 Post by goingnuts »

How-to message:

Code: Select all

#!/bin/sh
. dialogfunctions.sh

TITLE="Message Demo"
TEXT="This is the output from the
message_fc
function.
"
#run the function
message_fc
Attachments
Xdialog_output.png
Output from message_fc via Xdialog
(6.29 KiB) Downloaded 1038 times
dialogOutput.png
Output from message_fc via dialog
(6.15 KiB) Downloaded 1087 times
ShellOutput.png
Shell/console output from message_fc
(2.22 KiB) Downloaded 997 times
Last edited by goingnuts on Thu 18 Nov 2010, 16:31, edited 1 time in total.

goingnuts
Posts: 932
Joined: Sun 07 Dec 2008, 13:33
Contact:

#8 Post by goingnuts »

Howto - menu_fc:

Code: Select all

#!/bin/sh
. dialogfunctions.sh
 
TITLE="Message Demo"
TEXT="Example of creating a menu and evaluation
of the function retur."
#write CHOICES to file
echo "This is the first choice
      This is the second choice
      And the last choice" >/tmp/choices
#run the function
menu_fc
#evaluate response
menuitem=$(cat /tmp/reply) 2> /dev/null
case $menuitem in
  0) echo "Your further code for first choice goes here";;
  1) echo "your further code for second choice goes here";;
  2) echo "Your further code for third choice goes here";;
esac 
Attachments
XdialogMenu.png
Output via Xdialog
(9.94 KiB) Downloaded 1073 times
dialogMenu.png
Output via dialog
(9.28 KiB) Downloaded 1024 times
shellMenu.png
Output via shell/console
(3.39 KiB) Downloaded 1026 times

goingnuts
Posts: 932
Joined: Sun 07 Dec 2008, 13:33
Contact:

#9 Post by goingnuts »

big_bass wrote: people still complain because it looks old but they miss the point that it runs without X
The GUI/look is where your code meets the user...and the user is always right...

The dialogfunctions is a try to get around the look without compromising on functionality. A series of dialogs is still old school compared to a GTK GUI where everything can be set in one window - but the inclusion of the more basic core user-interfaces do not exclude the one-window-GTK-GUI: Just make a switch in the script to use GTK-fancy if GTK is present and you are in an X-environment - otherwise it will run anyway and you do not leave the user with no way to get the job done.

I found my self more productive not spending time on getting a dialog or a Xdialog widget to work - just doing the basic script logic and call the dialogfunctions.

I have exercised the functions on Puppy setup-scripts - always using a lot of time reading and understanding the GTK-part of the programs. On the other hand applying the functions on a CLI or dialog based script takes much less time.

If it makes sense and is of any help I could try to apply the dialogfunctions on your modified CLI or Xdialog Slackware pkgtool?

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#10 Post by big_bass »

here is an odd script on the same thought


some notes dialog and xdialog have differences when
you try to make dynamic menus
in xdialog I had to code a work around for it because
it lacks a command only used in dialog


joe



Code: Select all

 #!/bin/sh
 
 # detects if X display is running and you clicked on the script
 # or if you are running an app  pasted in a terminal
 # or you are not running x
 # Joe Arose 
 # call this xtest2
 
clear
  
 CONSOLE_APP=(`env |grep "DISPLAY="`)



clear
 
 #  you ran the script in X pasted inside a terminal 
 if [[ $CONSOLE_APP = "DISPLAY=:0.0" ]]; then
                #echo " X  is being used  on --->  $CONSOLE_APP 
                 yaf-splash -font "8x16" -outline 0 -margin 4  -fg black -text " you ran the script in X pasted inside a terminal"


# you ran the script in X  by clicking on it    DISPLAY=:0  not :0.0
 elif [[ $CONSOLE_APP = "DISPLAY=:0" ]]; then
                #echo " X  is being used  on --->  $CONSOLE_APP 
                 yaf-splash -font "8x16" -outline 0 -margin 4  -fg black -text " you ran the script in X by clicking it"




 elif [[ $CONSOLE_APP = "" ]]; then
               echo "-----------------------------------------------------------------------"
               echo ""
               echo "X is not running you are running in the console type xwin to return  "
               echo "pkgtool is for installing packages in the console   "
               echo ""
               echo "-----------------------------------------------------------------------"   
 fi



jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#11 Post by jpeps »

big_bass wrote:here is an odd script on the same thought


some notes dialog and xdialog have differences when
you try to make dynamic menus
in xdialog I had to code a work around for it because
it lacks a command only used in dialog


joe

Lets you know if you click from ROX, but not from something like gtkdialog3 or bacon.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#12 Post by technosaurus »

$console_app is unnecessary - you can query DISPLAY directly (there are some examples in pupngo)

it _should_ work from gtkdialog and bacon and x and y and z - DISPLAY is set when X starts and unset when it exits (at least it is supposed to)

the :0.0 refers to screen 1 ... if you have multiple monitors (via Xinerama) I _think_ it would be :o.* (where * is the screen number)

I use this to set the DIALOG variable and use it in my scripts where the Xdialog scripts do not use a tag that isn't available in dialog
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#13 Post by big_bass »

lets you know if you click from ROX, but not from something like gtkdialog3 or bacon.
havent tried in bacon
that would be difficult because you compile the code another head for the code would have to do the check maybe

you just place it as a head in gtkdialog like the easy demo here

then the gtkdialog code


Code: Select all

#!/bin/bash

 
 # detects if X display is running and you clicked on the script
 # or if you are running an app from console pasted in a termial
 # or you are not running x
 # Joe Arose 
 # call this xtest2
 
clear
  
 CONSOLE_APP=(`env |grep "DISPLAY="`)
# you ran the script in X  by clicking on it    DISPLAY=:0  not :0.0
# yaf-splash -font "8x16" -outline 0 -margin 4  -fg black -text " X is being used  on --->  "$CONSOLE_APP"" &

clear
 
 #  you ran the script in X pasted inside a terminal 
 if [[ $CONSOLE_APP = "DISPLAY=:0.0" ]]; then
                #echo " X  is being used  on --->  $CONSOLE_APP 
                 yaf-splash -font "8x16" -outline 0 -margin 4  -fg black -text " you ran the script in X pasted inside a terminal"

 elif [[ $CONSOLE_APP = "DISPLAY=:0" ]]; then
                #echo " X  is being used  on --->  $CONSOLE_APP 
                 yaf-splash -font "8x16" -outline 0 -margin 4  -fg black -text " you ran the script in X by clicking it"




 elif [[ $CONSOLE_APP = "" ]]; then
               echo "-----------------------------------------------------------------------"
               echo ""
               echo "X is not running you are running in the console type xwin to return  "
               echo "pkgtool is for installing packages in the console   "
               echo ""
               echo "-----------------------------------------------------------------------"   
 fi



export MAIN_DIALOG='
 <vbox>
  <frame Checkbox example>
    <checkbox>
      <label>This is a checkbox...</label>
      <variable>CHECKBOX</variable>
      <action>echo Checkbox is $CHECKBOX now.</action>
      <action>if true enable:ENTRY</action>
      <action>if false disable:ENTRY</action>
    </checkbox>
    <checkbox>
      <label>I want an OK button NOW!</label>
      <variable>CHECKBOX2</variable>
      <action>if true enable:OKBUTTON</action>
      <action>if false disable:OKBUTTON</action>
    </checkbox>
    <checkbox>
      <label>I want coffee NOW!</label>
      <variable>CHECKBOX3</variable>
      <action>if true enable:OKBUTTON</action>
      <action>if false disable:OKBUTTON</action>
    </checkbox>
    </frame>
  <hbox>
   <button ok>
     <variable>OKBUTTON</variable>
   </button>
   <button cancel></button>
  </hbox>
 </vbox>
'

gtkdialog --program=MAIN_DIALOG







jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#14 Post by jpeps »

Tried script as posted, with same result.

Edit: Works if clicked from ROX, though, but not if loaded from a JWM icon.
That seems to be the issue....running the program from the JWM taskbar.

goingnuts
Posts: 932
Joined: Sun 07 Dec 2008, 13:33
Contact:

#15 Post by goingnuts »

Maybe this one will do it?

Code: Select all

#!/bin/sh
[ -n $DISPLAY ] && [ "$(tty)" = "not a tty" ] && exec rxvt -e sh -c $0 
echo "I am forced to be spawned from a shell"
read DUMMY
Seems to work clicking from rox, running from shell in X and running from console.
From jwm menu you still need to start with "rxvt -e PROGRAM".
From jmw tray TrayButton use "exec:rxvt -e PROGRAM".
From jwm desktop TrayButton use "exec:PROGRAM".

So final header in dialogfunctions.sh could be:

Code: Select all

TYPE="cli"		#dialog or cli
[ $(which dialog) ] && TYPE="dialog" && DIALOG=dialog
[ $(which Xdialog) ] && [ -n $DISPLAY ] && DIALOG=Xdialog
[ -n $DISPLAY ] && [ "$(tty)" = "not a tty" ] && [ ! "${DIALOG}" = "Xdialog" ] && exec rxvt -e sh -c $0
(Xdialog do not need the forced spawn - I do not know if GTKdialogs needs it). The call for rxvt could be changed to /usr/local/bin/defaultshell if present or a check for different shells available.

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#16 Post by jpeps »

This seems to work:

Code: Select all

#!/bin/sh
VAR="$(echo $(tty) | cut -d: -f1 )"
if [ "$VAR" = "not a tty" ]; then
yaf-splash -text "clicked"
else
yaf-splash -text "in console"
fi

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#17 Post by technosaurus »

Code: Select all

#!/bin/sh
[ ! $DISPLAY ] && echo NoX && exit || \
[ $(tty) ] && yaf-splash -text Terminal && exit || \
yaf-splash -text Gui
or if you don't care if it is in a terminal you can simply use

Code: Select all

#!/bin/sh
[ $DISPLAY ] && yaf-splash -text X && exit || echo NoX
the second will be part of bashbox as a function called message

Code: Select all

message() {
[ $DISPLAY ] && yaf-splash -text "$@" && exit || echo "$@"
}
I also use this to pick what dialog to use:

Code: Select all

[ $DISPLAY ] && DIALOG=Xdialog || DIALOG=dialog
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

goingnuts
Posts: 932
Joined: Sun 07 Dec 2008, 13:33
Contact:

#18 Post by goingnuts »

Maybe I am missing the point (and excuse me if I am) but:
jpeps: I cant get your code to work from console.
technosaurus: cli/dialog-scripts run from console wont be able to run with your switches - but it will tell me why...

jpeps code indicate that there can be different output from tty so I rephrase my code suggestion to:

Code: Select all

#!/bin/sh
[ -n $DISPLAY ] && [ "$(tty | grep not)" ] && exec rxvt -e sh -c $0
echo "I am forced to be spawned from a shell...
and I could call cli, dialog, Xdialog, yaf-splash,
GTK-dialog or xmessage-content, dependent on which
environment I am in - instead of just using echo..."
read DUMMY

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#19 Post by technosaurus »

Yes - they will be able to run in the console - I tested it
I was just simplifying the logic so that the code is more readable,

It simply echoes "NoX" from the console (that is the code)
In a terminal it displays "Terminal" using yaf-splash
From a gui it displays "Gui" using yaf-splash

feel free to insert whatever necessary code at those points
The only reason I can see bothering with tty is if you think that Xdialog
would not be available. Then you would set DIALOG="rxvt -e dialog"
or something like that.

I made an automatic gui generator in bashbox that just accepts a set of inputs - it would be fairly simple to build a cli interface to it.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

goingnuts
Posts: 932
Joined: Sun 07 Dec 2008, 13:33
Contact:

#20 Post by goingnuts »

technosaurus:
:oops: you are right :oops: it works of course in console.
The only reason I can see bothering with tty is if you think that Xdialog would not be available
That is what I am trying to say - quoting myself:
Setup-wizards should as a minimum be able to run from shell without any GUI wrapped around it
I will change thread title to:
Wizards should always be able to run from shell/console without X...

Post Reply