Skip to content

Commit

Permalink
Let cot with - read from stdin (close #1171)
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Jan 25, 2025
1 parent 4821684 commit abc6c20
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- The new “prevent editing” option that prevents documents from being unintentionally edited.
- Introduce the `--readonly` (`-r`) option to the `cot` command-line tool to open documents as read-only.
- Introduce the new AppleScript parameter `editable` to document objects.
- Read input from the standard input when the `cot` command-line tool is used with the `-` option.


### Improvements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ <h2>Examples</h2>
<pre class="source"><code>cot --line -1 --column -1 foo.txt
</code></pre>

<p>Passing a dash (<code>-</code>) in place of filenames enters the mode to receive standard input. The input received will be opened as a new document.</p>

<pre class="source"><code>cot -
</code></pre>

<p>You can also pipe a text. Then, CotEditor opens a new document with the piped text.</p>

<pre class="source"><code>echo &quot;I am a dog but also a cow at the same time.&quot; | cot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ <h2>例</h2>
<pre class="source"><code>cot --line -1 --column -1 foo.txt
</code></pre>

<p>ファイル名の代わりにダッシュを渡すと、標準入力を受け取るモードになります。受け取った入力は新規書類として開かれます。</p>

<pre class="source"><code>cot -
</code></pre>

<p>文字列をパイプで渡すこともできます。CotEditorは渡された文字列を新規書類として開きます。</p>

<pre class="source"><code>echo &quot;I am a dog but also a cow at the same time.&quot; | cot
Expand Down
11 changes: 7 additions & 4 deletions CotEditor/Resources/cot
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def parse_args():
args.new_window = args.new and not args.files

# check file existence and create if needed
if args.files:
if args.files and args.files != ['-']:
# strip symlink
args.files = list(map(os.path.realpath, args.files))
# skip file check if file is directory
Expand Down Expand Up @@ -402,10 +402,13 @@ if __name__ == "__main__":
# parse arguments
args = parse_args()

# read piped text if exists
if args.files or sys.stdin.isatty():
# read standard input if exists
if args.files == ['-']:
stdin = sys.stdin.read()
args.files = []
elif args.files or sys.stdin.isatty():
stdin = None
else:
else: # piped text
stdin = ''.join(sys.stdin)

main(args, stdin)

0 comments on commit abc6c20

Please sign in to comment.