Small Basic Forum
Small Basic CMD simulator - Printable Version

+- Small Basic Forum (https://litdev.uk/mybb)
+-- Forum: Small Basic (https://litdev.uk/mybb/forumdisplay.php?fid=1)
+--- Forum: Standard Small Basic (https://litdev.uk/mybb/forumdisplay.php?fid=2)
+--- Thread: Small Basic CMD simulator (/showthread.php?tid=315)



Small Basic CMD simulator - sm4llprogrammer2008 - 04-19-2025

Finally,

After publishing my Bash simulator, I've tried to expand it's very basic code.

ID: ZVVD6.000
Link: https://smallbasic.com/program/?ZVVD6.000

Code:
TextWindow.Hide()
TextWindow.Title = "Windows Command Prompt"
year = Clock.Year
path = "C:\Windows\System32"
helpInfoTxt[1] = "== HELP =="
helpInfoTxt[2] = "dir <path> - Lists files from a path."
helpInfoTxt[3] = "echo <text> - Writes line to the prompt."
helpInfoTxt[4] = "cd <path> - Enters a path."
helpInfoTxt[5] = "help - Shows this text."
TextWindow.Show()
TextWindow.ForegroundColor = "White"
TextWindow.WriteLine("Copyright "+year+" sm4llprogrammer2008")
While ("True")
  TextWindow.ForegroundColor = "White"
  TextWindow.Write(path+">> ")
  TextWindow.ForegroundColor = "Gray"
  prompt = TextWindow.Read()
  EventHandler()
EndWhile

Sub EventHandler 'We'll return events based on input (variable: prompt)
  getting = LDText.Split(prompt, " ") 'Split text
    If getting[1] = "dir" Then 'Event handler for dir
      getpath = getting[2]
      If Text.GetIndexOf(getpath, "C:\") = 1 Then
        files = File.GetFiles(getpath) 'Get files
        For i = 1 To Array.GetItemCount(files) 'List all files from the directory input by the user
          TextWindow.WriteLine(files[i])
        EndFor
      Else 'If no path is typed or none
          TextWindow.WriteLine("Invalid path. Use a valid directory starting with C:\")
     EndIf
    ElseIf getting[1] = "cd" Then 'Event handler for cd
      getpath = getting[2]
      If Text.GetIndexOf(getpath, "C:\") = 1 Then
        path = getpath
      Else
        TextWindow.WriteLine("Invalid path. Use a valid directory starting with C:\")
      EndIf
    ElseIf getting[1] = "echo" Then
      returning = Text.GetSubText(prompt, Text.GetIndexOf(prompt, "echo") + 5, Text.GetLength(prompt) - Text.GetIndexOf(prompt, "echo") - 4)
      TextWindow.WriteLine(returning)
    ElseIf getting[1] = "help" Then
      For i = 1 To Array.GetItemCount(helpInfoTxt)
        TextWindow.WriteLine(helpInfoTxt[i])
      EndFor
    Else
      TextWindow.WriteLine("Unknown command. Type 'help' for info.")
    EndIf
EndSub

Feel free to expand!

Edit: requires LitDev Beta library!