Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch feature #117

Closed
wants to merge 1 commit into from
Closed

Watch feature #117

wants to merge 1 commit into from

Conversation

henzeb
Copy link

@henzeb henzeb commented Feb 24, 2024

What?

Introducing watch. A simple feature that mimics the basic features of the watch function found on Linux/Unix systems.

Why?

I've developed a lot of command line tools, and I often needed to monitor output in the console. Most of the time with tables and such.

Using the watch functionality on Linux systems should do it, but this requires the entire framework to be loaded every time, which can put a strain on the server capacity. With this, we don't even need cache, but we can use the once function instead for things that shouldn't be cached, but still are needed.

Example

In the playground directory, there is an example with a table updating with new records every second. Here to just demonstrate the simplicity.

use function Laravel\Prompts\table;
use function Laravel\Prompts\watch;

watch(
   watch: function(){
       table(['header'], [random_int()]);
   },
   interval: 2 // intervals are in seconds
);

###Testing

Watch comes with a few testing tools.

fakeTimes

By default it iterates just once, but you can tell how many iterations watch should execute.

use Laravel\Prompts\Prompts;
use Laravel\Prompts\Watch;
use function Laravel\Prompts\watch;
use function Laravel\Prompts\info;

Prompts::fake();
Watch::fakeTimes(2);

watch(
    function(){
       info('hello world');
    }
);

Prompts::content(); // would contain hello world twice

assertSecondsSleptBetweenIntervals

Allows you to assert the total seconds slept between intervals. Allows users to test if they passed the correct value.

use Laravel\Prompts\Prompts;
use Laravel\Prompts\Watch;
use function Laravel\Prompts\watch;
use function Laravel\Prompts\info;

Prompts::fake();
Watch::fakeTimes(2);

watch(
    function(){
       info('hello world');
    }
);

Watch::assertSecondsSleptBetweenIntervals(4); // would succeed because default interval is 2 and it iterated twice.

shouldNotFakeIntervalSleep

This is not really a thing users would most likely use, but I used it to allow actually test sleeping. I can remove it if you don't want us to actually test sleep functions.

Considerations

Right now, Prompt is resetting the cursor position using the lines count. When a user presses enter, this messes up the output.

I added a new method isFaked to the FakesInputOutput trait which can be used to detect if Prompt is currently being faked. This might be helpful in the future if things like sections as described in #115 are going to be added.

@henzeb henzeb changed the title [FEATURE] watch [Feature] watch Feb 24, 2024
@henzeb henzeb force-pushed the feature-watch branch 9 times, most recently from 00a2fe6 to 7a2c1bc Compare February 25, 2024 07:46
@henzeb henzeb marked this pull request as draft February 25, 2024 08:10
@henzeb henzeb force-pushed the feature-watch branch 10 times, most recently from 069b3a1 to abf8363 Compare February 25, 2024 11:25
@henzeb henzeb marked this pull request as ready for review February 25, 2024 11:31
@driesvints driesvints changed the title [Feature] watch Watch feature Feb 25, 2024
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

@henzeb henzeb deleted the feature-watch branch February 25, 2024 19:40
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.

2 participants