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

Move parsing values to Zen processor #30

Merged
merged 1 commit into from
Sep 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/Dialog/AbstractDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,6 @@ protected function decamelize($name)
return strtolower(preg_replace('/([a-z])([A-Z])/', '$1-$2', $name));
}

/**
* Parse the given value from the dialog to the appropriate return value for this dialog
*
* @param string $value The raw value received from the dialog window
* @return mixed The logical value presented to the user
* @internal
*/
public function parseValue($value)
{
return $value;
}

/**
* Create a dialog handler to process the results for this dialog.
*
Expand Down
16 changes: 6 additions & 10 deletions src/Dialog/CalendarDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace Clue\React\Zenity\Dialog;

use Clue\React\Zenity\Dialog\AbstractTextDialog;
use \DateTime;
use DateTime;
use React\Promise\Deferred;
use Icecave\Mephisto\Process\ProcessInterface;
use Clue\React\Zenity\Zen\CalendarZen;

/**
* Use the --calendar option to create a calendar dialog.
Expand Down Expand Up @@ -83,15 +86,8 @@ public function setDateTime(DateTime $date)
return $this;
}

/**
* Parses the date string returned from the dialog into a DateTime object
*
* @internal
* @see parent::parseValue()
* @return \DateTime
*/
public function parseValue($value)
public function createZen(Deferred $deferred, ProcessInterface $process)
{
return new DateTime($value);
return new CalendarZen($deferred, $process);
}
}
16 changes: 5 additions & 11 deletions src/Dialog/ColorSelectionDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Clue\React\Zenity\Dialog;

use Clue\React\Zenity\Dialog\AbstractDialog;
use React\Promise\Deferred;
use Icecave\Mephisto\Process\ProcessInterface;
use Clue\React\Zenity\Zen\ColorSelectionZen;

/**
* Use the --color-selection option to create a color selection dialog.
Expand Down Expand Up @@ -40,17 +43,8 @@ public function setShowPalette($palette)
return $this;
}

/**
* Parses the color string returned from the dialog into a #rrggbb string
*
* @internal
* @see parent::parseValue()
* @return string
* @link https://answers.launchpad.net/ubuntu/+source/zenity/+question/204096
*/
public function parseValue($value)
public function createZen(Deferred $deferred, ProcessInterface $process)
{
// convert zenity's #rrrrggggbbbb to #rrggbb by skipping duplicate info
return '#' . substr($value, 1, 2) . substr($value, 5, 2) . substr($value, 9, 2);
return new ColorSelectionZen($deferred, $process);
}
}
31 changes: 5 additions & 26 deletions src/Dialog/FileSelectionDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Clue\React\Zenity\Dialog;

use Clue\React\Zenity\Dialog\AbstractDialog;
use SplFileInfo;
use React\Promise\Deferred;
use Icecave\Mephisto\Process\ProcessInterface;
use Clue\React\Zenity\Zen\FileSelectionZen;

/**
* Use the --file-selection option to create a file selection dialog.
Expand Down Expand Up @@ -120,31 +122,8 @@ public function setFileFilter($filter)
return $this;
}

/**
* Parses the path string returned from the dialog into a SplFileInfo object
*
* Usually, this will return a single SplFileInfo object.
*
* If the `setMultiple(true)` option is active, this will return an array
* of SplFileInfo objects instead. The size of the array depends on the
* number of files selected by the user.
*
* @internal
* @see parent::parseValue()
* @return SplFileInfo|SplFileInfo[] a single or any number of SplFileInfo objects depending on the multiple setting
* @see self::setMultiple()
*/
public function parseValue($value)
public function createZen(Deferred $deferred, ProcessInterface $process)
{
if ($this->multiple) {
$ret = array();

foreach(explode($this->separator, $value) as $path) {
$ret[] = new SplFileInfo($path);
}

return $ret;
}
return new SplFileInfo($value);
return new FileSelectionZen($deferred, $process, $this->multiple, $this->separator);
}
}
14 changes: 5 additions & 9 deletions src/Dialog/FormsDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Clue\React\Zenity\Dialog;

use Clue\React\Zenity\Dialog\AbstractTextDialog;
use React\Promise\Deferred;
use Icecave\Mephisto\Process\ProcessInterface;
use Clue\React\Zenity\Zen\FormsZen;

/**
* Use the --forms option to create a forms dialog.
Expand Down Expand Up @@ -150,15 +153,8 @@ public function getArgs()
return array_merge(parent::getArgs(), $this->fields);
}

/**
* Parses the input string returned from the dialog into an array of string values
*
* @return string[]
* @internal
* @see parent::parseValue()
*/
public function parseValue($value)
public function createZen(Deferred $deferred, ProcessInterface $process)
{
return explode($this->separator, $value);
return new FormsZen($deferred, $process, $this->separator);
}
}
32 changes: 6 additions & 26 deletions src/Dialog/ListDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Clue\React\Zenity\Dialog;

use Clue\React\Zenity\Dialog\AbstractTextDialog;
use React\Promise\Deferred;
use Icecave\Mephisto\Process\ProcessInterface;
use Clue\React\Zenity\Zen\ListZen;

