Skip to content

repl: support executing shell commands on REPL with !sh [COMMAND] - #20496

Merged
spytheman merged 3 commits into
vlang:masterfrom
viniciusfdasilva:master
Jan 12, 2024
Merged

spytheman merged 3 commits into
vlang:masterfrom
viniciusfdasilva:master

Conversation

@viniciusfdasilva

@viniciusfdasilva viniciusfdasilva commented Jan 11, 2024 •

Copy link
Copy Markdown
Contributor

Add suport to execute shell scripts on v REPL

Implementation code change on vrepl.v

fn repl_help() {
	println(version.full_v_version(false))
	println('
	|help                   Displays this information.
	|list                   Show the program so far.
	|reset                  Clears the accumulated program, so you can start a fresh.
	|Ctrl-C, Ctrl-D, exit   Exits the REPL.
	|clear                  Clears the screen.
	|pin                    Pins the entered program to the top.
	|!sh [COMMAND]          Execute on REPL shell commands.
'.strip_margin())
}
fn run_shell(command string) {
	if command.len >= 2 && command[0..2] == 'cd' {
		command_splited := command.split(' ')
		assert command_splited.len >= 2
		dir := command_splited[command_splited.len - 1]

		os.chdir(dir) or { eprintln('`${command}` failed, err: ${err}') }
	} else {
		os.system(command)
	}
}
if r.line.len > 4 && r.line[0..3] == '!sh' {
	run_shell(r.line[4..r.line.len])
	continue
}

Implementation working

repl

Tested implementation!

v fmt -w cmd/tools/vrepl.v Execution result

file

@Delta456

Copy link
Copy Markdown
Member

I don't understand the need for this. One can always import os and use the system function to achieve the same functionality.

@viniciusfdasilva

viniciusfdasilva commented Jan 11, 2024 •

Copy link
Copy Markdown
Contributor Author
  • Well, its more easy to REPL user type direct shell commands than call every time os library functions!
  • In this case it was implemented for users that execute a lot of library functions only change directory, create files, see process running, etc...
  • This is to make user interact with operating system without exit of V REPL without execute a lot os library functions!

@spytheman

Copy link
Copy Markdown
Contributor

@Delta456 It is a good feature. ipython for example has ! for this, try ! ls -la for example or ! df /.

@spytheman

Copy link
Copy Markdown
Contributor

@viniciusfdasilva please do v fmt -w cmd/tools/vrepl.v and push the result too.

Comment thread cmd/tools/vrepl.v Outdated
Comment thread cmd/tools/vrepl.v Outdated
Comment thread cmd/tools/vrepl.v Outdated

@spytheman spytheman left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.
Thank you for your first PR.

@spytheman spytheman changed the title [Feature] - REPL - Support to execute shell commands on REPL repl: support to executing shell commands on REPL with !sh [COMMAND] Jan 12, 2024
@spytheman spytheman changed the title repl: support to executing shell commands on REPL with !sh [COMMAND] repl: support executing shell commands on REPL with !sh [COMMAND] Jan 12, 2024
@spytheman
spytheman merged commit d986f82 into vlang:master Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants