Formatting output using sprintf - format

I have following output with sprintf,
getcmvol:get CM CLI voltage
getcmfirmwareversion: displays CM cli firmware version
getcmserialnum: display CM serial number
I wanted it to output like below,
getcmvol--------------------: get CM CLI voltage
getcmfirmwareversion----: displays CM cli firmware version
getcmserialnum------------: display CM serial number
Pleas Ignore ------- here since this editor is not considering the spaces I have used (hyphen or minus) -----.
printf formats(\t, %3d etc) are not working any help would be appreciated.

So as I understand you problem: You want to output a string field, left-justified with a certain width. To get your a string with this property you could do something like:
... = sprintf("%-20s: %d\n", "getcmvol", voltage);
... = sprintf("%-20s: %d\n", "getcmfirmwareversion", version);
...
The format strings of printf or sprintf are explained in detail e.g. here. In the above format-string %-20s means a 20 chars wide string field, which is left-justified.

Related

golang screen printing formatted data with color attributes

Want to print a row/column table that is formatted as in a typical
fmt.Printf("%5s %5s %5s\n",col1, col2, col3)
Works fine of course if the strings are plain text, but if a string has display attributes
like color, bold, font - even though the visible data is the same length as the plain text,
and would be fine in %5s; doing len(col1) is much longer it skews the table alignment.
Is there a way for Printf to accomplish this, or another std Go package?
Want:
Item Item Item
===== ===== ====
abc defgh xyz
x abc d
vv xxxxx zz <=== this happens if string xxxxx has display attributes from
fatih,gchalk, etc. to set foreground/background color
`
//
package main
import (
"fmt"
"github.com/jwalton/gchalk"
"github.com/fatih/color"
)
func main() {
var colorWithGchalk = gchalk.Red
var data = []string{"one", "ten", "twenty"}
gchalk.SetLevel(gchalk.LevelAnsi16m) // seems needed for gitbash
// note output columns framed by <> just to see actual width
fmt.Println("desired formatted output")
fmt.Printf("<%-10s> <%-10s> <%-10s>\n\n", data[0],data[1],data[2])
/*
** gchalk
*/
// first try using gchalk for color
// colorize second column - column width ignored?
fmt.Println("colorized field loses its 10 character width, so subsequent fields now misaligned")
fmt.Printf("<%-10s> <%-10s> <%-10s>\n", data[0], colorWithGchalk(data[1]), data[2])
// same as above but eliminate gchalk function and just apply colorizing directly - same result
fmt.Printf("<%-10s> <%-10s> <%-10s>\n", data[0], gchalk.Red(data[1]), data[2])
/*
** fatih
*/
fmt.Println("\nwith fatih")
var colorWithFatih = color.New(color.FgRed).SprintFunc()
fmt.Printf("<%-10s> <%-10s> <%-10s>\n", data[0], colorWithFatih(data[1]), data[2])
}
`
Output:
`
desired formatted output
colorized field loses its 10 character width,
so subsequent fields now misaligned
with fatih
`
On screen the above 3 lines display the word "ten" in red as desired, but the field is no longer 10 wide.
Is there a way for Printf to accomplish this,
No
or another std Go package?
No
(What you call "display attributes" are part of the output stream of bytes, they are not "attributes", this is "inline data" interpreted by the terminal emulator. What you can do is filter out this inline data before printing.)
You could use https://github.com/olekukonko/tablewriter as an example for how to output tables or just use the package.
With the advice of Jason Walton the porter of chalk to gchalk. I got fmt.Printf %s to satisfy my need, although their may be issues if the field widths (%s) were narrow.
I wanted to concatenate at least two strings together to provide to one %s.
The first string was plain text (sgCharToPrint) the next string was colorized so it was the actual screen text (missedRaw) (missed was the color string e.g. missedRaw wrapped with ansi formatting characters.
myLen = len(sgCharToPrint) + len(missedRaw)
padded = sgCharToPrint + missed + strings.Repeat(" ", 30 - olen)
fmt.Printf("%30s %4d %10s \n",padded, value, tail)
Now the "table" display stays in alignment.

Converting an ASCII string to USB UTF16 string format expresses as shell string using shell command

I could do this on my head with python or C or Java but after going through dozes of google hits I can't make a start on how to do this with shell commands in a script.
Background:
I've got a (serial) number that I need to convert into a UTF16 string which I will then pass to a utility (HEXMATE) that will plunge it into an Intel hex file. The final format of the string in memory is USB string i.e. two byte length (LSB,MSB) and then all the string characters as UTF16 characters (LSB,MSB). The utility only accepts a string which it will plunge as is (plus null) to the given memory address. Hence I need to embed the string length to the beginning of the string using escapes.
Is this even doable in a script?
I know by now I could have solved this with many other means but I don't want to bring in new tools (outside say posix or MacOs) and I would like to learn how to do this with a script.
So how do I turn "ABC" to "\003\000A\000B\000C000\" using shell tools?
For simplicity we can assume the serial number is ASCII and not longer than 255 characters.
As can be seen from comments this it is probably not possible to do exactly what I asked for. However the underlying actual programming problem I've solved with little python and sed.
here is my shell script:
SRC="firmware.hex"
DST="firmware.hex-with-serialno.hex"
SERNO=`python serialno.py`
REPLACE=":107FC000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC1"
sed "s/$REPLACE/$SERNO/g" <$SRC >$DST
and here is serialno.py
file = open("serialno.txt","r")
serno = int(file.readline())
file.close()
serno = serno + 1
file = open("serialno.txt","w")
print >>file, serno
file.close
t="107FC0001003"
for c in '{:07d}'.format(serno):
t += '{:02X}'.format(ord(c))
t += '00'
s = 0
for i in xrange(0, len(t), 2):
b = t[i:i+2]
s += int(b,16)
s = (-s)&0xFF
t = ":" + t + '{:02X}'.format(s)
print t

Gnuplot: Convert integer to ASCII value

I would like to generate multiple plots within one gnuplot script, and would like to start each plot's title with a running capital letter, i.e., the first plot will have the title (A) sample title for first chart, the second one (B) sample title for second chart, and so on.
In Java, for this I basically have to do
int i = 65; // ASCII value for 65
char c = (char)i; // Convert 65 to corresponding ASCII value ('A')
i++;
// Use c; then repeat
I just tried something similar in gnuplot by using gprintf and using the %c formatter, yet I could not get it working due to the problem
These format specifiers are not the same as those used by the standard C-language routine sprintf().
Long question short: How to convert an integer to its corresponding ASCII value?
gnuplot> print sprintf("%c", 65)
#A
Gnuplot provides a gprintf which uses gnuplot format specifiers and sprintf.

How to split input by ASCII control codes with Progress 4GL?

how can I split this barcode by group separator with Progress? I've tried chr(29) without any luck.
Barcode scanned into Notepad++: http://i.imgur.com/8DmPZ.png
Barcode scanned into input field: 2409271405202120330017100282
Thanks.
def var c as char no-undo.
def var i as int no-undo.
update c format "x(50)".
do i = 1 to length(c):
message substr(c, i, 1) = chr(29).
end.
The problem is that GS is an undefined control code. So you need to make it be recognized.
Add the following to your terminal's entry in protermcap to define GS as F13:
:(F13)=\035:\
(The octal code for GS is \035 and F13 is an undefined function key -- so the combination should work. I don't have a scanner to test with but this works for the control codes that I can type into my keyboard...)
Then use code like this:
define variable bc as character no-undo format "X(50)".
update bc editing:
if lastkey = 313 then
apply ".". /* 313 is the code for F13 */
else
apply lastkey.
end.
This should cause "." to be inserted instead of GS. Which will allow you to parse the string using "." rather than GS.
It's a wild guess, but I'm thinking ENTRY(entry-num, barcode-string, "group-separator-string")?
This works for me:
/* create a test file (otherwise not needed...)
*/
output to "barcode.dat".
put control "240927140520" chr(29) "2120330017" chr(29) "100282".
output close.
/* if you already have barcode.dat start here
*/
define variable m as memptr no-undo.
define variable bc as character no-undo.
set-size( m ) = 100.
input from "barcode.dat" binary no-convert.
import unformatted m.
input close.
bc = get-string( m, 1 ).
display
entry( 1, bc, chr(29)) format "x(12)" skip
entry( 2, bc, chr(29)) format "x(12)" skip
entry( 3, bc, chr(29)) format "x(12)" skip
.

String value formatting

Sometimes in Strings I see something like this %1$s or this %2$d. Can somebody explain to me how to read such things?
Check this document http://download.oracle.com/javase/1,5.0/docs/api/java/util/Formatter.html#syntax the string is basically broken in
%[argument_index$][flags][width][.precision]conversion
From your example %1$s,
% means replace with a parameter
1$ is the position in the parameter array.
s signals that the parameter is a string.
This is taken from Java, but a lot of programming languages use the same syntax for string formatting.
the % stand for the relative argument position, and the "s" or "d" (or others) stands for the type.
This is used to format a string through the printf functions
format= 'The %2$s contains %1$04d monkeys';
printf(format, num, location);
see the printf docs of the langage you use to get all the details (there is a lot)

Resources