/**
* Use the --list option to create a list dialog.
Expand Down Expand Up @@ -201,33 +204,10 @@ public function getArgs()
return array_merge(parent::getArgs(), $this->columns);
}

/**
* Parses the string returned from the dialog
*
* Usually, this will return a single string.
*
* If the `setMultiple(true)` option is active or this is a checklist, this
* will return an array of strings instead. The size of the array depends on
* the number of rows selected by the user.
*
* @internal
* @see parent::parseValue()
* @return string|string[] a single or any number of SplFileInfo objects depending on the multiple setting
* @see self::setMultiple()
*/
public function parseValue($value)
public function createZen(Deferred $deferred, ProcessInterface $process)
{
if (trim($value) === '') {
// TODO: move logic
return false;
}
$single = (!$this->multiple && !$this->checklist);

// always split on separator, even if we only return a single value (explicitly or a checklist)
// work around an issue in zenity 3.8: https://bugzilla.gnome.org/show_bug.cgi?id=698683
$value = explode($this->separator, $value);
if (!$this->multiple && !$this->checklist) {
$value = $value[0];
}
return $value;
return new ListZen($deferred, $process, $single, $this->separator);
}
}
26 changes: 5 additions & 21 deletions src/Dialog/PasswordDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Clue\React\Zenity\Dialog;

use Clue\React\Zenity\Dialog\AbstractDialog;
use React\Promise\Deferred;
use Icecave\Mephisto\Process\ProcessInterface;
use Clue\React\Zenity\Zen\PasswordZen;

/**
* Use the --password option to create a password entry dialog.
Expand Down Expand Up @@ -32,9 +35,6 @@
*/
class PasswordDialog extends AbstractDialog
{
/** @var string */
const SEPARATOR = '|';

protected $username = false;

/**
Expand All @@ -50,24 +50,8 @@ public function setUsername($username)
return $this;
}

/**
* Parses the string returned from the dialog
*
* Usually, this will return a single password string.
*
* If the `setUsername(true)` option is active, this will return an array
* of string username and string password.
*
* @internal
* @see parent::parseValue()
* @return string|string[] a single password string or an array($user, $pass) depending on the username setting
* @see self::setUsername()
*/
public function parseValue($value)
public function createZen(Deferred $deferred, ProcessInterface $process)
{
if ($this->username) {
return explode(self::SEPARATOR, $value, 2);
}
return $value;
return new PasswordZen($deferred, $process, $this->username);
}
}
14 changes: 5 additions & 9 deletions src/Dialog/ScaleDialog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Clue\React\Zenity\Dialog;

use Clue\React\Zenity\Dialog\AbstractTextDialog;
use React\Promise\Deferred;
use Icecave\Mephisto\Process\ProcessInterface;
use Clue\React\Zenity\Zen\ScaleZen;

/**
* Use the --scale option to create a scale dialog.
Expand Down Expand Up @@ -87,15 +90,8 @@ public function setHideValue($toggle = true)
return $this;
}

/**
* Converts the scale string returned from the dialog to an integer
*
* @internal
* @see parent::parseValue()
* @return int
*/
public function parseValue($value)
public function createZen(Deferred $deferred, ProcessInterface $process)
{
return (int)$value;
return new ScaleZen($deferred, $process);
}
}
4 changes: 2 additions & 2 deletions src/Launcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ public function launch(AbstractDialog $dialog)

$zen = $dialog->createZen($deferred, $process);

$process->outputStream()->on('end', function() use ($process, $zen, &$result, $dialog, $deferred) {
$process->outputStream()->on('end', function() use ($process, $zen, &$result, $deferred) {
$code = $process->status()->exitCode();
if ($code !== 0) {
$deferred->reject($code);
} else {
if ($result === null) {
$result = true;
} else {
$result = $dialog->parseValue(trim($result));
$result = $zen->parseValue(trim($result));
}
$deferred->resolve($result);
}
Expand Down
12 changes: 12 additions & 0 deletions src/Zen/BaseZen.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,16 @@ protected function writeln($line)
{
$this->process->inputStream()->write($line . PHP_EOL);
}

/**
* Parse the given value from the dialog to the appropriate return value for this dialog
*
* @param string $value The raw value received from the dialog window
* @return mixed The logical value presented to the user
* @internal
*/
public function parseValue($value)
{
return $value;
}
}
21 changes: 21 additions & 0 deletions src/Zen/CalendarZen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Clue\React\Zenity\Zen;

use Clue\React\Zenity\Zen\BaseZen;
use DateTime;

class CalendarZen extends BaseZen
{
/**
* Parses the date string returned from the dialog into a DateTime object
*
* @internal
* @see parent::parseValue()
* @return \DateTime
*/
public function parseValue($value)
{
return new DateTime($value);
}
}
22 changes: 22 additions & 0 deletions src/Zen/ColorSelectionZen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Clue\React\Zenity\Zen;

use Clue\React\Zenity\Zen\BaseZen;

class ColorSelectionZen extends BaseZen
{
/**
* Parses the color string returned from the dialog into a #rrggbb string
*
* @internal
* @see parent::parseValue()
* @return string
* @link https://answers.launchpad.net/ubuntu/+source/zenity/+question/204096
*/
public function parseValue($value)
{
// convert zenity's #rrrrggggbbbb to #rrggbb by skipping duplicate info
return '#' . substr($value, 1, 2) . substr($value, 5, 2) . substr($value, 9, 2);
}
}
Loading