Skip to content
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# Binary assets
*.png binary
*.ico binary
*.gif binary
*.pfx binary
*.p12 binary
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:
mkdir -p dist
cp out/termrest* dist/ 2>/dev/null || true
rm -rf dist/*.pdb dist/*.dbg dist/*.dSYM
cp README.md dist/
cp README.md LICENSE dist/
ls -la dist
- uses: actions/upload-artifact@v4
with:
Expand Down
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Deterministic>true</Deterministic>
<Version>0.1.0</Version>
<Product>TermRest</Product>
<Copyright>Copyright (c) 2026 Shike Chen</Copyright>
<Description>A lightweight, mouse-friendly terminal REST client with Postman-compatible collections.</Description>
<NeutralLanguage>en</NeutralLanguage>
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
Expand Down
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 Shike Chen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ A runtime answers in about a millisecond once started; starting takes 0.1–0.3
**Choosing the runtimes**

- **In the UI:** *Tools › Scripts…*, one row for each language. Type a command (`python3`, `node`, `bun`, `dotnet`, `pwsh`) or a path, which may be relative to the workspace (`.venv/bin/python`, `.venv\Scripts\python.exe`). A command is looked up in the folders of PATH only (on Windows as `name.exe`, unless the name already ends with a program extension from PATHEXT, such as `.cmd`: `python3.12` is `python3.12.exe`; for pyenv-win's shim, type `python.bat`), never in the workspace or TermRest's own folder. A file picked with *Browse…* in the workspace's own folder is kept as `./name`, a path. Or press **Find**: for Python, it looks for `.venv`, `venv` and `env` in the workspace, then `python3` and `python` on PATH; for JavaScript, `node` and `bun` on PATH; for C#, `dotnet` on PATH, then `~/.dotnet`; for PowerShell, `pwsh` on PATH, then `C:\Program Files\PowerShell\7` on Windows. A Python found in the workspace is filled in without being run, as it may have come with the repository: **Check** runs it and shows the version. Python 3.8, Node.js 18 or Bun 1.1, the .NET 10 SDK, and PowerShell 7.0 or later are needed (for C#, the SDK: a .NET runtime alone cannot build scripts; for PowerShell, `pwsh`: Windows PowerShell 5.1, `powershell.exe`, is too old).
- **Your choices are kept per workspace folder in your user settings**, never in the workspace. A cloned repository cannot choose a program for TermRest to run.
- **Your choices are kept per workspace folder in your user settings**, never in the workspace. A cloned repository cannot choose a program for TermRest to run. Outside Windows, a folder is known by its path exactly as written: a path in another case may be another folder (on Linux, and on a macOS volume formatted case-sensitive), so neither these choices nor the proxy and files from outside the folder you allowed carry over to it.
- **On the command line:** `--python <path>`, `--javascript <path>`, `--csharp <path>` and `--powershell <path>` on `send` and `run`; without them, the UI's choices for that workspace apply. A path there is relative to the current folder, as the command line's other paths are (from a repository's root: `termrest run postman/api.postman_collection.json --python .venv/bin/python`); a command such as `node` is looked up on PATH. `--script-order <languages>` changes the [language order](#languages) for the run. `--no-scripts` turns scripts off.
- **No runtime chosen for a language:** in the UI, its scripts are skipped and the request is sent; the status line says `⚠ scripts not run` and the *Scripts* tab says which. On the command line, that is an error (so a CI run never passes without its tests): the request is not sent, and the message says to pass `--python`, `--javascript`, `--csharp`, `--powershell` or `--no-scripts`. A missing runtime does not make a request choose another language: the language depends on the scripts only (see below).

Expand Down Expand Up @@ -771,3 +771,7 @@ Notes:
- Proxy authentication.
- Code snippets.
- **Packaging:** winget, Homebrew, `.deb`/`.rpm`, and signed binaries.

## License

TermRest is released under the [MIT License](LICENSE). Its icon is in [`assets`](assets): `termrest.svg` is the source, and `termrest.ico` and `termrest.png` are drawn from it.
Binary file added assets/termrest.ico
Binary file not shown.
Binary file added assets/termrest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions assets/termrest.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/TermRest.Cli/TermRest.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<StripSymbols>true</StripSymbols>
<ServerGarbageCollector>false</ServerGarbageCollector>
<ConcurrentGarbageCollection>false</ConcurrentGarbageCollection>
<!--
The Windows icon of termrest.exe: assets/termrest.svg drawn at 16, 20, 24, 32, 40, 48 and 64 px (32-bit bitmaps) and
256 px (PNG), the largest size Windows takes from an icon. Native AOT copies it into the executable.
-->
<ApplicationIcon>..\..\assets\termrest.ico</ApplicationIcon>
</PropertyGroup>

<ItemGroup>
Expand Down
53 changes: 47 additions & 6 deletions src/TermRest.Core/Import/ShellWords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class ShellWords
{
/// <summary>
/// Guesses the shell from continuation characters and Windows-only escapes outside quoted values (see
/// <see cref="FindMarks"/> and <see cref="HasPowerShellContinuation"/>); defaults to POSIX.
/// <see cref="FindMarks"/>, <see cref="HasPowerShellContinuation"/> and <see cref="HasCmdContinuation"/>); defaults to POSIX.
/// </summary>
public static ShellDialect Detect(string text)
{
Expand All @@ -43,9 +43,19 @@ public static ShellDialect Detect(string text)
return ShellDialect.Posix;
}

if (HasPowerShellContinuation(text))
// A quoted value left open may be one another shell closes, or never opens: PowerShell's "…" ends at the quote after
// a \ ("C:\temp\"), and cmd.exe has no '…' (foo'bar). A line continuation outside that shell's quoted values tells.
if (marks.Unclosed)
{
return ShellDialect.PowerShell;
if (HasPowerShellContinuation(text))
{
return ShellDialect.PowerShell;
}

if (HasCmdContinuation(text))
{
return ShellDialect.Cmd;
}
}

var start = text.TrimStart();
Expand Down Expand Up @@ -80,10 +90,11 @@ public static ShellDialect Detect(string text)
/// <c>curl ^"https://…^"</c>), outside quoted values, which say nothing about the shell: '…' (PowerShell's '' inside one
/// ends it and starts another), $'…' (where \ escapes the next character) and "…" (where \ and ` do). Outside them, \, ^
/// and ` escape the next character, so \' and ^" start no quoted value, and a # that starts a word starts a comment.
/// Also whether a quoted value is left open, as the text ends before its closing quote.
/// </summary>
private static (bool Backtick, bool Caret, bool Backslash, bool CaretQuote) FindMarks(string text)
private static (bool Backtick, bool Caret, bool Backslash, bool CaretQuote, bool Unclosed) FindMarks(string text)
{
bool backtick = false, caret = false, backslash = false, caretQuote = false;
bool backtick = false, caret = false, backslash = false, caretQuote = false, unclosed = false;
for (var i = 0; i < text.Length; i++)
{
var wordStart = i == 0 || IsBlank(text[i - 1]);
Expand All @@ -102,12 +113,15 @@ private static (bool Backtick, bool Caret, bool Backslash, bool CaretQuote) Find
break;
case '\'':
i = QuotedEnd(text, i + 1, '\'', "");
unclosed |= i >= text.Length;
break;
case '$' when i + 1 < text.Length && text[i + 1] == '\'':
i = QuotedEnd(text, i + 2, '\'', "\\");
unclosed |= i >= text.Length;
break;
case '"':
i = QuotedEnd(text, i + 1, '"', "\\`");
unclosed |= i >= text.Length;
break;
case '#' when wordStart:
while (i + 1 < text.Length && text[i + 1] != '\n')
Expand All @@ -119,7 +133,7 @@ private static (bool Backtick, bool Caret, bool Backslash, bool CaretQuote) Find
}
}

return (backtick, caret, backslash, caretQuote);
return (backtick, caret, backslash, caretQuote, unclosed);
}

/// <summary>Where the quoted value whose text starts at <paramref name="i"/> ends: its closing quote, or the end of the text.</summary>
Expand Down Expand Up @@ -174,6 +188,33 @@ private static bool HasPowerShellContinuation(string text)
return false;
}

/// <summary>
/// Whether a ^ ends a line outside quoted values as cmd.exe reads them: only "…", in which ^ is a character. An apostrophe
/// is a character there too (foo'bar), where <see cref="FindMarks"/> starts a quoted value that hides the ^ ending a line.
/// </summary>
private static bool HasCmdContinuation(string text)
{
var quoted = false;
for (var i = 0; i < text.Length; i++)
{
if (text[i] == '"')
{
quoted = !quoted;
}
else if (text[i] == '^' && !quoted)
{
if (NewlineLength(text, i + 1) > 0)
{
return true;
}

i++;
}
}

return false;
}

/// <summary>Length of the line break starting at <paramref name="index"/> (LF or CRLF), or 0.</summary>
private static int NewlineLength(string text, int index) =>
index >= text.Length ? 0
Expand Down
41 changes: 35 additions & 6 deletions src/TermRest.Core/Postman/V3/V3Names.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Runtime.ExceptionServices;

namespace TermRest.Core.Postman.V3;

/// <summary>File and folder naming rules of the Postman collection format v3 layout.</summary>
Expand Down Expand Up @@ -84,18 +86,45 @@ public static bool IsCollectionDirectory(string directory) =>
private static bool HasContent(string directory) =>
File.Exists(DefinitionPath(directory)) || Directory.EnumerateFiles(directory, "*.request.y*ml").Any(IsRequestFile);

/// <summary>Whether folders of <paramref name="directory"/> (not hidden ones, nor links) have definitions or requests.</summary>
/// <summary>
/// Whether folders of <paramref name="directory"/> (not hidden ones, nor links) have definitions or requests. A folder that
/// cannot be listed (another user's) may hold them: when no other folder has any, its error is thrown, so that loading
/// says why rather than the collection being left out without a word.
/// </summary>
private static bool FoldersHaveContent(string directory, int depth)
{
try
if (depth <= 0)
{
return false;
}

Exception? unreadable = null;
foreach (var sub in Directory.EnumerateDirectories(directory, "*", FolderSearch))
{
return depth > 0 && Directory.EnumerateDirectories(directory, "*", FolderSearch)
.Any(sub => !Path.GetFileName(sub).StartsWith('.') && (HasContent(sub) || FoldersHaveContent(sub, depth - 1)));
if (Path.GetFileName(sub).StartsWith('.'))
{
continue;
}

try
{
if (HasContent(sub) || FoldersHaveContent(sub, depth - 1))
{
return true;
}
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
unreadable ??= ex; // another folder may have them
}
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)

if (unreadable is not null)
{
return false;
ExceptionDispatchInfo.Throw(unreadable);
}

return false;
}

/// <summary>Whether <paramref name="directory"/> is in a <c>postman/collections</c> folder, where Postman keeps collections.</summary>
Expand Down
7 changes: 6 additions & 1 deletion src/TermRest.Core/Workspace/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,13 @@ public bool AllowOutsideFiles(string workspaceRoot, bool allow)
return true;
}

/// <summary>
/// Whether two workspace folders are the same, for the choices kept for them: on Windows without regard to case, as its
/// file systems are; elsewhere by their paths exactly, as on Linux and on a case-sensitive macOS volume work/api and
/// work/API are two folders (a repository cloned next to a trusted one must not get its runtimes or approvals).
/// </summary>
private static bool SameFolder(string a, string b) =>
string.Equals(NormalizeFolder(a), NormalizeFolder(b), OperatingSystem.IsWindows() || OperatingSystem.IsMacOS() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
string.Equals(NormalizeFolder(a), NormalizeFolder(b), OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);

private static string NormalizeFolder(string path)
{
Expand Down
23 changes: 23 additions & 0 deletions tests/TermRest.Core.Tests/CurlImporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,29 @@ public void A_powershell_value_that_ends_in_a_backslash_ends_at_its_quote()
Assert.Equal(("https://h/upload", "C:\\temp\\"), (result.Spec.Url, result.Spec.Body.Raw));
}

[Fact]
public void A_cmd_value_that_holds_an_apostrophe_keeps_the_lines_that_follow()
{
// cmd.exe has no '…': an apostrophe is a character there, where the other shells start a quoted value with it.
var result = CurlImporter.Import("curl https://h/x --data foo'bar ^\n -H X-Test:ok");

Assert.Equal(ShellDialect.Cmd, result.Dialect);
Assert.Equal(("foo'bar", "ok"), (result.Spec.Body.Raw, Header(result.Spec, "X-Test")));
}

[Theory]
[InlineData("curl https://h/x -d 'it'\\''s ```\nx'", "it's ```\nx")]
[InlineData("curl https://h/x -d 'a ^\nb'", "a ^\nb")]
public void A_line_that_ends_inside_a_closed_quoted_value_says_nothing_about_the_shell(string command, string body)
{
// Read as PowerShell or cmd.exe would read it, the quotes of 'it'\''s fall elsewhere: only a value they leave open
// has them try other shells' quoting.
var result = CurlImporter.Import(command);

Assert.Equal(ShellDialect.Posix, result.Dialect);
Assert.Equal(body, result.Spec.Body.Raw);
}

[Fact]
public void Browser_escapes_of_emoji_keep_the_character()
{
Expand Down
32 changes: 32 additions & 0 deletions tests/TermRest.Core.Tests/PostmanV3Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,38 @@ public void Links_of_a_workspace_to_folders_that_are_gone_are_load_errors()
}

/// <summary>Lets the current user list <paramref name="folder"/>, or not.</summary>
[Fact]
public void A_collection_whose_requests_are_in_a_folder_that_cannot_be_listed_is_a_load_error()
{
// Without a definition, only its folders tell a folder of postman/collections is a collection: one that cannot be
// listed (another user's) may hold its requests, so the collection is not left out without a word.
var root = TestFiles.NewTempDirectory();
var cart = Directory.CreateDirectory(Path.Combine(root, "postman", "collections", "Shop", "Cart")).FullName;
File.WriteAllText(Path.Combine(cart, "Add item.request.yaml"), "$kind: http-request\nurl: https://shop.example.com/cart/items\nmethod: POST\n");
Listable(cart, false);
try
{
try
{
Directory.GetFileSystemEntries(cart);
Assert.Skip("This user can list any folder (root).");
}
catch (UnauthorizedAccessException)
{
// as for other users
}

var workspace = ApiWorkspace.Open(root);

Assert.Empty(workspace.Collections);
Assert.Contains(workspace.LoadErrors, e => e.StartsWith("Shop: ", StringComparison.Ordinal));
}
finally
{
Listable(cart, true);
}
}

private static void Listable(string folder, bool listable)
{
if (OperatingSystem.IsWindows())
Expand Down
Loading
Loading