Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F2895236
tsprintf.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Advanced/Developer...
View Handle
View Hovercard
Size
2 KB
Referenced Files
None
Subscribers
None
tsprintf.php
View Options
<?php
/**
* Format text for terminal output. This function behaves like `sprintf`,
* except that all the normal conversions (like "%s") will be properly escaped,
* and additional conversions are supported:
*
* %B (Block)
* Escapes text, but preserves tabs and newlines.
*
* %R (Raw String)
* Inserts raw, unescaped text. DANGEROUS!
*
* Particularly, this will escape terminal control characters.
*/
function
tsprintf
(
$pattern
/* , ... */
)
{
$args
=
func_get_args
(
)
;
$args
[
0
]
=
PhutilConsoleFormatter
::
interpretFormat
(
$args
[
0
]
)
;
$string
=
xsprintf
(
'xsprintf_terminal'
,
null
,
$args
)
;
return
new
PhutilTerminalString
(
$string
)
;
}
/**
* Callback for terminal encoding, see @{function:tsprintf} for use.
*/
function
xsprintf_terminal
(
$userdata
,
&
$pattern
,
&
$pos
,
&
$value
,
&
$length
)
{
$type
=
$pattern
[
$pos
]
;
switch
(
$type
)
{
case
's'
:
$value
=
PhutilTerminalString
::
escapeStringValue
(
$value
,
false
)
;
$type
=
's'
;
break
;
case
'B'
:
$value
=
PhutilTerminalString
::
escapeStringValue
(
$value
,
true
)
;
$type
=
's'
;
break
;
case
'R'
:
$type
=
's'
;
break
;
case
'W'
:
$value
=
PhutilTerminalString
::
escapeStringValue
(
$value
,
true
)
;
$value
=
phutil_console_wrap
(
$value
)
;
$type
=
's'
;
break
;
case
'!'
:
$value
=
tsprintf
(
'<bg:yellow>** <!> %s **</bg>'
,
$value
)
;
$value
=
PhutilTerminalString
::
escapeStringValue
(
$value
,
false
)
;
$type
=
's'
;
break
;
case
'?'
:
$value
=
tsprintf
(
'<bg:green>** ? **</bg> %s'
,
$value
)
;
$value
=
PhutilTerminalString
::
escapeStringValue
(
$value
,
false
)
;
$value
=
phutil_console_wrap
(
$value
,
6
,
false
)
;
$type
=
's'
;
break
;
case
'>'
:
$value
=
tsprintf
(
" **$ %s**\n"
,
$value
)
;
$value
=
PhutilTerminalString
::
escapeStringValue
(
$value
,
false
)
;
$type
=
's'
;
break
;
case
'd'
:
$type
=
'd'
;
break
;
default
:
throw
new
Exception
(
pht
(
'Unsupported escape sequence "%s" found in pattern: %s'
,
$type
,
$pattern
)
)
;
}
$pattern
[
$pos
]
=
$type
;
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Jan 19 2025, 21:09 (6 w, 1 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1128779
Default Alt Text
tsprintf.php (2 KB)
Attached To
Mode
rARC Arcanist
Attached
Detach File
Event Timeline
Log In to Comment