Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

@LINE function that supports OptionBase 1

Aug
2,833
149
Display line n from a text file,
or line n from the Windows Clipboard,
based on the value of Windows Environment Variable OptionBase

I wrote this utility for use with Take Command Console (TCC).

TCC uses option base 0 for many,
if not all,
of its functions.

I prefer the first line in a file to be line 1,
not line 0.

Code:
E:\...\Line>set optionbase=1

E:\...\Line>line.exe r:\test.txt 1
Line 1

E:\...\Line>set optionbase=0

E:\...\Line>line.exe r:\test.txt 1
Line 2

I have created a User-Defined Function to replace the internal @LINE function;

Code:
E:\...\Line>function line=`%@execstr[line.exe %1 %2]`

E:\...\Line>which @LINE
@LINE is a user-defined variable function (%@execstr[line.exe %1 %2])

It is used the same as the internal @LINE function.

NOTE: The internal @LINE function does not support the -0 argument.


Code:
E:\...\Line>set optionbase=0

E:\...\Line>echo %@LINE[r:\test.txt 0]
Line 1

E:\...\Line>echo %@LINE[r:\test.txt -0]
Line 5

E:\...\Line>set optionbase=1

E:\...\Line>echo %@LINE[r:\test.txt 0]
EOF

E:\...\Line>echo %@LINE[r:\test.txt 1]
Line 1

E:\...\Line>echo %@LINE[r:\test.txt -0]
Line 5

Line.exe does NOT support;

the line number in hexadecimal format
input from standard input if you specify CON as the filename
redirected input to @LINE
the TCC clipboards (CLIP0: - CLIP9:)
the TCC temporary files (TMP0: - TMP9:)

Here's how I compile line.cs on my Windows 10 system;

Code:
E:\...\Line>which csc
csc is an alias : "C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\csc.exe"

E:\...\Line>csc line.cs
Microsoft (R) Visual C# Compiler version 4.14.0-3.26424.7 (a528c90a)
Copyright (C) Microsoft Corporation. All rights reserved.

Review the contents of LineTest.btm for a demonstration on how to use Line.exe

Source code and files mentioned here available on GitHub:
GitHub - joec4281/Line: Display line n from a text file, or line n from the Windows Clipboard, based on the value of Windows Environment Variable OptionBase

Joe
 
Last edited:
Nice solution Joe.

Another possible solution, if you are willing to use new function names, would be to define your own Base1 functions as pass through functions.

Code:
[C:\Program Files\JPSoft\TCMD36]function line1=`%@line[%1,%@dec[%2]]`

[C:\Program Files\JPSoft\TCMD36]function lines1=`%@inc[%@lines[%1]]`



[C:\Program Files\JPSoft\TCMD36]echo %@line[readme.txt,0]
                         Take Command v36.50

[C:\Program Files\JPSoft\TCMD36]echo %@line1[readme.txt,1]
                         Take Command v36.50



[C:\Program Files\JPSoft\TCMD36]echo %@lines[readme.txt]
221

[C:\Program Files\JPSoft\TCMD36]echo %@lines1[readme.txt]
222

This approach is not reliant on an external .exe and should allow the new functions to leverage all the functionality of the underlying functions.

Of course, just using passthrough functions could make it more challenging to add functionality like the -0 option.


I have not found a way to redefine an existing function using the same name without causing an infinite loop. So this doesn't work.

Code:
REM ---  This does NOT work. 
function line=`%@line[%1,%@dec[%2]]`

If there was a way in the Function command to refer to a native function (like you can reference native/unaliased commands in an alias by prefixing the command name with *) then it could be possible to redefine functions to behave the way you want but use the same name. Perhaps that could be a future enhancement to the Function command, if enough people found it useful.
 
Indeed,
there are other ways to achieve this.

I was going to write it in PowerShell 5.1 (line.ps1),
but the TC-PowerShellHost64.dll only supports MTA,
not STA,
which is required for clipboard access.

Thus,
I decided to us C#,
that way I can run line.exe from TCC, CMD, and PowerShell.

Mind you,
I could have also done a callback into TCC from PowerShell,
to obtain the clipboard contents,
that way I avoid the whole MTA/STA issue.

I'll see if I can come up with a .ps1 solution.

Joe
 
Back
Top