Terminal with Shell

Post

Posted
Rating:
#1 (In Topic #448)
Avatar
Regular
cage is in the usergroup ‘Regular’
Dose anyone know how to determine when a shell process ends using the vt100 terminal. This is the command I am using in a experimental program I am working on.

Code (gambas)

  1. TerminalView1.Shell("ls -a")

Any suggestions will be very much welcome
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
I found the way to use Terminal view like that was to use the Process.State to see if it's active.
I also had a job getting standard commands to do anything with the Shell or Exec methods.
Only got it to work by first issuing it the 'bash' command, then Input my command followed by ;exit
then a shell is opened , active till the command is done then quits. process state is 1 while bash shell is active
and goes to 0 after exiting.

Not sure if that's the right way to do it but after many hours experimenting that's the workaround i came up with.

(another option is to periodcally read the TerminalView.Text property and process the text (should be something to compare during and after command)


I did something like this…

Code

Public p as Process
Public t as Timer

Public Sub Form_Open()
t = New Timer As "Tim"
t.Delay = 1000
t.Enabled = True
End

Public Sub Tim_Timer()
If p Then
debug p.State  ' Here p.State is either 1 or 0 depending on shell being alive (not exited)
If p.State = 0 Then p.Kill()
Endif

End

Public Sub Button1_Click()
If p Then p.kill  ' Just in case
p = TerminalView1.Exec(["bash"])  ' get a valid shell
TerminalView1.Input("ls -a;exit")  ' send command followed by ;exit then the process stops and p.state becomes 0
End

Not sure if that helps fella
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Guru
cogier is in the usergroup ‘Guru’
I could not get this to work but BruceSteers pointed me in the right direction. I could not get BruceSteers version to work until I added '\n' to the command. Anyway here is my version. Copy the code to a Graphical Application and add the 'gb.form.terminal' component. Hope it helps.

Code (gambas)

  1. ''NEEDS gb.form.terminal
  2.  
  3. TerminalView1 As TerminalView
  4. ComboBox1 As ComboBox
  5. pProcess As Process
  6. Spring1 As Spring
  7. Button1 As Button
  8. Timer1 As Timer
  9. HBox1 As HBox
  10. HBox2 As HBox
  11.  
  12. Public Sub Form_Open()
  13.  
  14.   Setup
  15.   pProcess = TerminalView1.Exec(["bash"])
  16.  
  17.  
  18. Public Sub Timer1_Timer()
  19.  
  20.   If pProcess Then
  21.     If pProcess.State = Process.Stopped Then
  22.       Timer1.Stop
  23.       Message("Finished", "OK")
  24.       TerminalView1.Clear
  25.       pProcess = TerminalView1.Exec(["bash"])
  26.       TerminalView1.SetFocus
  27.     End If
  28.  
  29.  
  30. Public Sub Button1_Click()
  31.  
  32.   Timer1.Start
  33.   TerminalView1.Input(ComboBox1.Text & ";exit\n")
  34.  
  35.  
  36. Public Sub Setup() 'This just sets up the Form and its components
  37.  
  38.   With Me
  39.     .H = 448
  40.     .W = 448
  41.     .Padding = 5
  42.     .Arrangement = Arrange.Vertical
  43.  
  44.   With HBox1 = New HBox(Me)
  45.     .Expand = True
  46.     .Padding = 5
  47.  
  48.   With TerminalView1 = New TerminalView(HBox1)
  49.     .Expand = True
  50.     .Blink = True
  51.  
  52.   With HBox2 = New HBox(Me)
  53.     .W = 488
  54.     .H = 28
  55.  
  56.   With ComboBox1 = New ComboBox(HBox2) As "ComboBox1"
  57.     .W = 100
  58.     .ReadOnly = True
  59.     .List = ["ls -Ra", "ls -a", "du", "tree"]
  60.  
  61.   Spring1 = New Spring(hBox2)
  62.  
  63.   With Button1 = New Button(HBox2) As "Button1"
  64.     .W = 90
  65.     .Text = "&Go"
  66.     .Picture = Picture["icon:/22/right"]
  67.  
  68.   With Timer1 = New Timer As "Timer1"
  69.     .Enabled = True
  70.     .Delay = 250
  71.  
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
aah lol , yeah don't forget the return key \n  :D
Nice code Cogier :)

You get the gist I hope.

Knowing what the terminal view is actually doing internally (idle or not) while it has an active shell in it seems limited but we can monitor the process.status of whether the shell is active.

so trailing your command with ';exit\n' works for that
(i remembered the \n that time ;) )

I assume you know the standard Shell command ?
I'll not presume to know your reasons for using the terminal view to run ls but if i want to run a shell command like that and get it's output i'd use the internal Shell command
Ie. to get the output from your 'ls -a' command…

Code

Dim ComText As String
Shell "ls -a" Wait To ComText
Print ComText

Advantage here of course is in using the 'Wait' argument or not so your program can either launch a command in the background while it continues or it waits for the shell command to be run before continuing.
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
cage is in the usergroup ‘Regular’
Charlies code did work for me.  I modified the code to meet my needs.  The ls command was one of the commands I was experimenting with.  The goal was to create a program that would use some of the off the wall commands for friends who are not comfortable using the terminal command line.  Personally I have no problem using the terminal command line for things.  Anyways here is my modified code:

Code (gambas)

  1. ' Gambas class file
  2. ' Thanks Charlie for the code
  3. 'NEEDS gb.form.terminal
  4.  
  5. TerminalView1 As TerminalView
  6. pProcess As Process
  7. Timer1 As Timer
  8. HBox1 As HBox
  9. TermCmd As String
  10. CmdNum As Integer
  11.  
  12. Public Sub Form_Open()
  13.  
  14.   Setup
  15.   pProcess = TerminalView1.Exec(["bash"])
  16.  
  17.  
  18. Public Sub Timer1_Timer()
  19.  
  20.   If pProcess Then
  21.     If pProcess.State = Process.Stopped Then
  22.       Timer1.Stop
  23.       If CmdNum = 1 Then
  24.         Message("Finished", "OK")
  25.       Endif
  26.       pProcess = TerminalView1.Exec(["bash"])
  27.       TerminalView1.SetFocus
  28.     End If
  29.  
  30.  
  31. Public Sub Setup() 'This just sets up the Form and its components
  32.  
  33.   With Me
  34.     .H = 1024
  35.     .W = 1024
  36.     .Padding = 5
  37.     .Arrangement = Arrange.Vertical
  38.  
  39.   With HBox1 = New HBox(Me)
  40.     .Expand = True
  41.     .Padding = 5
  42.  
  43.   With TerminalView1 = New TerminalView(HBox1)
  44.     .Expand = True
  45.     .Blink = True
  46.  
  47.    With Timer1 = New Timer As "Timer1"
  48.     .Enabled = True
  49.     .Delay = 250
  50.  
  51.  
  52. Public Sub mnuLs_Click()
  53.     CmdNum = 1
  54.     TermCmd = "ls"
  55.    Timer1.Start
  56.   TerminalView1.Input(TermCmd & ";exit\n")
  57.  
  58.  
  59. Public Sub mnuLUpdate_Click()
  60.   CmdNum = 1
  61.   TermCmd = " pkexec pacman --sync --refresh --sysupgrade "
  62.   Timer1.Start
  63.   TerminalView1.Input(TermCmd & ";exit\n")
  64.  
  65.  
  66. Public Sub mnuClear_Click()
  67.  
  68.   TermCmd = "clear"
  69.   Timer1.Start
  70.   TerminalView1.Input(TermCmd & ";exit\n")
  71.  
  72.  
  73. Public Sub mnuClose_Click()
  74.   'Thanks Bruce for this little tid bit.
  75.   Quit 0
  76.  
  77.  

I used a menu for the commands which friends prefer.  I also put in the command to shut down the terminal otherwise the terminal continues to run after the form is closed.  Thanks guys for your input and thanks Charlie once again for the code.
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
Happy to set you all on the right path <EMOJI seq="1f609" tseq="1f609">😉</EMOJI>

Ps. Another way to get your forms to close properly is simply put 'Quit 0' at the end of your form_close procedure. That will kill any lingering unwanted processes on exit <EMOJI seq="1f60e" tseq="1f60e">😎</EMOJI>
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
cage is in the usergroup ‘Regular’
 Thank you very much Bruce.  That works like a champ.
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’
Just a thought m8 I dunno if it will help but i did think of you with the GForm app i've made.

Mostly my thinking was some problems i've had using the terminal view, for some shell stuff i need a proper terminal lol.

I wondered if the app might help as your program could be shell based to solve issues but still have a GUI for your friends.

Just a thought :)
Online now: No Back to the top
1 guest and 0 members have just viewed this.